<template>
  <div class="reward" @click="_toggleReward(true)">
    <div class="btn">
      <div class="icon"></div>
    </div>
    <div class="title">打赏</div>
    <div class="reward-box" v-transfer-dom v-show="isShow" @click.stop>
      <div class="tab-box" v-if="showTab">
        <div :class="{ 'z-active': tabIndex == 0 }" @click="_tabIndex(0)">
          礼物打赏
        </div>
        <div :class="{ 'z-active': tabIndex == 1 }" @click="_tabIndex(1)">
          现金打赏
        </div>
      </div>
      <div class="type-box">
        <keep-alive>
          <rewardGift v-if="isShow && showRewardGift" />
          <rewardCash v-if="isShow && showReward" />
        </keep-alive>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
const rewardCash = resolve => require(['./rewardCash'], resolve);
const rewardGift = resolve => require(['./rewardGift'], resolve);

export default {
  name: 'suspension-reward',
  components: {
    rewardCash,
    rewardGift
  },
  data() {
    return {
      isShow: false,
      tabIndex: 0
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      userInfo: 'userInfo'
    }),
    showTab() {
      return this.channelInfo.rewardGiftStatus === 1 && this.channelInfo.rewardStatus === 1;
    },
    showRewardGift() {
      if (this.showTab && this.channelInfo.rewardGiftStatus === 1 && this.tabIndex === 0) {
        return true;
      } else if (!this.showTab && this.channelInfo.rewardGiftStatus === 1) {
        return true;
      } else {
        return false;
      }
    },
    showReward() {
      if (this.showTab && this.channelInfo.rewardStatus === 1 && this.tabIndex === 1) {
        return true;
      } else if (!this.showTab && this.channelInfo.rewardStatus === 1) {
        return true;
      } else {
        return false;
      }
    }
  },
  mounted() {
    this.$Bus.$on('bus-showReward', this._toggleReward);
  },
  methods: {
    _toggleReward(bol) {
      if (!this.userInfo.id && bol) {
        this.$layer.msg('请先登录');
        this.$Bus.$emit('bus-showLogin', true);
        return false;
      }
      this.isShow = bol;
    },
    _tabIndex(index) {
      this.tabIndex = index;
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-showReward', this._toggleReward);
  }
};
</script>
<style lang="less" scoped>
.reward {
  width: 0.8rem;
  height: 0.8rem;
  .btn {
    position: relative;
    padding: 0 0.1rem;
    width: 0.8rem;
    height: 0.6rem;
    background: var(--suspension-reward-bg) no-repeat center center;
    background-size: contain;
    .icon {
      width: 100%;
      height: 100%;
      background: url(./img/icon-reward.png) no-repeat center center;
      background-size: auto 60%;
    }
  }
  .title {
    margin-top: 0.02rem;
    line-height: 0.18rem;
    text-align: center;
    white-space: nowrap;
    font-size: 0.16rem;
    color: #666;
  }
}
.reward-box {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 1000;
  width: 100%;
  background-color: #ffffff;
  .tab-box {
    padding: 0.1rem;
    div {
      display: inline-block;
      margin-right: 0.1rem;
      vertical-align: top;
      font-size: 0.12rem;
      color: #333;
      &.z-active {
        color: var(--main-color);
        &:after {
          content: '';
          margin: 0.05rem auto 0;
          display: block;
          height: 0.02rem;
          width: 0.3rem;
          background-color: var(--main-color);
        }
      }
    }
  }
}
</style>
