<template>
  <div class="couponMarkDiv">
    <div :style="styleData.couponMark" v-for="(item, index) in taskCouponList" :key="index">
      <div v-if="item.discountType == 2" :style="styleData.discountType">
        <div :style="styleData.priceInfo1">{{ item.priceInfo }}</div>
        <div :style="styleData.priceInfoImg1"></div>
      </div>

      <div v-if="item.discountType == 1" :style="styleData.discountType">
        <div :style="styleData.priceInfoImg"></div>
        <div :style="styleData.priceInfo">{{ item.priceInfo }}</div>
      </div>

      <div :style="styleData.couponQuota">
        <span v-if="item.couponQuota">满{{ item.couponQuota }}元使用</span>
        <span v-else>无使用限制</span>
      </div>
      <div :style="styleData.remainNum">剩余：{{ item.remainNum }}份</div>
      <div :style="styleData.beginTime">起：{{ item.beginTime }}</div>
      <div :style="styleData.endTime">止：{{ item.endTime }}</div>
      <!-- 领取状态 0, 未领取， 1 已领取，2，领取失败, 4 未开始, 5, 已结束 -->
      <div :style="styleData.goLingqu" v-if="item.remainNum > 0 && (item.receive == 0 || item.receive == 4 || item.receive == 5)" @click="drawFun(item)"></div>
      <div :style="styleData.lingquOk" v-if="item.receive == 1"></div>
      <div :style="styleData.lingquNo" v-if="item.receive == 2"></div>
      <div :style="styleData.leftNumNull" v-if="item.remainNum == 0 && item.receive == 0" @click="drawFun(item)"></div>
    </div>
  </div>
</template>

<script>
import apis1 from '../../../util/apis1.js';
let { couponMarketingList, immediatelyDraw3 } = apis1;
import { PrizeResultPopComService, NoPrizeResultPopComService } from '../../../util/hdzxtcomCreate.js';
import dateUtil from '../../../util/dateUtil.js';
export default {
  props: ['isTemplate', 'datas', 'keyData', 'styleData', 'templateData'],
  data() {
    return {
      taskCouponList: [],
      preventClick: false
    };
  },

  created() {
    this.initData();
  },
  mounted() {},
  methods: {
    initData() {
      if (this.isTemplate == true) {
        let dataAll = this.templateData;
        if(dataAll){
          console.log(dataAll,'dataAll')
          for(var i = 0;i<dataAll.draw.prizeList.length;i++){
            dataAll.draw.prizeList[i].receive = 0;
            dataAll.draw.prizeList[i].remainNum = dataAll.draw.prizeList[i].totalPrizeNum;
            dataAll.draw.prizeList[i].beginTime = dateUtil.formatTime( dataAll.draw.prizeList[i].beginTime,null);
            dataAll.draw.prizeList[i].endTime = dateUtil.formatTime( dataAll.draw.prizeList[i].endTime,null);
            if(dataAll.draw.prizeList[i].fullPriceInfo){
              dataAll.draw.prizeList[i].couponQuota = dataAll.draw.prizeList[i].fullPriceInfo
            }

          }
          this.taskCouponList = dataAll.draw.prizeList

        }else{
          //不请求接口 模板预览
          this.taskCouponList = [
            {
              beanNum: 1,
              beginTime: '2022-06-22 14:21',
              couponQuota: 20,
              couponType: 1,
              couponValidateDay: 30,
              discountType: 1,
              endTime: '2022-07-22 00:00',
              expireType: 1,
              priceInfo: '5',
              prizeInfoId: '1539535031777038338',
              prizeName: '1',
              prizeType: 2,
              receive: 0,
              remainNum: 1,
              showImg: '//img10.360buyimg.com/imgzone/jfs/t1/215393/22/4731/33329/61946651E535ea01f/5cee5951d6bd1612.png'
            }
          ];
        }

      } else {
        couponMarketingList(res => {
          this.taskCouponList = res.data.taskCouponDTOS;
        });
      }
    },
    drawFun(itemData) {
      let actStatus = sessionStorage.getItem('actStatus');
      if (this.isTemplate) {
        this.$toast('活动预览，仅供查看');
        return;
      }
      if (actStatus == 1) {
        this.$toast('活动尚未开始');
        return;
      } else if (actStatus == 2) {
        this.$toast('活动已结束');
        return;
      }
      if (itemData.remainNum == 0) {
        this.$toast('该优惠券已领完~');
        return;
      }

      if (itemData.receive == 4) {
        this.$toast('优惠券未生效，无法领取~');
        return;
      }
      if (itemData.receive == 5) {
        this.$toast('优惠券已过期，无法领取~');
        return;
      }

      if (this.preventClick) {
        return;
      }
      this.preventClick = true;
      immediatelyDraw3(itemData.prizeInfoId, receiveData => {
        if (receiveData.resp_code == 0) {
          if (receiveData.data.result == true) {
            var obj = {
              datas: {},
              keyData: {
                prizeUrl: 'showImg',
                prizeName: 'prizeName',
                prizeType: 'prizeType',
                priceInfo: 'priceInfo',
                discountType: 'discountType',
                couponType: 'couponType'
              }
            };
            obj.datas = itemData;
            PrizeResultPopComService.create(obj, receiveData.data, this.styleData, submit => {
              console.log(submit);
              PrizeResultPopComService.close();
            });
            this.initData();
            this.preventClick = false;
          }else{
            NoPrizeResultPopComService.create(this.styleData, submit => {
              NoPrizeResultPopComService.close();
            });
            this.initData();
            this.preventClick = false;
          }
        } else {
          setTimeout(() => {
            this.$toast('全部券已领完，可去店铺查看更多活动~');
          }, 1000);
          this.initData();
          this.preventClick = false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.couponMarkDiv {
  width: 6.9rem;
  margin-left: calc(50% - 6.9rem / 2);
}
</style>
