// pages/scan_result/elpackage.js
let log = require('../../utils/log.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    progressPercentage: 100, // 初始进度值为50%
    // chargPile: null,
    chargPile: {

    },
    parkObj:{},
    eleList:{},
    orderid: null,
    userInfo:{},
    userId: null,
    repaidbalance: 0,
    resultTable: [],
    selectEleIndex : -1,
    // 是否减免停车费
    carParkingRate: false,
    // 停车时间
    parkTime: 0,
    url:'/images/scan_result.png',
    authStatus: 1,
    defPlateNumber:'',
    car_active: false,
    hid:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    let userInfo = wx.getStorageSync('userInfo');
    that.setData({
        userInfo
    })


    wx.getStorage({
      key: 'elpackage',
      success: function (res) {
        console.log(res);
        console.log(res.data);
        let chargPile = res.data.chargStation;
        let eleList = res.data.chargPileList; 
        let parkObj = res.data

        eleList.forEach((key)=>{
            if(!key.currentSoc){
                key.currentSoc = 0
            }
            key['active'] = true
        })


        that.setData({
            chargPile,
            eleList,
            parkObj
        })

        // 真机诡异的BUG(微信小程序真机)
        // 直接设置为false 会 导致界面上的电池量不显示(这是因为真机会在css之前加上两个横线--，但是直接在css中写这两个横线会导致编译报错)
        // 这里折中先设置为true，然后在定时器中设置为false 刷新一下
        setTimeout(()=>{
            eleList.forEach((key)=>{
                key['active'] = false
            })
            that.setData({
                eleList
            })
        }, 250)

        setTimeout(()=>{
            that.setData({
                hid:true
            })
        }, 300)

        // 更新是否减停车费



        wx.removeStorage({
          key: 'elpackage',
          success(res) {
            console.log(res);
          }
        });

      }
    });
  },

  selectEle(e){
    let idx = e.currentTarget.dataset.idx;

    let that = this;
    let eleList = that.data.eleList;
    let car_active = that.data.car_active;
    if(eleList[idx].active){
        eleList[idx].active = false;
        idx = -1
    }else{
        eleList[idx].active = true;
        if(that.data.selectEleIndex>=0){
            eleList[that.data.selectEleIndex].active = false;
        }

    }
    car_active = (idx>=0)
    that.setData({
        selectEleIndex: idx,
        eleList,
        car_active
    })
    
  },
  callCar(){
    let that = this
    let car_active = that.data.car_active;

    if(!car_active){
        return;
    }

    let ivParkPointId = that.data.parkObj.ivParkPointId 
    let chargePileId = that.data.eleList[that.data.selectEleIndex].id
    let userId = that.data.userInfo.userId

    wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/callCarRequirement?'
        + 'ivParkPointId=' + ivParkPointId
        + '&chargePileId=' + chargePileId
        + '&userId=' + userId
        + '&requirementType=0',
        data: {
        },
        method: 'POST',
        success: function (res) {
            if( res.data.code ==200){
                // 走你
                let url = `/pages/batteryPack/batteryPackWait`;
                wx.navigateTo({
                  url
                });
            }else{
                // 给出提示
                wx.showModal({
                    showCancel: false,
                    content: "调度失败" + res.data?.msg,
                    confirmColor:'#00AADD',
                  });
            }
        },
        fail(e) {
          console.log("呼叫充电车失败");
        }
  });



  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let car = wx.getStorageSync('chooseCar_plateNumber')
    let plateNumber = ''
    if(car){
        plateNumber = car.plateNumber;
        wx.removeStorageSync('chooseCar_plateNumber')
    }
    this.setData({
        plateNumber
    })
    console.info('将会自动跳过弹窗,默认车牌号:' + plateNumber)

    if(car){
        this.beginChargeInner(this,plateNumber)
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})