<template>
  <div>
    <div class="rain-list" v-show="!isGet && rainInfo.id"> <!-- v-show="!isGet && rainInfo.id" -->
      <div class="receive" @click="_showRed()">
        <div class="name">红包雨</div>
      </div>
    </div>
    <transition name="fade">
      <div v-show="showRedInfo.type == 15 && showRedInfo.id && showRed">
        <div class="rain" >
          <div class="rain-box" >
            <i class="close" @click="_closeRed"></i>
            <div class="info">
                <div class="greeting">红包雨来袭</div>
                <div class="big">快来戳!</div>
            </div>
            <div class="btn" @click="playGame">立即抢红包</div>
          </div>
        </div>
      </div>
    </transition>
    <transition name="fade">
      <div class="rainGame" v-if="showGame" ref="rainGame" id="rainGame">
        <!-- 游戏区域 -->
      </div>
    </transition>
  </div>
</template>

<script>
const RED_TYPE = 15;
import { mapGetters, mapMutations } from 'vuex';
import { openRedRainpacket, checkRainIsOpen, robRedpacket } from '@/plugins/API/apiRed';
import sha1 from 'crypto-js/sha1';
export default {
  name: 'red-rain-box',
  props: {
    rainInfo: {}
  },
  data() {
    return {
      showGame: false,
      isAjax: true,
      isGet: false
    }
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      userInfo: 'userInfo',
      showRed: 'showRed',
      showRedInfo: 'showRedInfo'
    }),
  },
  mounted() {
    this.$nextTick(() => {
      this.checkRainIsOpen()
    })
    this.$Bus.$on('bus-showRainStart', this.showRainStart)
  },
  methods: {
    ...mapMutations({
      set_showRed: 'set_showRed',
      set_showRedInfo: 'set_showRedInfo'
    }),
    showRainStart() {
      this.isGet = false
      this.set_showRedInfo({
        type: RED_TYPE,
        id: this.rainInfo.id
      })
    },
    async _robRedpacket() {
      if (!this.isAjax) {
        return false;
      }
      this.isAjax = false;
      const parm = {
        id: this.showRedInfo.id,
        uin: this.channelInfo.uin,
        channelId: this.channelInfo.id,
        sv: this.$md5(`${this.channelInfo.uin}_${this.userInfo.id}_${this.$redSV.rob}`)
      }
      const res = await robRedpacket(parm);
      this.isAjax = true;
      if (res.code === 200 && res.errorCode === 0) {
        this.rainInfo.svSalt = res.data.svSalt || '';
        return res
      } else {
        res.errorMessage && this.$layer.msg(res.errorMessage);
        return false
      }
    },
    async checkRainIsOpen() {
      if (this.userInfo.id && this.rainInfo.id){
        this.isGet = true
        const res = await checkRainIsOpen({
          uin: this.channelInfo.uin,
          id: this.rainInfo.id
        });
        if (res.code === 200 && res.errorCode === 0) {
          this.isGet = res.data
        }
      }
    },
    async _openRed() {
      if (!this.isAjax) {
        return '红包不存在';
      }
      this.isAjax = false;
      const id = this.showRedInfo.id
      const uin = this.channelInfo.uin
      const channelId = this.channelInfo.id
      const timestamp = ~~(new Date().getTime() / 1000)
      const sv = this.$md5(`${this.channelInfo.uin}_${this.userInfo.id}_${this.$redSV.open}_${this.showRedInfo.id}_${this.rainInfo.svSalt || ''}`)
      const sign = sha1(channelId + '' + id + '' + sv + '' + timestamp + '' + uin + 'zf33f4248819bf94e73wx937fecfe9b8').toString()
      const res = await openRedRainpacket({
        id,
        uin,
        channelId,
        sv,
        timestamp,
        sign
      });
       this.isAjax = true;
       if (res.code === 200) {
         if (res.errorCode === 0) {
          this.isGet = true
          return res.data
         } else {
           return res.errorMessage || '未抢到'
         }
       }
       return '未抢到'
    },
    async playGame() {
      if (this.userInfo.id) {
        const rob = await this._robRedpacket()
        if (!rob) {
          return
        }
        const rainGame = require('./game')
        this.showGame = !this.showGame
        this.$nextTick(() => {
          rainGame(this.$refs.rainGame, this.rainInfo.rainTime || 20, () => {
            this.showGame = !this.showGame
            this.set_showRed(false)
          }, this._openRed)
       })
      } else {
        this.$layer.msg('登陆后才能领取红包哦!~');
        this.$Bus.$emit('bus-showLogin', true);
      }
    },
    _showRed(id) {
      if (this.userInfo.id) {
        this.set_showRedInfo({
          type: RED_TYPE,
          id: id || this.rainInfo.id || 1
        });
      } else {
        this.$layer.msg('登陆后才能领取红包哦!~');
        this.$Bus.$emit('bus-showLogin', true);
      }
    },
    _closeRed() {
      this.set_showRed(false);
    }
  },
  watch: {
    rainInfo() {
      this.checkRainIsOpen()
    }
  }
};
</script>

<style lang="less" scoped>
.rain-list {
  position: relative;
  display: block;
  width: 0.4rem;
  height: 0.4rem;
  margin-bottom: 0.05rem;
  background: url('./img/red-small.png');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 100% 100%;
  .receive {
    position: absolute;
    bottom: 0;
    left: -0.1rem;
    display: block;
    height: 0.25rem;
    line-height: 0.25rem;
    width: 0.6rem;
    background-size: auto 100%;
    text-align: center;
    font-size: 0.1rem;
    .name {
      color: #fff;
      transform: scale(0.6) rotate(30deg);
      margin-right: .125rem;
      margin-top: .01rem;
    }
  }
}
.rainGame {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  background-color: rgba(0, 0, 0, 0.3);
  .close {
    z-index: 1000;
  }
}

.rain {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 500;
  background-color: rgba(0, 0, 0, 0.3);

  .rain-box {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    margin: auto;
    width: 2.75rem;
    height: 4.625rem;
    .close {
      position: absolute;
      left: calc(50% - 0.16rem);
      bottom: -0.1rem;
      z-index: 2;
      width: 0.32rem;
      height: 0.32rem;
      background: url('./img/icon-close.png') no-repeat center center;
      background-size: 100% 100%;
    }
    .info {
      position: relative;
      width: 100%;
      height: 3.875rem;
      background: url('./img/red-bg.png') no-repeat 0 0;
      background-size: 100% 100%;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .greeting,
      .big {
         margin-top: 1.3rem;
         z-index: 2;
         font-size: .3rem;
         font-family: Regular35;
         font-weight: 400;
         font-style: italic;
         color: #FFFFFF;
         line-height: 47px;
         background: linear-gradient(180deg, #FFC788 0%, #FFFAEF 100%);
         background-clip: text;
         -webkit-text-fill-color: transparent;
      }
      .big {
           margin-top: 0;
           font-size: .36rem;
           text-align: center;
      }
    }
    .btn {
      position: absolute;
      left: calc(50% - .8rem);
      bottom: 0.8rem;
      z-index: 2;
      width: 1.6rem;
      height: .35rem;
      background: linear-gradient(180deg, #FFEDB0, #FF9D29);
      border-radius: 41px;
      text-align: center;
      line-height: .35rem;
      font-weight: normal;
      font-style: italic;
      color: #CF0B00;
    }
  }
}
</style>
