<template>
  <div class="course-tag-wrapper" v-show="loaded">
    <div class="course-tag-hd">
      <h1 class="course-title">{{detail.course_name}}</h1>
    </div>
    <div class="course-tag-bd">
      <div class="course-tag-main">
        <h1 class="tag-title">{{chapter.name}}</h1>
        <ul class="tag-list">
          <li class="tag-item" v-for="item in tagList" :key="item.id" @click="onClick(item)">
            <span>{{item.title}}</span>
            <van-icon name="arrow" />
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from '@/api/course.js'
export default {
  name: 'CourseTagList',
  metaInfo() {
    return {
      title: this.isTest ? '知识点小测' : '知识点速学'
    }
  },
  data() {
    return {
      loaded: false,
      detail: { chapters: [] }
    }
  },
  computed: {
    courseId() {
      return this.$route.params.courseId
    },
    chapterId() {
      return this.$route.params.chapterId
    },
    chapter() {
      const found = this.detail.chapters.find(
        item => item.id === this.chapterId
      )
      return found || {}
    },
    tagList() {
      return this.chapter.tag || []
    },
    isTest() {
      return this.$route.query.is_test === '1'
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    // 获取知识点列表
    getCourseTagList() {
      this.loaded = false
      api.getCourseTagList(this.courseId).then(response => {
        this.loaded = true
        this.detail = response
      })
    },
    onClick({ id }) {
      const path = `/exam/answer?type=2&tag_id=${id}&papersUrl=zy/v2/examination/course-papers`
      if (this.isWeapp) {
        if (this.isTest) {
          // 知识点测试
          const src = encodeURIComponent(`${window.location.origin}${path}`)
          wx.miniProgram.navigateTo({ url: `/pages/web/index?src=${src}` })
        } else {
          wx.miniProgram.navigateTo({
            url: `/pages/web/index?src=${window.location.origin}/course/tag/${id}`
          })
        }
      } else {
        if (this.isTest) {
          // 知识点测试
          this.$router.push({ path })
        } else {
          this.$router.push({ name: 'courseTagItem', params: { id } })
        }
      }
    }
  },
  beforeMount() {
    this.getCourseTagList()
  }
}
</script>
<style lang="scss" scoped>
.course-tag-wrapper {
  min-height: 100vh;
  background-color: #eee;
  box-sizing: border-box;
}
.course-tag-hd {
  position: sticky;
  top: 0;
  padding: 20px;
  background-color: #fff;
  z-index: 10;
}
.course-tag-bd {
  padding: 20px;
}
.course-tag-main {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.course-title {
  font-size: 15px;
  font-weight: bold;
  color: #222;
}
.tag-title {
  font-size: 15px;
  font-weight: bold;
  color: #222;
  line-height: 24px;
}
.tag-item {
  position: relative;
  padding: 20px 20px 20px 0;
  font-size: 15px;
  color: #222;
  line-height: 1.4;
  border-bottom: 1px solid #eee;
  &:last-child {
    border-bottom: 0;
  }
  ::v-deep .van-icon {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    color: #999;
  }
}
</style>
