const { default: api } = require("../../js/api");

Page({
  data: {
    id: 1 , // 初始ID值
    nozzleId:0,
    order:null,
    // countdown: 120,
    progressWidth: 100, // 初始进度条宽度为100%
    totalSeconds: 120,// 总秒数
    currentSeconds: 120, // 当前剩余秒数
    countDownTimer:null, //倒计时定时器
    checkNozzleStatuesTimer:null, //检查油枪状态定时器
  },
  onLoad(options) {
    console.log("收到预支付确定页信息",options)
    this.setData({
      id:options.id,
      nozzleId:options.nozzleId
    })
    
    const that = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromAuthorization', function (data) {
      console.log(data)
      that.setData({
        order:data
      })
      //授权
    if(options.id == 1) {
      that.toAuthorization();
    }
    });
    
  },

  /** 返回重新支付 */
  back(){
    setTimeout(() => {
      wx.navigateBack()
    },500)
  },
  
  /** 授权 */
  toAuthorization() {
    this.setData({
      id:1
    })
    const that = this;
    api.request_NozzleAuthorization(this.data.order.orderId).then(res => {
      console.log("授权",res)
      if(res.data.statusCode != 200) {
        that.setData({
          id:5
        })
        return
      }
      that.setData({
        id:3
      })
      that.countDown()
      that.startCheckNozzle()
    }).catch(err => {
      console.log("授权报错",err)
      that.setData({
        id:5
      })
    })
  },

  /** 取消授权 */
  toUnAuthorization(){
    const that = this;
    wx.showLoading({
      title: '正在取消授权',
    })
    api.request_CancelNozzleAuthorization(this.data.order.orderId).then(res => {
      console.log("取消授权",res)
      wx.hideLoading()
      if(res.data.statusCode != 200) {
        wx.showToast({
          title: '取消授权失败，重新取消授权',
        })
        that.countDown()
        that.startCheckNozzle()
        return
      }
      that.setData({
        id:5
      })
    }).catch(err => {
      console.log("取消授权报错",err)
      wx.hideLoading()
    })
  },

  /** 查看历史订单 */
  toHistory() {
    setTimeout(() => {
      wx.reLaunch({
        url: '../historyOrder/historyOrder?paymentMode='+1,
      })
    },500)
  },

  /** 授权成功倒计时 */
  countDown() {
    const that = this;
    let timer = setInterval(() => {
      let { currentSeconds, totalSeconds } = that.data;
      currentSeconds--;
      if (currentSeconds < 0) {
        that.toUnAuthorization()
        that.setData({
          currentSeconds: 120,
          progressWidth: 100
        })
        that.clearTimer()
        return
      }
      let aprogressWidth = (currentSeconds / totalSeconds) * 100;
      that.setData({
        currentSeconds: currentSeconds,
        progressWidth: aprogressWidth
      });
    }, 1000);
    that.setData({
      countDownTimer:timer
    }) // 保存定时器，以便在onUnload中清除
  },

  /** 轮询检查油枪状态 */
  startCheckNozzle(){
    const that = this;
    const timer = setInterval(() => {
      that.checkNozzleInfo()
    }, 3000);
    that.setData({
      checkNozzleStatuesTimer:timer
    })
  },
  /** 检查油枪状态 */
  checkNozzleInfo(){
    const that = this;
    api.request_GetNozzleInfo(this.data.nozzleId).then(res => {
      if (res.data.content.length != 0) {
        var nozzle = res.data.content[0];
        //加油态，转加油中页面
        if(nozzle.status == 8) {
          that.setData({
            id:4
          })
          that.clearTimer()
        }
      }
    })
  },

  onUnload() {
    this.clearTimer()
  },

  clearTimer(){
    if (this.data.countDownTimer != null) {
      clearInterval(this.data.countDownTimer);
      this.data.countDownTimer = null;
    }
    if(this.data.checkNozzleStatuesTimer != null) {
      clearInterval(this.data.checkNozzleStatuesTimer)
      this.data.checkNozzleStatuesTimer = null;
    }
  }
});