const { minutesToTimeEl } = require("../common");

// packageH/freeLottery/index/index.js
const App = getApp();
const LottreyWaitUrl = App.getNetAddresss("plugin.free-lottery.frontend.lottery.wait");
const LottreyUrl = App.getNetAddresss("plugin.free-lottery.frontend.lottery");
let LottertWaitHandler = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    prizies: [
      {
        text: "谢谢参与",
      },
      {
        text: "免单",
      },
      {
        text: "谢谢参与",
      },
      {
        text: "免单",
      },
      {
        text: "谢谢参与",
      },
      {
        text: "免单",
      },
      {
        text: "谢谢参与",
      },
      {
        text: "免单",
      },
      {
        text: "谢谢参与",
      },
      {
        text: "免单",
      },
    ],
    pageLoaded: false,
    hiddenResultPopup: true,
    startSprnning: false,
    isLottery: false,
    showFooterButtons: false,
    luckyNumber: 0,
    orders: [],
    orderId: null,
    task: {},
    freeTasks: [],
    lotteryPopupClass: "",
    turntableSprnned: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.order_ids) {
      wx.showToast({
        title: "订单不存在",
        icon: "none",
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1000);
      return;
    }
    wx.showLoading({
      title: "订单支付中",
      // mask: true,
    });
    this.pollingConfirmPay(1000, options.order_ids);
    this.checkConfirmPay(options.order_ids);
    this.setData({
      orderId: options.order_ids,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(LottertWaitHandler);
    LottertWaitHandler = null;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync("yz_uid");
    return {
      path: "/packageG/index/index?mid="+userId,
    };
  },
  turntabeEnd(e) {
    this.setData({
      turntableSprnned: true,
    });
    setTimeout(() => {
      this.setData({
        hiddenResultPopup: false,
      });
    }, 300);
  },
  displayResultPopup() {
    if (this.data.turntableSprnned === true && this.data.pageLoaded === true) {
      this.setData({
        hiddenResultPopup: !this.data.hiddenResultPopup,
      });
    }
  },
  //* 转盘转动
  lottery() {
    App._getNetWork({
      url: LottreyUrl,
      data: {
        order_ids: this.data.orderId,
      },
      success: ({ data: { data, result, msg } }) => {
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        data.task.limit_day = Number(data.task.limit_time / 1440).toFixed(1);
        data.task.limit_str = minutesToTimeEl(data.task.limit_time);
        let lotteryPopupClass = "lottery-popup_lost";
        if (data.status === true) {
          if (data.task.status === 1 || data.task.status === -1) {
            lotteryPopupClass = "";
          } else if (data.task.status === 0) {
            lotteryPopupClass = "lottery-popup_first";
          }
        }

        this.setData(
          {
            luckyNumber: data.status === true ? 2 : 3,
          },
          () => {
            this.setData({
              luckyNumber: data.status === true ? 2 : 3,
              startSprnning: true,
              isLottery: data.status,
              freeTasks: data.dividend,
              task: data.task,
              lotteryPopupClass,
              pageLoaded: true,
              showFooterButtons: data.status && data.task.status === 0,
            });
          }
        );

        wx.setNavigationBarTitle({
          title: data.plugin_name,
        });
      },
      fail(err) {
        wx.showToast({
          title: err.msg || err.message,
          icon: "none",
        });
      },
    });
  },
  //* 轮询确认支付成功，方可让转盘 真正转动
  pollingConfirmPay(time = 3000, orderId = this.data.orderId) {
    LottertWaitHandler = setInterval(() => {
      this.checkConfirmPay(orderId);
    }, time);
  },
  checkConfirmPay(orderId = this.data.orderId) {
    App._getNetWork({
      url: LottreyWaitUrl,
      data: {
        order_ids: orderId,
      },
      success: ({
        data: {
          data: { status },
        },
      }) => {
        if (status) {
          wx.hideLoading();
          clearInterval(LottertWaitHandler);
          LottertWaitHandler = null;
          this.lottery();
        }
      },
    });
  },
  toRecords() {
    wx.navigateTo({
      url: "/packageH/free_of_charge/FreeRecord/FreeRecord",
    });
  },
});
