<template>
  <div class="tag-message" :class="classes">
    <div class="tag-message-bd">
      <div class="tag-message-content" ref="content" v-html="html"></div>
    </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: 'TagMessage',
  props: {
    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() {
      return this.data.contents.replace(/\n/g, '<br/>')
    }
  },
  methods: {
    toggleMore() {
      this.showMore = !this.showMore
    }
  },
  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;
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
</style>
