// pages/batteryPack/batteryPackWait.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chargePileId: -9,
    timer:{},
    userInfo:{},
    // count: 0 , // 用于测试
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let userInfo = wx.getStorageSync('userInfo');
    let timer  = setInterval(()=>{
        that.wait(that)
    }, 2000);
    that.setData({
        timer,
        userInfo
    })

  },
  wait(that){
    let userInfo = that.data.userInfo;
    if(!userInfo){
       return; 
    }
    wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/ivCarRequirement' + 
        '?userId='+userInfo.userId,
        data: {
        },
        method: 'POST',
        success(res) {
            // 0-7 进等待 8进终端详情
            if(res?.data?.result?.requirementStep>=0 && res?.data?.result?.requirementStep<=7 
                // || that.data.count < 3
            ){
                that.setData({
                    chargePileId: res?.data?.result?.terminalId,
                    // chargePileId: res?.data?.result?.terminalId,
                    // count: that.data.count + 1
                })
                // 等待
                console.log(new Date() + " - requirementStep:" + res?.data?.result?.requirementStep)
            }else if(res?.data?.result?.requirementStep==8 
                // || that.data.count>3
                ){
                that.setData({
                    chargePileId: res?.data?.result?.terminalId,
                    // chargePileId: res?.data?.result?.terminalId,
                    // count: that.data.count + 1
                })
                // 8进终端详情
                clearInterval(that.data.timer)
                that.gotoScan_result(that)
            }else{
                that.gotoIndex()
            }
            
        },
        fail(e) {
            getApp().showNetworkError();
        }
    });
  },
gotoIndex(){
    let url = `/pages/index/index`;
    wx.reLaunch({
        url
      });
},
gotoScan_result(that){
    let chargPileId = that.data.chargePileId
    let userInfo = that.data.userInfo
    wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargPile',
        // data: charePileId,
        data:{
          chargPileId,
          userId: userInfo.userId
        },
        method: 'POST',
        success(res) {
          //console.log(res.data);
          // log.info('[首页]', '[获取微信扫一扫充电桩]', '[响应]', res.data);
          if (!res.data || res.data.code == 500) {
            //没有该充电桩信息
            wx.showModal({
              showCancel: false,
              content: res && res.data && res.data.msg ? res.data.msg :'无效的终端编号',
              confirmColor:'#00AADD',
            });
          } else {
            res.data = res.data.result
            //充电状态0：离线、1：故障、2：空闲中、3：充电中、4：欠压故障、5：过压故障、6：过电流故障、8：预约、9：在线升级、10：操作中、11:已插枪等
            if (!res.data.chargstatus || res.data.chargstatus == 0 || res.data.chargstatus == 1 || res.data.chargstatus == 4 || res.data.chargstatus == 5 || res.data.chargstatus == 6 || res.data.chargstatus == 9) {
              //充电桩故障

              wx.showModal({
                showCancel: false,
                content: '终端故障，维修中',
                confirmColor:'#00AADD',
              });
            } else if (res.data.chargstatus == 3 || res.data.chargstatus == 8 || res.data.chargstatus == 10) {
              wx.showModal({
                showCancel: false,
                content: '正在充电中',
                confirmColor:'#00AADD',
              });
            } else if (res.data.chargstatus == 2 || res.data.chargstatus == 11) {
              wx.setStorage({
                key: "scan_chargpile",
                data: res.data
              });
            //  log.info('[首页]', '[微信扫一扫充电桩空闲跳转去充电界面]');
              let url = `/pages/scan_result/scan_result`;
              wx.reLaunch({
                url
              });
            }
          }
        },
        fail(e) {
          getApp().showNetworkError();
        }
      });
},


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})