<template>
  <div class="top">
    <div class="left">
      <div class="box" v-show="!isClose">
        <div class="head">
          <img :src="channelInfo.chatAdminAvaUrl" alt="" />
        </div>
        <div class="info">
          <div class="name">
            {{ channelInfo.chatAdminName || '管理员' }}
          </div>
          <div class="watch-num" v-if="channelInfo.isDisplayWatchNum === 1">
            {{ allWatchNum | formatNum }}观看
          </div>
        </div>
        <div class="follow" v-if="wechatInfo.status" @click="_showFollow">关注</div>
        <transition name="fade">
          <follow-box
            v-transfer-dom
            v-if="wechatInfo.status"
            v-show="wechatInfo.isShow"
            v-model="wechatInfo"
          ></follow-box>
        </transition>
      </div>
    </div>
    <div class="right">
      <div class="invite" v-if="isShowList" v-show="!isClose" @click="showInvite = true">
        榜单
      </div>
      <transition name="move-bottom">
        <div class="invite-mask" v-transfer-dom v-if="isShowList" v-show="showInvite">
          <invite-box v-model="showInvite"></invite-box>
        </div>
      </transition>
      <div class="close" @click="isClose = !isClose">
        {{ isClose ? '显示' : '清屏' }}
      </div>
    </div>
  </div>
</template>

<script>
import { getWatchNum, getWechatInfo } from '@/plugins/API/apiAll';
import { mapGetters } from 'vuex';
const followBox = resolve => require(['@/components/modules-vertical/followBox'], resolve);
const inviteBox = resolve => require(['@/components/modules-vertical/inviteBox'], resolve);

export default {
  name: 'vertical-top-box',
  components: {
    followBox,
    inviteBox
  },
  props: {
    value: {}
  },
  data() {
    return {
      watchNum: {
        custom: 0,
        real: 0
      },
      wechatInfo: {},
      showInvite: false
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      channelConfig: 'channelConfig'
    }),
    isClose: {
      get() {
        return this.value;
      },
      set(nVal) {
        this.$emit('input', nVal);
      }
    },
    isShowList() {
      const info = this.channelConfig.invitation || {};
      return info.isShowList === 1;
    },
    allWatchNum() {
      return this.watchNum.custom + this.watchNum.real;
    }
  },
  mounted() {
    this.getWatchNum();
    this.getWechatInfo();
    this.$Bus.$on('bus-realWatchNum', this.changeWatchNum);
    this.$Bus.$on('bus-customWatchNum', this.changeWatchNumCustom);
  },
  methods: {
    async getWatchNum() {
      const res = await getWatchNum({
        id: this.channelInfo.id,
        uin: this.channelInfo.uin
      });
      if (res.code === 200 && res.errorCode === 0) {
        const { customWatchNum, watchNum } = res.data;
        this.watchNum = {
          custom: Number(customWatchNum) || 0,
          real: Number(watchNum) || 0
        };
      }
    },
    changeWatchNum(num) {
      if (num) {
        this.watchNum.real = Number(num) || 0;
      }
    },
    changeWatchNumCustom(num) {
      if (num) {
        this.watchNum.custom += Number(num) || 0;
      }
    },
    async getWechatInfo() {
      const res = await getWechatInfo({
        id: this.channelInfo.id
      });
      if (res.code === 200 && res.errorCode === 0) {
        this.wechatInfo = res.data || {};
      }
    },
    _showFollow() {
      this.wechatInfo.isShow = true;
    },
    _showInvite() {
      this.showInvite = true;
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-customWatchNum', this.changeWatchNumCustom);
    this.$Bus.$off('bus-realWatchNum', this.changeWatchNum);
  }
};
</script>

<style lang="less" scoped>
.top {
  padding: 0 0.1rem;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left {
    flex: 0 0 auto;
    .box {
      display: flex;
      align-items: center;
      padding: 0 0.03rem 0 0.01rem;
      height: 0.32rem;
      border-radius: 0.2rem;
      background-color: rgba(0, 0, 0, 0.29);
      font-size: 0;
      .head {
        width: 0.3rem;
        height: 0.3rem;
        border-radius: 100%;
        overflow: hidden;
        img {
          display: block;
          width: 100%;
          height: 100%;
        }
      }
      .info {
        padding: 0 0.05rem;
        max-width: 1rem;
        min-width: 0.5rem;
        color: #fff;
        .name {
          height: 0.18rem;
          line-height: 0.18rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-size: 0.11rem;
        }
        .watch-num {
          height: 0.12rem;
          line-height: 0.12rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-size: 0.09rem;
        }
      }
      .follow {
        width: 0.35rem;
        height: 0.26rem;
        line-height: 0.26rem;
        border-radius: 0.2rem;
        text-align: center;
        background-color: #fff;
        color: #409eff;
        font-size: 0.11rem;
      }
    }
  }
  .right {
    flex: 0 0 auto;
    .invite {
      margin-right: 0.06rem;
      display: inline-block;
      width: 0.48rem;
      height: 0.32rem;
      line-height: 0.32rem;
      border-radius: 0.2rem;
      background-color: rgba(0, 0, 0, 0.29);
      text-align: center;
      font-size: 0.11rem;
      color: #fff;
      cursor: pointer;
    }
    .close {
      display: inline-block;
      width: 0.48rem;
      height: 0.32rem;
      line-height: 0.32rem;
      border-radius: 0.2rem;
      background-color: rgba(0, 0, 0, 0.29);
      text-align: center;
      font-size: 0.11rem;
      color: #fff;
      cursor: pointer;
    }
  }
}
.invite-mask {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  height: 60%;
  min-height: 3rem;
  border-radius: 0.1rem 0.1rem 0px 0px;
  overflow: hidden;
}
</style>
