<template>
  <div>
    <div :class="showTotal ? 'total-introduce' : 'detailed-introduce'">
      <div ref="desc" class="intro-content" :title="introduce">
        <span v-if="introduce" class="merchant-desc">
          {{ introduce }}
        </span>
        <div v-if="showExchangeButton" class="unfold" @click="showTotalIntro">
          <p>{{ exchangeButton ? '展开' : '收起' }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Spread',
  props: {
    value: String
  },
  data() {
    return {
      introduce: '',
      // 是否展示所有文本内容
      showTotal: true,
      // 显示展开还是收起
      exchangeButton: true,
      // 是否显示展开收起按钮
      showExchangeButton: false,
      rem: ''
    };
  },
  watch: {
    introduce: function() {
      this.$nextTick(
        function() {
          // console.log("nextTick");
          // 判断介绍是否超过四行
          let rem = parseFloat(this.getRem());
          // console.log("watch 中的rem", rem);
          if (!this.$refs.desc) {
            // console.log("desc null");
            return;
          }
          let descHeight = window.getComputedStyle(this.$refs.desc).height.replace('px', '');
          // console.log("descHeight:" + descHeight);
          // console.log("如果 descHeight 超过" + 5.25 * rem + "就要显示展开按钮");
          if (descHeight > 7 * rem) {
            // 真不知道为啥是5.25
            // console.log("超过了四行");
            // 显示展开收起按钮
            this.showExchangeButton = true;
            this.exchangeButton = true;
            // 不是显示所有
            this.showTotal = false;
          } else {
            // 不显示展开收起按钮
            this.showExchangeButton = false;
            // 没有超过四行就显示所有
            this.showTotal = true;
            // console.log("showExchangeButton", this.showExchangeButton);
            // console.log("showTotal", this.showTotal);
          }
        }.bind(this)
      );
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    showTotalIntro() {
      this.showTotal = !this.showTotal;
      this.exchangeButton = !this.exchangeButton;
    },
    getRem() {
      const defaultRem = 13;
      let winWidth = window.innerWidth;
      let rem = (winWidth / 375) * defaultRem;
      return rem;
    },
    init() {
      this.introduce = this.value;
    }
  }
};
</script>

<style lang="less" scoped rel="stylesheet/less">
.top-prove {
  height: 100px;
  width: 100%;
  background: #dddddd;
  text-align: center;
  line-height: 100px;
}
.total-introduce {
  height: auto;
  overflow: hidden;
  font-size: 13px;
  color: #333;
  .intro-content {
    .merchant-desc {
      width: 100%;
      line-height: 18px;
    }
  }
  .unfold {
    display: block;
    z-index: 11;
    float: right;
    width: 30px;
    height: 18px;
    p {
      margin: 0;
      line-height: 18px;
      color: #5371e0;
      font-weight: bold;
    }
  }
}
.detailed-introduce {
  font-size: 13px;
  color: #333;
  position: relative;
  overflow: hidden;
  .intro-content {
    // 最大高度设为4倍的行间距
    max-height: 90px;
    line-height: 18px;
    word-wrap: break-word;
    /*强制打散字符*/
    word-break: break-all;
    background: #ffffff;
    /*同背景色*/
    color: #ffffff;
    overflow: hidden;
    .merchant-desc {
      width: 100%;
      line-height: 18px;
    }
    &:after,
      // 这是展开前实际显示的内容
      &:before {
      content: attr(title);
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      color: #333;
      // overflow: hidden;
    }
    // 把最后最后一行自身的上面三行遮住
    &:before {
      display: block;
      overflow: hidden;
      z-index: 1;
      max-height: 70px;
      background: #ffffff;
    }
    &:after {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      height: 87px;
      /*截取行数*/
      -webkit-line-clamp: 5;
      text-overflow: ellipsis;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      /*行首缩进字符数，值为[(截取行数-1)*尾部留空字符数]*/
      text-indent: -85px;
      /*尾部留空字符数*/
      padding-right: 30px;
    }
    .unfold {
      z-index: 11;
      width: 30px;
      height: 18px;
      outline: 0;
      position: absolute;
      right: 0;
      bottom: 0;
      p {
        margin: 0;
        line-height: 18px;
        color: #5371e0;
        font-weight: bold;
      }
    }
  }
}
</style>
