<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'
    }
  },
  methods: {
    // 获取知识点列表
    getCourseTagList() {
      this.$toast.loading({
        duration: 0,
        message: '加载中...',
        forbidClick: true
      })
      this.loaded = false
      api.getCourseTagList(this.courseId).then(response => {
        this.loaded = true
        this.detail = response
        this.$toast.clear()
      })
    },
    onClick({ id }) {
      if (this.isTest) {
        const query = {
          type: 2,
          tag_id: id,
          papersUrl: 'zy/v2/examination/course-papers'
        }
        this.$router.push({ path: '/exam/answer', query })
        return
      }
      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: 0.4rem;
  background-color: #fff;
  z-index: 10;
}
.course-tag-bd {
  padding: 0.4rem;
}
.course-tag-main {
  padding: 0.2rem;
  background-color: #fff;
  border-radius: 0.12rem;
}
.course-title {
  font-size: 0.3rem;
  color: #222;
}
.tag-title {
  font-size: 0.3rem;
  font-weight: bold;
  color: #222;
  line-height: 0.48rem;
}
.tag-item {
  position: relative;
  padding: 0.4rem 0.4rem 0.4rem 0;
  font-size: 0.3rem;
  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%);
  }
}
</style>
