var t = getApp(), e = require("../../utils/login.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: '',
    agentname: '',
    devicecode: '',
    remainingTime: 0,
    orderid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const orderInfo = wx.getStorageSync('progressorder');
    if (!orderInfo) {
      wx.showToast({
        title: '没有进行中的订单',
        icon: 'none',
        duration: 2000
      });
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/index/index'
        });
      }, 2000);
      return;
    }

    const currentTime = Math.floor(Date.now() / 1000);
    const remainingTime = Math.max(0, orderInfo.timestamp + orderInfo.siteoccupationTime * 60 - currentTime);

    this.setData({
      agentname: orderInfo.agentname,
      devicecode: orderInfo.devicecode,
      remainingTime: remainingTime,
      orderid: orderInfo.id
    });

    if (remainingTime > 0) {
      this.countdown();
    }
  },

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

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handleSettlement() {
    wx.showModal({
      title: '确认结算',
      content: '确定要结算此次洗车订单吗?',
      success: (res) => {
        if (res.confirm) {
          e.request('/miniprogram/machine/shutdown', {
            orderid: this.data.orderid
          }, true).then((res) => {
            if (res.code === 200) {
              wx.showToast({
                title: res.message || '结算成功',
                icon: 'success',
                duration: 2000
              });
              setTimeout(() => {
                wx.removeStorageSync('progressorder');
                wx.reLaunch({
                  url: '/pages/index/index'
                });
              }, 2000);
            }
          }).catch((err) => {
            // setTimeout(() => {
            //   wx.removeStorageSync('progressorder');
            //   wx.reLaunch({
            //     url: '/pages/index/index'
            //   });
            // }, 2000);
          })
        }
      }
    });
  },

  countdown() {

    // 没有  Cannot read property 'remainingTime' of undefined
    if (!this.data.remainingTime) {
      this.setData({
        countdown: '00:00'
      });
      return;
    }


    if (this.data.remainingTime <= 0) {
      this.setData({
        countdown: '00:00'
      });
      return;
    }

    const minute = Math.floor(this.data.remainingTime / 60);
    const second = this.data.remainingTime % 60;
    const formattedMinute = minute < 10 ? '0' + minute : minute;
    const formattedSecond = second < 10 ? '0' + second : second;

    this.setData({
      countdown: formattedMinute + ':' + formattedSecond,
      remainingTime: this.data.remainingTime - 1
    });

    setTimeout(this.countdown, 1000);
  },
})