<template>
  <div class="praise" @click="_clickPraise">
    <div class="btn">
      <div class="icon"></div>
      <div id="praisBox" ref="praisBox"></div>
    </div>
    <div class="title">{{ praiseTotal | formatNum }}</div>
  </div>
</template>

<script>
import { getpraisenum, sendPraise } from '@/plugins/API/apiAll';
import { mapGetters } from 'vuex';
export default {
  name: 'suspension-praise',
  data() {
    return {
      praiseTotal: 0,
      praiseNum: 0,
      tiemr: null
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo'
    })
  },
  mounted() {
    this.$Bus.$on('bus-praiseNum', this._addPraise);
    // 获取点赞数
    this._getpraisenum();
  },
  methods: {
    async _getpraisenum() {
      const res = await getpraisenum({
        id: this.channelInfo.id
      });
      if (res.code === 200 && res.errorCode === 0) {
        const praiseNum = Number(res.data.praiseNum) || 0;
        const customPraiseNum = Number(res.data.customPraiseNum) || 0;
        this.praiseTotal = praiseNum + customPraiseNum;
      }
    },
    _clickPraise() {
      this.praiseTotal++;
      this.praiseNum++;
      this.add(2);
      clearTimeout(this.tiemr);
      this.tiemr = setTimeout(() => {
        this._sendPraise();
      }, 1000);
    },
    async _sendPraise() {
      const res = await sendPraise({
        id: this.channelInfo.id,
        uin: this.channelInfo.uin,
        num: this.praiseNum
      });
      if (res.code === 200 && res.errorCode === 0) {
        setTimeout(() => {
          this.praiseNum = 0;
        }, 1000);
      }
    },
    _addPraise(num) {
      if (this.praiseNum !== Number(num)) {
        this.praiseTotal += Number(num);
      }
      if (num > 10) {
        this.add(5);
      } else if (num > 0) {
        this.add(2);
      }
    },
    add(num) {
      for (let i = 0; i < num; i++) {
        this._add();
      }
    },
    _add(type) {
      const div = document.createElement('div');
      const a = Math.floor(Math.random() * 7);
      div.style.backgroundPosition = `-${a * 0.6}rem 0`;
      div.style.transform = `matrix(1, 0, 0, 1, 0, 0)`;
      this.$refs.praisBox.appendChild(div);
      this._animation(div);
    },
    _animation(e) {
      e.animate(
        [
          { transform: `matrix(1, 0, 0, 1, 0, 0)`, opacity: 1 },
          {
            transform: `matrix(1, 0, 0, 1, ${Math.random() * 50 - 20}, ${Math.random() * 50 -
              150})`,
            opacity: 1
          },
          {
            transform: `matrix(1, 0, 0, 1, ${Math.random() * 50 - 20}, ${Math.random() * 50 -
              300})`,
            opacity: 0.7
          },
          {
            transform: `matrix(1, 0, 0, 1, ${Math.random() * 50 - 20}, ${Math.random() * 50 -
              350})`,
            opacity: 0
          }
        ],
        1500
      );
      setTimeout(() => {
        e.parentNode.removeChild(e);
      }, 1500);
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-praiseNum', this._addPraise);
  }
};
</script>
<style lang="less" scoped>
.praise {
  position: relative;
  width: 0.8rem;
  height: 0.6rem;
  .btn {
    position: relative;
    padding: 0 0.1rem;
    width: 0.8rem;
    height: 0.6rem;
    background: var(--suspension-praise-bg) no-repeat center center;
    background-size: contain;
    .icon {
      width: 100%;
      height: 100%;
      background: url(./img/icon-praise.png) no-repeat center center;
      background-size: auto 60%;
    }
  }
  .title {
    position: absolute;
    left: 0;
    right: 0;
    bottom: -0.2rem;
    line-height: 0.2rem;
    text-align: center;
    white-space: nowrap;
    font-size: 0.16rem;
    color: #666;
  }
  #praisBox {
    position: absolute;
    top: -0.6rem;
    left: -0.6rem;
    ::v-deep div {
      position: absolute;
      top: 0;
      left: 0;
      width: 0.6rem;
      height: 0.6rem;
      background: var(--icon-praise-list) no-repeat 0 0;
      background-size: auto 0.6rem;
      background-position: 0 0;
      opacity: 0;
    }
  }
}
</style>
