<template>
  <div>
    <div class="tag-message" :class="classes" @click="showMore = true">
      <div class="tag-message-bd">
        <div class="tag-message-content" ref="content" v-html="html"></div>
      </div>
      <div class="more" @click.stop="toggleMore" v-if="hasMore">
        <template v-if="!showMore">
          <span>更多</span>
          <van-icon name="arrow-down"></van-icon>
        </template>
        <template v-else>
          <span>收起</span>
          <van-icon name="arrow-up"></van-icon>
        </template>
      </div>
    </div>
    <div class="tools">
      <ul>
        <li @click="toExamPage">知识点考试</li>
        <li @click="toCourseVideo" v-if="data.has_video">相关视频</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TagMessage',
  props: {
    courseId: { type: String },
    data: { type: Object }
  },
  data() {
    return {
      showMore: false,
      maxHeight: 90,
      contentHeight: 0
    }
  },
  computed: {
    hasMore() {
      return this.contentHeight > this.maxHeight
    },
    classes() {
      return {
        'has-more': this.hasMore && !this.showMore
      }
    },
    html() {
      let contents = this.data.contents || ''
      const hasHtmlTag = contents.includes('</p>')
      contents = contents
        .replace(/---------- start ----------[\n]{0,1}/gi, '')
        .replace(/---------- end ----------/gi, '')
      return hasHtmlTag ? contents : contents.replace(/\n/g, '<br/>')
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    toggleMore() {
      this.showMore = !this.showMore
    },
    viewMore() {
      if (this.isWeapp) {
        wx.miniProgram.navigateTo({
          url: `/pages/web/index?src=${window.location.origin}/course/tag/${this.data.id}`
        })
      } else {
        this.$router.push({
          name: 'courseTagItem',
          params: { id: this.data.id }
        })
      }
    },
    // 去知识点考试页面
    toExamPage() {
      const path = `/exam/answer?type=2&tag_id=${this.data.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 })
      }
    },
    // 去课程视频页面
    toCourseVideo() {
      if (this.isWeapp) {
        wx.miniProgram.navigateTo({
          url: `/pages/course/item?id=${this.data.course_id}&chapter_id=${this.data.section_id}`
        })
      } else {
        window.alert('请在微信小程序中打开')
      }
    }
  },
  mounted() {
    this.contentHeight = this.$refs.content.offsetHeight
  }
}
</script>

<style lang="scss" scoped>
.tag-message {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.has-more .tag-message-bd {
  height: 90px;
  overflow: hidden;
}
.tag-message-content {
  font-size: 13px;
  line-height: 30px;
  ::v-deep img {
    max-width: 100%;
  }
  ::v-deep b {
    font-weight: bold;
  }
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
.tools {
  margin-top: 5px;
  li {
    display: inline-block;
    height: 24px;
    margin: 0 10px 10px 0;
    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;
  }
}
</style>
