<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.children && data.children.length">
        <ul class="message-tag-list" ref="content">
          <template v-for="item in data.children">
            <li class="course-tag-item" :key="item.id" @click="$emit('search', item)">{{item.name}}</li>
          </template>
        </ul>
      </template>
    </div>
    <div class="more" @click="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>
</template>

<script>
export default {
  name: 'CourseTagMessage',
  props: {
    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: {
    toggleMore() {
      this.showMore = !this.showMore
    }
  },
  mounted() {
    this.contentHeight = this.$refs.content.offsetHeight
  }
}
</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>
