<template>
  <div v-if="show">
    <div class="activePrizeDiv">
      <div class="activeDiv">
         <div class="shopName">
          {{ pcontent }}
        </div>
        <div style="text-align: center">
          <img
            src="//img10.360buyimg.com/imgzone/jfs/t26485/16/1007470757/86545/2749209b/5bbf0ce2N4fede82b.png"
            style="width: 4.04rem"
          />
        </div>
        <div style="position: relative; top: 0.3rem">
          <div class="eggs" ref="topInfo">
            <div
              class="egg"
              :class="[run == 0 ? jump : '', active]"
              @click="boom(0)"
            ></div>
            <div
              class="egg"
              :class="[run == 1 ? jump : '', active1]"
              @click="boom(1)"
            ></div>
            <div
              class="egg"
              :class="[run == 2 ? jump : '', active2]"
              @click="boom(2)"
            ></div>
            <div
              class="egg"
              :class="[run == 3 ? jump : '', active3]"
              @click="boom(3)"
            ></div>
            <div
              class="egg"
              :class="[run == 4 ? jump : '', active4]"
              @click="boom(4)"
            ></div>
            <div
              class="egg"
              :class="[run == 5 ? jump : '', active5]"
              @click="boom(5)"
            ></div>
          </div>

          <div
            v-if="chuiziFlag == 0"
            class="chuizi chuizi00"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 1"
            class="chuizi chuizi01"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 2"
            class="chuizi chuizi02"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 3"
            class="chuizi chuizi03"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 4"
            class="chuizi chuizi04"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 5"
            class="chuizi chuizi05"
            :class="chuizi == 0 ? jump : ''"
          ></div>
          <div
            v-if="chuiziFlag == 6"
            class="chuizi chuizi06"
            :class="chuizi == 0 ? jump : ''"
          ></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import store from "../../../store/store";
import { goToShopPage } from "../../../util/tools.js";
import apis1 from "../../../util/apis1.js";
let { drawPrize, startDraw, followShopAddTask } = apis1;
import {
  PrizeResultPopComService,
  NoPrizeResultPopComService,
  FollowShopnotWinPrizePopComService,
} from "../../../util/comCreate.js";
export default {
  props: ["isTemplate", "datas", "styleData", "keyData", "templateData"],
  data() {
    return {
     prizeListData: [
        {
          position: 1,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 2,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 3,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 4,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 5,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 6,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 7,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
        {
          position: 8,
          showImg: "",
          angle: 0,
          prizeName: "谢谢参与",
        },
      ],
      run: -1, // 金蛋样式初始值
      chuizi: 0, // 锤子样式初始值
      chuiziFlag: 0, // 锤子显示位置
      showPop: false, // 砸蛋限制
      jump: "jump",
      active: "",
      active1: "",
      active2: "",
      active3: "",
      active4: "",
      active5: "",
      eggBg: "", // 金蛋图片
      isShow: "", // 按钮文字是一张图片 false,否则true
      eggButton: false, // 控制砸蛋 状态
      shopName: "",
      prizeResultData: {
        styleData: {},
        datas: {},
        keyData: {
          prizeUrl: "showImg",
          prizeName: "prizeName",
          prizeType: "prizeType",
          priceInfo: "priceInfo",
          discountType: "discountType",
          couponType: "couponType",
        },
      },
    };
  },
  computed: {},
  created() {
    this.initData();
  },
  mounted() {
    var i = -1;
    this.timer = setInterval(() => {
      if (i < 6) {
        i++;
      } else {
        i = -1;
      }
      this.run = i;
    }, 500);
    this.timer1 = setInterval(() => {
      // 动态锤子
      if (this.chuizi == 0) {
        this.chuizi = 1;
      } else {
        this.chuizi = 0;
      }
    }, 500);
  },
  methods: {
    closeClick() {
      this.submit();
    },
    async initData() {
      if (this.isTemplate) {
      } else {
        //获取奖品信息接口
    drawPrize((initdatas) => {
          console.log(initdatas,"ssss");
          
          let prizeList = initdatas.data.prizeInfo;
          this.restDrawCount = initdatas.data.drawNumber;
          if (prizeList.length > 0) {
            for (var i = 0; i < this.prizeListData.length; i++) {
              for (var j = 0; j < prizeList.length; j++) {
                if (prizeList[j].position == this.prizeListData[i].position) {
                  this.prizeListData[i] = prizeList[j];
                }
              }
            }
          }
  
        });
      }
    },
    boom(index) {
      if (this.isTemplate) {
        this.$toast("活动预览，仅供查看");
        return;
      }
      const self = this;
      let actStatus = sessionStorage.getItem("actStatus");
      if (actStatus == 2) {
        this.$toast("活动已结束");
        return;
      }
      if (actStatus == 1) {
        this.$toast("活动未开始");
        return;
      }

      if (this.eggButton) {
        this.$toast("正在砸蛋中，请稍后再试");
        return;
      }

      this.eggButton = true;

      // 砸蛋行为记录
      if (!this.showPop) {
        followShopAddTask((initdatas) => {});
      }

      if (!this.showPop) {
        this.run = index;
        if (index == 0) {
          this.chuiziFlag = 1;
        } else if (index == 1) {
          this.chuiziFlag = 2;
        } else if (index == 2) {
          this.chuiziFlag = 3;
        } else if (index == 3) {
          this.chuiziFlag = 4;
        } else if (index == 4) {
          this.chuiziFlag = 5;
        } else if (index == 5) {
          this.chuiziFlag = 6;
        }
      }

      clearInterval(this.timer);
      this.timer = null;

      let _this = this;
      if (!_this.showPop) {
        setTimeout(function () {
          if (index == 0) {
            _this.active = "active";
          } else if (index == 1) {
            _this.active1 = "active";
          } else if (index == 2) {
            _this.active2 = "active";
          } else if (index == 3) {
            _this.active3 = "active";
          } else if (index == 4) {
            _this.active4 = "active";
          } else if (index == 5) {
            _this.active5 = "active";
          }
        }, 1500);
      }

      setTimeout(function () {
        if (_this.showPop) {
          FollowShopnotWinPrizePopComService.create(
            "亲，您已参加过活动。",
            "https://img10.360buyimg.com/imgzone/jfs/t18640/237/360861051/18605/7bbde14/5a7126a4Nfb005ae2.png",
            (submit) => {
              FollowShopnotWinPrizePopComService.close();
              _this.active = "";
              _this.active1 = "";
              _this.active2 = "";
              _this.active3 = "";
              _this.active4 = "";
              _this.active5 = "";
              var i = -1;
              _this.timer = setInterval(() => {
                if (i < 6) {
                  i++;
                } else {
                  i = -1;
                }
                _this.run = i;
              }, 500);
              _this.eggButton = false;
              _this.chuiziFlag = 0;
            }
          );
          return;
        }
        _this.showPop = true;

        // 判断是否中奖
        startDraw((initDatas) => {
          if (initDatas.resp_code == 50009) {
            this.$toast("活动已结束");
            return;
          }

          if (initDatas.data) {
            console.log(_this.prizeListData);
            for (var i = 0; i < _this.prizeListData.length; i++) {
              if (_this.prizeListData[i].id == initDatas.data.prizeInfoId) {
                _this.prizeResultData.datas = _this.prizeListData[i];

                PrizeResultPopComService.create(
                  _this.prizeResultData,
                  initDatas.data,
                  (submit) => {
                    PrizeResultPopComService.close();
                    _this.active = "";
                    _this.active1 = "";
                    _this.active2 = "";
                    _this.active3 = "";
                    _this.active4 = "";
                    _this.active5 = "";
                    var i = -1;
                    _this.timer = setInterval(() => {
                      if (i < 6) {
                        i++;
                      } else {
                        i = -1;
                      }
                      _this.run = i;
                    }, 500);
                    _this.eggButton = false;
                    _this.chuiziFlag = 0;
                  }
                );

                break;
              }
            }
          } else {
            NoPrizeResultPopComService.create(
              "非常遗憾，您与大奖擦肩而过~",
              "//img10.360buyimg.com/imgzone/jfs/t1/136745/32/20121/48059/5fd996feE56314a28/b952f0e6f3fe811f.png",
              (submit) => {
                NoPrizeResultPopComService.close();
                _this.active = "";
                _this.active1 = "";
                _this.active2 = "";
                _this.active3 = "";
                _this.active4 = "";
                _this.active5 = "";
                var i = -1;
                _this.timer = setInterval(() => {
                  if (i < 6) {
                    i++;
                  } else {
                    i = -1;
                  }
                  _this.run = i;
                }, 500);
                _this.eggButton = false;
                _this.chuiziFlag = 0;
              }
            );
          }
        });
      }, 1800);
    },
  },
  // //得奖后的处理
  // fulfillHandle(prizeData, initDatas) {
  //   this.prizeResultData.datas = prizeData;
  //   console.log(this.prizeResultData.datas + "this.prizeResultData.datas");
  //   PrizeResultPopComService.create(
  //     this.prizeResultData,
  //     initDatas,
  //     (submit) => {
  //       PrizeResultPopComService.close();
  //       this.active = "";
  //       this.active1 = "";
  //       this.active2 = "";
  //       this.active3 = "";
  //       this.active4 = "";
  //       this.active5 = "";
  //       var i = -1;
  //       this.timer = setInterval(() => {
  //         if (i < 6) {
  //           i++;
  //         } else {
  //           i = -1;
  //         }
  //         this.run = i;
  //       }, 500);
  //       this.eggButton = false;
  //       this.chuiziFlag = 0;
  //     }
  //   );
  // },

  enterstore() {
    if (this.isTemplate) {
      this.$toast("活动预览，仅供查看");
      return;
    }
    document.querySelector("body").removeAttribute("style");
    console.log("进入商店");
    let shopId = store.state.shopId;
    goToShopPage(shopId);
  },
};
</script>
<style lang="scss" scoped>
.activePrizeDiv {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  z-index: 20;
}

.activeDiv {
  max-width: 750px;
  margin-left: auto;
  margin-right: auto;
  min-height: 100%;
  background: #2E46FA
    url(https://img10.360buyimg.com/imgzone/jfs/t17188/295/2382009087/52409/10a390d/5af26cd1N0a9c5fa8.jpg)
    no-repeat;
  background-size: 100%;
  padding-top: 0.2rem;
}
.shopName {
  color: rgb(255, 255, 255);
  font-size: 0.24rem;
  margin-top: .3rem;
  margin-left: .3rem;
}
.f-tac {
  text-align: center;
}

.f-ib {
  display: inline-block;
  vertical-align: middle;
}
.chuizi,
.eggs .egg,
.record {
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.fliter {
  -webkit-filter: blur(0.025rem);
  filter: blur(0.025rem);
}

.eggs,
.eggs .egg {
  position: relative;
}

.eggs {
  overflow: hidden;
  padding: 0 0.14rem;
}

.eggs.two {
  padding: 0 0.34rem;
}

.eggs.two .egg {
  margin-right: 0.175rem;
  margin-left: 0.175rem;
}

.eggs.four .egg:nth-child(4) {
  margin-left: 0.97rem;
}

.eggs.five .egg:nth-child(4) {
  margin-right: 0.35rem;
  margin-left: 0.37rem;
}

.eggs.five .egg:nth-child(5) {
  margin-left: 0;
}

.eggs.seven .egg:nth-child(7) {
  margin-left: 0.99rem;
}

.eggs.eight .egg:nth-child(7) {
  margin-right: 0.35rem;
  margin-left: 0.39rem;
}

.eggs.eight .egg:nth-child(8) {
  margin-left: 0;
}

.eggs .egg {
  float: left;
  width: 1.8rem;
  height: 1.8rem;
  margin: 0.5rem 0.3rem 0;
}

.eggs .egg:nth-child(7),
.eggs .egg:nth-child(8) {
  display: none;
}

.eggs .egg.jump {
  top: -0.2rem;
}

.eggs::after {
  display: table;
  content: "";
}

.eggs::after:after,
.eggs::after:before {
  display: table;
  content: "";
}

.chuizi {
  position: absolute;
  width: 0.86rem;
  height: 0.86rem;
  -webkit-transform-origin: right bottom;
  transform-origin: right bottom;
  -webkit-transform: rotate(15deg);
  transform: rotate(15deg);
}
.chuizi00 {
  top: -0.3rem;
  left: 6.3rem;
}
.chuizi01 {
  top: -0.153rem;
  left: 1.53rem;
}
.chuizi02 {
  top: -0.3rem;
  left: 3.9rem;
}
.chuizi03 {
  top: -0.15rem;
  left: 6.3rem;
}
.chuizi04 {
  top: 2.15rem;
  left: 1.5rem;
}
.chuizi05 {
  top: 2.15rem;
  left: 3.9rem;
}
.chuizi06 {
  top: 2.15rem;
  left: 6.3rem;
}

.chuizi.jump {
  -webkit-transform: rotate(0);
  transform: rotate(0);
}

.egg {
  background-image: url("https://img10.360buyimg.com/imgzone/jfs/t17185/343/387607812/9256/65d81644/5a70267dN6ff1a444.png") !important;
}

.egg.jump.active {
  background-image: url("https://img10.360buyimg.com/imgzone/jfs/t18454/350/368386222/7697/2deb95c8/5a70267dN25811be5.png") !important;
}

.egg.jump {
  background-image: url("https://img10.360buyimg.com/imgzone/jfs/t17185/343/387607812/9256/65d81644/5a70267dN6ff1a444.png") !important;
}

.chuizi {
  background-image: url("https://img10.360buyimg.com/imgzone/jfs/t19690/51/357920895/3833/50322da7/5a70267dN73fe0b52.png") !important;
}
</style>
