<template>
  <div class="main-container" v-show="loaded">
    <template v-if="list.length">
      <course-item v-for="item in list" :data="item" :key="item.course_id" @on-click="handleClick" />
    </template>
    <van-empty description="暂无内容" v-else />
  </div>
</template>

<script>
import * as api from './api'
import CourseItem from '@/components/CourseItem2.vue'

export default {
  name: 'Course',
  components: { CourseItem },
  metaInfo: {
    title: '课程学习'
  },
  data() {
    return {
      loaded: false,
      list: []
    }
  },
  computed: {
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    getCourseList() {
      api
        .getCourseList()
        .then(response => {
          this.list = response
        })
        .finally(() => {
          this.loaded = true
        })
    },
    handleClick(data) {
      if (this.isWeapp) {
        const url = `/pages/web/index?src=${window.location.origin}/course/learn/${data.id}`
        wx.miniProgram.navigateTo({ url })
      } else {
        this.$router.push({
          name: 'courseLearnItem',
          params: { id: data.id }
        })
      }
    }
  },
  beforeMount() {
    this.getCourseList()
  }
}
</script>

<style lang="scss">
.course-item:last-child {
  border-bottom: 0;
}
</style>
