<template>
  <div class="course-tag-wrapper" v-show="loaded">
    <div class="course-tag-hd">
      <h1 class="tag-title">{{detail.title}}</h1>
    </div>
    <div class="course-tag-bd">
      <div class="course-tag-main">
        <div class="tag-content" v-html="html"></div>
      </div>
      <div class="tools">
        <ul>
          <li @click="toExamPage">知识点考试</li>
        </ul>
        <ul>
          <li @click="getCourseTag(detail.last)" v-if="detail.last">上一点</li>
          <li @click="getCourseTag(detail.next)" v-if="detail.next">下一点</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import * as api from '@/api/course.js'
export default {
  name: 'CourseTagItem',
  metaInfo() {
    return {
      title: '知识点速学'
    }
  },
  data() {
    return {
      loaded: false,
      detail: {}
    }
  },
  computed: {
    tagId() {
      return this.$route.params.id
    },
    html() {
      return this.detail.contents
        ? this.detail.contents.replace(/\n/g, '<br/>')
        : ''
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    // 获取知识点详情
    getCourseTag(tagId) {
      this.$toast.loading({
        duration: 0,
        message: '加载中...',
        forbidClick: true
      })
      this.loaded = false
      api
        .getCourseTag(tagId || this.tagId)
        .then(response => {
          this.loaded = true
          this.detail = response
        })
        .finally(() => {
          this.$toast.clear()
        })
    },
    // 去知识点考试页面
    toExamPage() {
      const path = `/exam/answer?type=2&tag_id=${this.detail.id}&papersUrl=zy/v2/examination/course-papers`
      if (this.isWeapp) {
        const src = encodeURIComponent(`${window.location.origin}${path}`)
        wx.miniProgram.navigateTo({
          url: `/pages/web/index?src=${src}`
        })
      } else {
        this.$router.push({ path })
      }
    }
  },
  beforeMount() {
    this.getCourseTag()
  }
}
</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;
}
.tag-title {
  font-size: 0.3rem;
  font-weight: bold;
  color: #222;
  line-height: 0.48rem;
}
.tag-content {
  padding: 20px 0;
  font-size: 13px;
  line-height: 24px;
  img {
    max-width: 100%;
  }
  b {
    font-weight: bold;
  }
}
.tools {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  li {
    display: inline-block;
    height: 24px;
    margin: 0 0 10px 10px;
    padding: 0 15px;
    font-size: 13px;
    color: #fff;
    line-height: 24px;
    background: #67a8ff;
    border-radius: 12px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    cursor: pointer;
    &:first-child {
      margin-left: 0;
    }
  }
}
</style>
