<template>
  <div class="main-container" v-if="loaded">
    <course-chapter
      :courseId="courseId"
      :data="detail.chapters"
      :show-progress="true"
      v-if="detail.chapters && detail.chapters.length"
    ></course-chapter>
    <van-empty description="暂无内容" v-else />
  </div>
</template>

<script>
import CourseChapter from '@/components/CourseChapter.vue'
import * as api from '@/api/course.js'
export default {
  name: 'CourseLearnItem',
  components: { CourseChapter },
  metaInfo() {
    return {
      title: this.detail.course_name || ''
    }
  },
  data() {
    return {
      tabActive: 0,
      loaded: false,
      detail: {}
    }
  },
  computed: {
    courseId() {
      return this.$route.params.id
    }
  },
  methods: {
    // 课程学习
    getCourse() {
      this.loaded = false
      api.getCourse(this.courseId).then(response => {
        this.loaded = true
        response.chapters = response.chapters.filter(item => {
          item.children = item.children.filter(
            child => child.type === 2 && child.resource_id && child.resource_id !== '6684350363920760832'
          )
          return item.children.length
        })
        this.detail = response
      })
    }
  },
  beforeMount() {
    this.getCourse()
  }
}
</script>

<style lang="scss" scoped>
.course-title {
  font-size: 15px;
  font-weight: 600;
  color: #222;
  padding: 20px 0 10px;
}
.tab-content {
  margin-left: -0.4rem;
  margin-right: -0.4rem;
  min-height: 100vh;
  background: #eee;
  padding: 0.4rem;
}
</style>
