<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)">{{item.title}}</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: 76,
      contentHeight: 0
    }
  },
  computed: {
    hasMore() {
      return this.contentHeight > this.maxHeight
    },
    classes() {
      return {
        'has-more': this.hasMore && !this.showMore
      }
    }
  },
  methods: {
    onClick(data) {
      if (this.isTest) {
        const query = {
          type: 2,
          tag_id: data.id,
          papersUrl: 'zy/v2/examination/course-papers'
        }
        this.$router.push({ path: '/exam/answer', query })
        return
      }
      this.$emit('change', data)
    },
    toggleMore() {
      this.showMore = !this.showMore
    },
    viewMore() {
      this.$router.push({
        name: 'courseTag',
        params: { courseId: this.courseId, chapterId: this.data.id }
      })
    }
  },
  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: 76px;
  overflow: hidden;
}
.course-tag-item {
  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;
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
</style>
