<template>
  <div>
    <div class="btnStatus" v-if="btnStatusImg"><img :src="btnStatusImg" @click="getDraw()" /></div>
  </div>
</template>

<script>
import apis1 from '../../../util/apis1.js';
let { getUserFollowInfo, saveFollowInfo } = apis1;
import { PrizeResultPopComService, NoPrizeResultPopComService } from '../../../util/comCreate.js';
export default {
  props: ['isTemplate', 'datas', 'keyData', 'templateData', 'styleData'],
  data() {
    return {
      prizeResultData: {
        styleData: {},
        datas: {},
        keyData: {
          prizeUrl: 'showImg',
          prizeName: 'prizeName',
          prizeType: 'prizeType',
          priceInfo: 'priceInfo',
          discountType: 'discountType',
          couponType: 'couponType'
        }
      },
      btnStatusImg: '',
      btnStatusImg1: '//img10.360buyimg.com/imgzone/jfs/t1/114673/16/24459/82046/624d572eE006c0965/ade2af887e2cf39c.png'
    };
  },
  mounted() {
    this.publicData.$on('getUserFollowInfoData', goodsNum => {
      this.goodsNum = goodsNum;
    });
    if (this.styleData.btnImg) {
      this.btnStatusImg = this.styleData.btnImg.btnStatusImg;
    } else {
      this.btnStatusImg = this.btnStatusImg1;
    }
  },
  methods: {
    //领奖
    getDraw() {
      let actStatus = sessionStorage.getItem('actStatus');
      if (this.isTemplate) {
        this.$toast('活动预览，仅供查看');
        return;
      }
      if (actStatus == '1') {
        this.$toast('活动未开始');
        return;
      }
      if (actStatus == '2') {
        this.$toast('活动已结束');
        return;
      }
      if (this.goodsNum > 0) {
        getUserFollowInfo(shopData => {
          if (shopData.data.goodsNum > 0) {
            if (shopData.data.followShop) {
              //有参加资格
              //可以正常关注店铺领奖
              saveFollowInfo(data111 => {
                if (data111.resp_code == 0) {
                  let prizeData = data111.data;
                  let initDatas = data111.data;
                  //重新刷新接口显示奖品剩余数量
                  this.publicData.$emit('reShowPrize', true);

                  if (data111.resp_msg == '发放奖品成功') {
                    //中奖
                    this.prizeResultData.datas = prizeData;
                    PrizeResultPopComService.create(this.prizeResultData, initDatas, submit => {
                      PrizeResultPopComService.close();
                    });
                  } else {
                    NoPrizeResultPopComService.create(
                      '非常遗憾，您与大奖擦肩而过~',
                      '//img10.360buyimg.com/imgzone/jfs/t1/136745/32/20121/48059/5fd996feE56314a28/b952f0e6f3fe811f.png',
                      submit => {
                        NoPrizeResultPopComService.close();
                      }
                    );
                  }
                }
              });
            } else {
              //没有参加资格
              this.$toast('此活动只针对新关注店铺用户~');
            }
          } else {
            this.$toast('奖品已发完哦~');
          }
        });
      } else {
        this.$toast('奖品已发完哦~');
      }
    }
  }
};
</script>

<style lang="scss">
.btnStatus {
  height: 1rem;
  margin-top: 0.3rem;
  img {
    width: 6.9rem;
    height: 1rem;
  }
}
</style>
