<template>
  <div class="main-container" v-show="loaded" style="margin:0 0.15rem;">
    <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}/tag`
        wx.miniProgram.navigateTo({ url })
      } else {
        this.$router.push({ name: 'courseLearnTag', params: { id: data.id } })
      }
    }
  },
  beforeMount() {
    this.getCourseList()
  }
}
</script>

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