<template>
  <div class="main-container" style="margin:0 0.15rem">
    <course-list :show-progress="true" :requestCallback="requestCallback" @on-click="handleClick">
      <template #empty>
        <van-empty description="您还没有学习课程，快快开始学习吧！" />
      </template>
    </course-list>
  </div>
</template>

<script>
import CourseList from '@/components/CourseList.vue'

export default {
  name: 'MyCourse',
  components: { CourseList },
  metaInfo: {
    title: '已学课程'
  },
  data() {
    return {}
  },
  computed: {
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    requestCallback(response) {
      // 扁平数据
      const flattenList = response.reduce((result, item) => {
        return result.concat(item.child)
      }, [])
      // 查找有没有进度，没有进度返回空数组
      const found = flattenList.find(item => item.video_progress)
      return found ? response : []
    },
    handleClick(data) {
      if (this.isWeapp) {
        const path = `${window.location.origin}/my/learn/${data.id}?refresh=true`
        const url = `/pages/web/index?src=${encodeURIComponent(path)}`
        wx.miniProgram.navigateTo({ url })
      } else {
        this.$router.push({ name: 'myLearnItem', params: { id: data.id } })
      }
    }
  }
}
</script>
