<template>
  <div class="course-tag-message" :class="classes">
    <div class="course-tag-message-hd">
      <div class="course-tag-message__title">{{data.name}}</div>
    </div>
    <div class="course-tag-message-bd">
      <template v-if="data.tag && data.tag.length">
        <ul class="message-tag-list" ref="content">
          <template v-for="item in data.tag">
            <li class="course-tag-item" :key="item.id" @click="onClick(item)">
              <p>{{item.title}}</p>
            </li>
          </template>
        </ul>
      </template>
    </div>
    <div class="more" @click="viewMore" 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>
</template>

<script>
export default {
  name: 'CourseTagMessage',
  props: {
    courseId: { type: String },
    isTest: { type: Boolean, default: false },
    data: { type: Object }
  },
  data() {
    return {
      showMore: false,
      maxHeight: 102,
      contentHeight: 0
    }
  },
  computed: {
    hasMore() {
      return this.contentHeight > this.maxHeight
    },
    classes() {
      return {
        'has-more': this.hasMore && !this.showMore
      }
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    onClick(data) {
      if (this.isTest) {
        // 知识点测试
        const path = `/exam/answer?type=2&tag_id=${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 })
        }
      } else {
        this.$emit('change', data)
      }
    },
    toggleMore() {
      this.showMore = !this.showMore
    },
    viewMore() {
      const path = `/course/learn/${this.courseId}/tag/${
        this.data.id
      }?is_test=${this.isTest ? '1' : '0'}`
      if (this.isWeapp) {
        wx.miniProgram.navigateTo({
          url: `/pages/web/index?src=${window.location.origin}${path}`
        })
      } else {
        this.$router.push({ path })
      }
    }
  },
  mounted() {
    this.contentHeight = this.$refs.content
      ? this.$refs.content.offsetHeight
      : 0
  }
}
</script>

<style lang="scss" scoped>
.course-tag-message {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.course-tag-message-hd {
  padding: 10px 0;
  font-size: 15px;
  color: #222;
}
.has-more .course-tag-message-bd {
  height: 102px;
  overflow: hidden;
}
.course-tag-item {
  display: inline-block;
  max-width: 100%;
  height: 24px;
  margin: 0 10px 10px 0;
  padding: 0 15px;
  background: #67a8ff;
  border-radius: 12px;
  cursor: pointer;
  p {
    font-size: 13px;
    color: #fff;
    line-height: 24px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
</style>
