<template>
  <div>
    <div class="countDown-list" @click="_showRed(countDownInfo.id)" v-show="countDownInfo.count && countDownInfo.count !== 0"> <!-- v-show="countDownInfo.count !== 0" -->
      <div class="receive">
        <div class="head" v-if="times > 0" :style="{backgroundImage: `url(${countDownInfo.userHeadImg || defaultHead})`}"></div>
        <div class="time" v-if="times > 0">{{ times | formatTime('hh:mm:ss') }}</div>
        <div class="count">{{countDownInfo.count || 0}}</div>
      </div>
    </div>
    <transition name="fade">
       <div class="countDown" v-show="showRedInfo.type == 16 && showRedInfo.id && showRed" @click.stop >
         <div class="box">
          <i class="close" @click="_closeRed"></i>
          <div class="logo" :style="{backgroundImage: `url(${countDownInfo.userHeadImg || defaultHead})`}"></div>
          <luck-list v-if="state === 3" :luckInfo="luckInfo" :channelInfo="channelInfo"></luck-list>
          <div class="red-info" v-if="state !== 3">
            <div class="content-box">
               <div class="name" v-if="state === 0">{{ countDownInfo.sponsor || channelInfo.chatAdminName || '管理员' }}</div>
               <div class="sum" v-if="state === 0">红包池：{{ countDownInfo.amount }}元</div>
               <div class="open" @click="_openRed" v-if="state === 0">
                 <span v-if="times > 0">
                    {{ times | formatTime('hh:mm:ss') }}
                    <div>后可打开</div>
                 </span>
                  <span v-else style="font-size: 20px">
                    抢
                  </span>
               </div>
               <div class="get" v-if="state === 1">
                 <div class="congratulation">恭喜您抢到</div>
                 <div class="money"><span>{{ redResult }}</span>元红包</div>
                 <div class="tips">红包已放入<span>个人中心-钱包</span>，记得提现哦</div>
               </div>
               <div class="miss" v-if="state === 2">
                 <div class="congratulation">红包与你擦肩而过</div>
                 <div class="tips">下次好运哦</div>
               </div>
               <div class="luck" v-if="state !== 0" @click="_getLuck">
                 <span>看看大家手气</span>
               </div>
            </div>
          </div>
         </div>
      </div>
    </transition>
  </div>
</template>

<script>
const RED_TYPE = 16;
import { mapGetters, mapMutations } from 'vuex';
import { robRedpacket, openRedRainpacket, countDownPacketDetail } from '@/plugins/API/apiRed';
import sha1 from 'crypto-js/sha1';
import luckList from './luckList';
export default {
  name: 'red-rain-box',
   components: {
    luckList
  },
  props: {
    countDownInfo: {}
  },
  data() {
    return {
      isAjax: true,
      defaultHead: require(`@/assets/img/default-head.jpg`),
      state: 0,
      times: 0,
      redResult: 0,
      luckInfo: {}
    }
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      userInfo: 'userInfo',
      showRed: 'showRed',
      showRedInfo: 'showRedInfo'
    }),
  },
  methods: {
     ...mapMutations({
      set_showRed: 'set_showRed',
      set_showRedInfo: 'set_showRedInfo'
    }),
    async _robRedpacket() {
      if (this.times > 0) {
        return false
      }
      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.countDownInfo.svSalt = res.data.svSalt || '';
        return res
      } else {
        res.errorMessage && this.$layer.msg(res.errorMessage);
        return false
      }
    },
     async _openRed() {
      if (this.times > 0) {
        return false
      }
      const rob = await this._robRedpacket()
      if (!rob) {
        return
      }
      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.countDownInfo.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
          this.state = 1
          this.redResult = res.data
          return
         } else {
           this.state = 2
         }
       }
       this.state = 2
    },
    timeCountDown() {
      clearInterval(this.timer)
      this.timer = setInterval( () => {
        this.times--
        if (this.times === 0){
          clearInterval(this.timer)
        }
      },1000)
    },
    async _getLuck() {
      if (!this.isAjax) {
        return false;
      }
      this.isAjax = false;
      const res = await countDownPacketDetail({
        id: this.countDownInfo.id,
        uin: this.channelInfo.uin
      });
      this.isAjax = true;
      if (res.code === 200 && res.errorCode === 0) {
        this.state = 3;
        this.luckInfo = res.data;
      }
    },
    _showRed(id) {
      if (this.userInfo.id) {
        this.set_showRedInfo({
          type: RED_TYPE,
          id: id || this.countDownInfo.id || 1
        });
      } else {
        this.$layer.msg('登陆后才能领取红包哦!~');
        this.$Bus.$emit('bus-showLogin', true);
      }
    },
    _closeRed() {
      this.state = 0
      this.set_showRed(false);
      this.$Bus.$emit('bus-getCountDown');
    }
  },
  watch: {
    countDownInfo(newVal) {
      const {id, countDown} = newVal
      if (id) {
        // eslint-disable-next-line radix
        const times = parseInt(countDown) - (~~((new Date()).getTime() / 1000))
        this.times = times > 0 ? times : 0
        this.timeCountDown()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.countDown-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 {
    text-align: center;
    font-size: 0.1rem;
    .head {
      position: absolute;
      top: calc(50% - .075rem);
      left: calc(50% - .075rem);
      width: .15rem;
      height: .15rem;
      background: #fff;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center center;
      border-radius: 50%;
    }
    .time {
      position: absolute;
      bottom: -0.01rem;
      left: -15%;
      width: 125%;
      transform: scale(0.8);
      background: rgba(0, 0, 0, 0.4);
      color: #fff;
      padding: 0 .01rem;
      border-radius: .2rem;
      font-weight: 300;
      text-align: center;
    }
    .count {
      position: absolute;
      right: -.02rem;
      top: -.02rem;
      width: .15rem;
      height: .15rem;
      background: #FFE45C;
      border-radius: 50%;
      line-height: .15rem;
      text-align: center;
      color:#FD2E41;
      transform: scale(0.85);
    }
  }
}
.countDown {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 500;
  background-color: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    position: absolute;
    z-index: 100;
    margin: auto;
    width: 3.75rem;
    height: 3.5rem;
    .close {
      position: absolute;
      right: 0.5rem;
      top: -0.4rem;
      z-index: 2;
      width: 0.32rem;
      height: 0.32rem;
      background: url('./img/icon-close.png') no-repeat center center;
      background-size: 100% 100%;
    }
    .logo {
      position: absolute;
      top: .59rem;
      left: calc(50% - .35rem);
      height: .7rem;
      width: .7rem;
      border-radius: 50%;
      background: #fff;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center center;
      z-index: 111;
    }
    .red-info {
      padding: 1.5rem 0.5rem 0;
      position: relative;
      width: 100%;
      height: 100%;
      background: url('./img/red-bg.png') no-repeat 0 0;
      background-size: 100% 100%;
      text-align: center;
      .content-box {
        padding-top: 0.1rem;
        margin-bottom: 0.2rem;
        font-size: 0.16rem;
        font-weight: 400;
        .name {
          color: #fff;
        }
        .sum {
          color: #FEB90E;
          margin: .15rem;
        }
        .open {
          width: .8rem;
          height: .8rem;
          background: #FEBE5C;
          box-shadow: 0px 3px 6px #D42104;
          border-radius: 50%;
          margin-left: calc(50% - .4rem);
          color: #DD424B;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          font-weight: 500;
          line-height: .2rem;
        }
        .luck {
          width: 1.4rem;
          height: .3rem;
          margin-left: calc(50% - .7rem);
          background: #FEBE5C;
          box-shadow: 0px 3px 6px #D42104;
          color: #DD424B;
          line-height: .3rem;
          opacity: 1;
          border-radius: 40px;
          font-size: .14rem;
          font-weight: 500;
        }
        .miss {
          color: #fff;
          .congratulation {
            
          }
          .tips {
            font-size: .12rem;
            margin: .15rem 0;
          }
        }
        .get {
          color: #fff;
          .congratulation {
            
          }
          .money {
            color: #F5E952;
            margin: .15rem 0;
            span {
              font-size: .25rem;
              margin-right: .02rem;
            }
          }
          .tips {
            font-size: .12rem;
            margin-bottom: .15rem;
            span {
              color: #F5E952;
            }
          }
        }
      }
    }
  }
}
</style>
