<template>
  <div class="prizePreview1">
    <div class="prizeDiv">
      <div class="titleDiv">待领取</div>
      <div class="toReceiveIntegral">{{ toReceviceIntegralTotal }}积分</div>
      <div class="toReceiveBtn" @click="drawprize()" v-lz-click="'16'"><img src="//img10.360buyimg.com/imgzone/jfs/t1/156214/38/12241/10761/603eff9cE41423a06/8a870d1990bde1f4.png" /></div>
      <div class="getIntegralDiv">
        已获得
        <span>{{ receviceIntegralSuccTotal }}积分</span>
      </div>
    </div>
  </div>
</template>

<script>
import apis1 from '../../../util/apis1.js';
let { invitationList, immediatelyDrawGray, immediatelyDraw } = apis1;
import { PrizeResultPopComService, NoPrizeResultPopComService } from '../../../util/comCreate.js';

export default {
  props: ['datas', 'keyData', 'styleData', 'isTemplate','templateData'],
  components: {},
  data() {
    return {
      toReceviceIntegralTotal: 0, //待领取积分总量
      receviceIntegralSuccTotal: 0 //以领取积分总量
    };
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      var datasAll = this.templateData
      if (this.isTemplate) {
        this.toReceviceIntegralTotal = 0;
        this.receviceIntegralSuccTotal = 0;
      } else {
        await invitationList(invitationData => {

          var invitationPrizeList = invitationData.data.prizeInfo;
          this.invitationPrizeList = invitationPrizeList;
          immediatelyDrawGray(receiveListData => {
            var canPrizeList = receiveListData.data.receivePrizes;
            this.canPrizeList = canPrizeList;
            for (var i = 0; i < invitationPrizeList.length; i++) {
              for (var j = 0; j < canPrizeList.length; j++) {
                if (canPrizeList[j].prizeInfoId == invitationPrizeList[i].id) {
                  //0 可领取 1 已经领取成功 2 领取失败）
                  if (canPrizeList[j].status == 0 || canPrizeList[j].status == 2) {
                    this.toReceviceIntegralTotal = this.toReceviceIntegralTotal + invitationPrizeList[i].beanNum;
                  }
                  if (canPrizeList[j].status == 1) {
                    this.receviceIntegralSuccTotal = this.receviceIntegralSuccTotal + invitationPrizeList[i].beanNum;
                  }
                }
              }
            }
          });
        });
      }
    },
    //立即领取点击按钮
    async drawprize(itemData) {
      // console.log( this.isDisabled,' this.isDisabled = true')
      if (this.isTemplate) {
        this.$toast('活动预览，仅供查看')
        return;
      } else if (!this.isTemplate) {
        let actStatus = sessionStorage.getItem('actStatus');
        if (actStatus == 1) {
          this.$toast('活动尚未开始');
          return;
        } else if (actStatus == 2) {
          this.$toast('活动已结束');
          return;
        }
        if (this.toReceviceIntegralTotal == 0) {
          this.$toast('暂时没有可领取积分');
          return;
        }

        var index = 0;
        for (var i = 0; i < this.canPrizeList.length; i++) {
          if (this.canPrizeList[i].status != 1) {
            await immediatelyDraw(this.canPrizeList[i].prizeInfoId, receiveData => {
              if (receiveData.resp_code == 0) {
                if (receiveData.data.result == true) {
                  index = index + 1;
                }
              }
            });
          }
        }


        if (index > 0) {
          var obj = {
            datas: {},
            keyData: {
              prizeUrl: 'showImg',
              prizeName: 'prizeName',
              prizeType: 'prizeType',
              priceInfo: 'priceInfo',
              discountType: 'discountType',
              couponType: 'couponType'
            }
          };
          this.$toast('领取成功');
          // obj.datas = this.invitationPrizeList[0];
          // // console.log(obj, 'obj**********');
          // PrizeResultPopComService.create(obj, null, submit => {
          //   PrizeResultPopComService.close();
          // });
        } else {
          this.$toast('领取失败');
          // NoPrizeResultPopComService.create(
          //   '抱歉您与奖品擦肩而过~',
          //   '//img10.360buyimg.com/imgzone/jfs/t1/152859/24/15591/14697/6008f58fE1744005f/765fe66b5a48aefc.png',
          //   submit => {
          //     NoPrizeResultPopComService.close();
          //   }
          // );
        }

        this.initData();
      }
    }
  }
};
</script>

<style lang="scss" scoped="scoped">
.prizePreview1 {
  .prizeDiv {
    position: absolute;
    top: 9.73rem;
    background: url('//img10.360buyimg.com/imgzone/jfs/t1/162524/5/8988/13287/603effb6E1d77ec28/6383fca9e98d1c84.png') no-repeat;
    background-size: 100% 100%;
    width: 7.5rem;
    height: 3.56rem;
    .titleDiv {
      font-size: 0.44rem;
      color: #634b15;
      margin-top: 0.22rem;
    }
    .toReceiveIntegral {
      font-size: 0.72rem;
      color: #5f4914;
      line-height: 0.72rem;
      margin-bottom: 0.14rem;
    }
    .toReceiveBtn {
      img {
        width: 1.88rem;
        height: 0.52rem;
      }
    }
    .getIntegralDiv {
      color: #ffe7b4;
      font-size: 0.22rem;
      margin-top: 0.3rem;
      span {
        font-size: 0.28rem;
      }
    }
  }
}
</style>
