<template>
  <div class="main-container" v-if="loaded">
    <template v-if="detail.chapters && detail.chapters.length">
      <course-chapter :courseId="courseId" :data="detail.chapters"></course-chapter>
    </template>
    <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.title
    }
  },
  data() {
    return {
      loaded: false,
      detail: {}
    }
  },
  computed: {
    courseId() {
      return this.$route.params.id
    },
    title() {
      return this.detail.curriculum ? this.detail.curriculum.curriculum_name : ''
    }
  },
  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>
