<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">
        <div class="inner">
          <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>
  </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() {
      if (this.detail.contents) {
        return this.detail.contents
          .replace(/---------- start ----------[\n]{0,1}/gi, '')
          .replace(/---------- end ----------/gi, '')
          .replace(/\n/g, '<br/>')
      }
      return ''
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    // 获取知识点详情
    getCourseTag(tagId) {
      this.loaded = false
      api.getCourseTag(tagId || this.tagId).then(response => {
        this.loaded = true
        this.detail = response
      })
    },
    // 去知识点考试页面
    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: 20px;
  background-color: #fff;
  z-index: 10;
}
.course-tag-bd {
  padding: 20px;
}
.course-tag-main {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.tag-title {
  font-size: 15px;
  font-weight: bold;
  color: #222;
  line-height: 24px;
}
.tag-content {
  padding: 10px 0;
  font-size: 13px;
  line-height: 22px;
  ::v-deep img {
    max-width: 100%;
  }
  ::v-deep b {
    font-weight: bold;
  }
}
.tools {
  margin-top: 5px;
  height: 50px;
  padding-bottom: env(safe-area-inset-bottom);
  .inner {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    padding: 0 20px;
    padding-bottom: env(safe-area-inset-bottom);
    background-color: #fff;
    box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.05);
  }
  ul {
    font-size: 0;
  }
  li {
    display: inline-block;
    height: 35px;
    margin-left: 10px;
    padding: 0 30px;
    font-size: 13px;
    color: #fff;
    line-height: 35px;
    background: #2b7ce9;
    border-radius: 6px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    cursor: pointer;
    &:first-child {
      margin-left: 0;
    }
  }
}
</style>
