// packageE/lottery/lottery.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",

    goods: [],
    radio: "",
    prizeIndex: 999,
    arrNum: [0, 1, 2, 3, 7, 11, 15, 14, 13, 12, 8, 4], // 定义转动的顺序
    clickFlage: true, // 点击事件，防止重复点击
    prizeInfoShow: false, // 显示中奖信息的遮罩层
    noPrizeShow: false, // 没中奖
    havePrizeShow: false, // 中奖了
    haveLottery: false, // 已经抽过奖了
    timer1: "",
    timer2: "",
    s1: "",
    s2: -1,
    lotteryId: "", // 抽奖活动的id
    hasRecord: false, // 是否显示获奖情况
    remainingTimes: 0, // 剩余抽奖次数
    prizeList: [], // 奖项列表
    prizeLevel: "", // 中奖等级
    prizeName: "", // 奖品名称
    prizeUrl: "", // 奖品图片
    timeFlag: 0, // 时间标记，抽奖请求时间过长，则返回错误
    startStatus: 1, // 活动开始的状态
    prizerListBol: false,

    activityModel: {},
    countdown_time: [],
    result_arr: [],
    usable: "",
    success_app_url: "", // 成功跳转链接
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
    let basic_info = wx.getStorageSync("yz_basic_info");
    if (basic_info.home.memberinfo) {
      this.setData({
        usable: basic_info.home.memberinfo.usable,
      });
    }
    if (this.data.datas.lottery && this.data.datas.lottery.name) {
      this.initData();
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    toUrl() {
      wx.navigateTo({
        url: "/packageC/orders/LotteryRecord/LotteryRecord?lotteryId=" + this.data.lotteryId,
      });
    },
    onChange(event) {
      this.setData({
        radio: event.detail,
      });
    },
    closePop() {
      this.setData({
        show: false,
      });
    },
    confirmGoods() {
      this.setData({
        show: false,
      });
      if (this.data.radio) {
        wx.navigateTo({
          url: "/packageA/detail_v2/detail_v2?id=" + this.data.radio,
        });
      }
    },
    // 更新页面数据
    getData() {
      console.log('111111');
      let that = this;
      let urlStr = app.getNetAddresss("plugin.lucky-draw.frontend.draw.index");
      app._getNetWork({
        url: urlStr,
        data: {
          lotteryId: this.data.lotteryId
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              activityModel: res.data.activityModel,
              ['datas.list.point.credit1']:res.data.point.credit1
            });
            if(res.data.love){
              that.setData({
                usable:res.data.love[0],
              });
            }
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    },
    initData() {
      if(!this.data.datas.list) {
        return;
      }
      let that = this;
      this.setData({
        result_arr: this.data.datas.list.result_arr,
        activityModel: this.data.datas.list.activityModel,
        success_app_url: this.data.datas.list.activityModel.success_app_url,
        countdown_time: this.data.datas.list.activityModel.countdown_time,
        lotteryId: this.data.datas.list.activityModel.id,
        remainingTimes: this.data.datas.list.surplus_time,
        goods: this.data.datas.list.goods,
      });
      if (this.data.goods && this.data.goods.length > 0) {
        that.setData({
          radio: this.data.goods[0].id,
        });
      }
      this.data.result_arr.forEach((item) => {
        item.bgSrc = "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/lottery/item_bg.png";
      });
      this.data.result_arr.splice(5, 0, " ");
      this.data.result_arr.splice(6, 0, " ");
      this.data.result_arr.splice(9, 0, " ");
      this.data.result_arr.splice(10, 0, " ");
      that.setData({
        prizeList: this.data.result_arr,
      });
      let temp = that.data.prizeList[4];
      that.setData({
        "prizeList[4]": that.data.prizeList[7],
        "prizeList[7]": temp,
      });
      temp = that.data.prizeList[12];
      that.setData({
        "prizeList[12]": that.data.prizeList[13],
        "prizeList[13]": temp,
      });
      temp = that.data.prizeList[14];
      that.setData({
        "prizeList[14]": that.data.prizeList[15],
        "prizeList[15]": temp,
      });

      // 获取状态，开始，未开始，结束
      that.statusFun(Date.parse(new Date()) / 1000, that.data.countdown_time[0], that.data.countdown_time[1]);
    },
    statusFun(c, s, e) {
      if (c < s) {
        // 当前时间小于开始时间，未开始
        this.setData({
          startStatus: 0,
        });
      } else if (c > s && c < e) {
        // 当前时间大于开始时间，并且小于结束时间，已经开始未结束
        this.setData({
          startStatus: 1,
        });
      } else if (c > e) {
        // 当前时间大于结束时间，已经结束
        this.setData({
          startStatus: 2,
        });
      }
    },
    prizeZhuan() {
      let that = this;
      this.setData({
        prizeIndex: 0,
      });
      if (this.data.remainingTimes > 0) {
        // 判断剩余抽奖次数
        if (this.data.clickFlage) {
          if (this.data.startStatus === 1) {
            // 活动开始
            this.setData({
              clickFlage: false, // 不能点击
              timer1: setInterval(() => {
                this.move();
              }, 100),
            });
            let urlStr = app.getNetAddresss("plugin.lucky-draw.frontend.draw.doDraw");
            app._getNetWork({
              url: urlStr,
              data: {
                lotteryId: this.data.lotteryId,
              },
              success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                  that.setData({
                    remainingTimes: that.data.remainingTimes - 1,
                  });
                  if (res.data.result) {
                    that.setData({
                      hasRecord: true,
                      prozeLevel: res.data.result.item,
                      prizeName: res.data.result.prize,
                      prizeUrl: res.data.result.thumb_url,
                    });
                    that.getData();
                  } else {
                    that.setData({
                      hasRecord: false,
                      prozeLevel: res.data.item,
                      prizeName: res.data.prize,
                      prizeUrl: res.data.thumb_url,
                      empty_prize_prompt: res.data.empty_prize_prompt,
                    });
                    that.getData();
                  }
                  setTimeout(() => {
                    clearInterval(that.data.timer1);
                    that.lowSpeed();
                  }, 1200);
                } else {
                  wx.showModal({
                    title: "提示",
                    content: res.msg,
                    showCancel: false,
                    success(res_1) {
                      if (res_1.confirm) {
                        if (that.data.goods && that.data.goods.length > 0) {
                          that.setData({
                            show: true,
                          });
                        }
                      }
                    },
                  });
                  that.setData({
                    clickFlage: true, // 不能点击
                  });
                  clearInterval(that.data.timer1);
                  clearInterval(that.data.timer2);
                }
              },
              fail: function (res) {
                console.log(res);
              },
            });
            // 请求，返回后给s定值
          } else if (this.data.startStatus === 0) {
            // 没开始
            wx.showToast({
              icon: "none",
              title: "活动尚未开始",
              duration: 1500,
            });
          } else if (this.data.startStatus === 2) {
            // 已经结束
            wx.showToast({
              icon: "none",
              title: "活动已经结束",
              duration: 1500,
            });
          }
        }
      } else {
        // 没有抽奖机会了
        this.setData({
          prizeInfoShow: true,
          haveLottery: true,
        });
      }
    },
    // 降速
    lowSpeed() {
      clearInterval(this.data.timer1);
      this.setData({
        clickFlage: false, // 不能点击
        timer2: setInterval(() => {
          this.move();
        }, 300),
      });
      setTimeout(() => {
        // 顺序打乱
        this.setData({
          s2: this.data.prozeLevel,
        });
      }, 900);
    },
    move() {
      if (this.data.prizeIndex === 0) {
        this.setData({
          prizeIndex: this.data.prizeIndex + 1,
        });
      } else if (this.data.prizeIndex === 12) {
        this.setData({
          prizeIndex: 0,
        });
      } else {
        this.setData({
          prizeIndex: this.data.prizeIndex + 1,
        });
      }
      if (this.data.s2 !== -1 && this.data.prizeList[this.data.arrNum[this.data.prizeIndex]] && this.data.prizeList[this.data.arrNum[this.data.prizeIndex]].item == this.data.s2) {
        clearInterval(this.data.timer1);
        clearInterval(this.data.timer2);
        this.stop();
      }
      this.setData({
        timeFlag: this.data.timeFlag + 100,
      });
      if (this.data.timeFlag >= 10000 && !this.data.prizeName) {
        clearInterval(this.data.timer1);
        clearInterval(this.data.timer2);
        wx.showToast({
          icon: "none",
          title: "当前抽奖人数过多，稍后再来",
          duration: 1500,
        });
        this.setData({
          clickFlage: true,
        });
      }
    },
    // 停止
    stop() {
      this.setData({
        s2: -1,
        clickFlage: false,
      });
      this.prizeInfo(); // 弹出是否中奖
    },
    prizeInfo() {
      setTimeout(() => {
        this.setData({
          clickFlage: true,
        });
        if (this.data.hasRecord) {
          this.setData({
            prizeInfoShow: true,
            havePrizeShow: true,
            noPrizeShow:false
          });
        } else {
          this.setData({
            prizeInfoShow: true,
            noPrizeShow: true,
            havePrizeShow:false
          });
        }
        if (this.data.noPrizeShow) {
          wx.showModal({
            title: "提示",
            content: `${this.data.empty_prize_prompt || "很遗憾，您没有中奖"}`,
            showCancel: false,
          });
        }
        if (this.data.havePrizeShow) {
          wx.showModal({
            title: "提示",
            content: `恭喜您抽中${this.data.prizeName}`,
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                if (this.data.success_app_url.indexOf("http") == -1) {
                  wx.navigateTo({
                    url: this.data.success_app_url,
                    fail: (res) => {},
                  });
                }
              }
            },
          });
        }
        if (this.data.haveLottery) {
          wx.showModal({
            title: "提示",
            content: "您已经抽过奖啦，不要太贪心哦！",
            showCancel: false,
          });
        }
      }, 800);
    },
  },
});
