let Util = require("../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandName:'',
    chargStationId:'',
    chargPileName:'加载中',
    chargPileId:'',
    chargPileIdId:'',
    userId:'',
    plateNumber:'',
    chargStationName:'',
    startparams:{
      placeholder: '请选择时间', 
      startDateTime: '2020-01-01 00:00',
      endDateTime: '2040-01-01 00:00',
      // pText: Util.formatTimePicker(new Date()) //'2022-04-30 00:00'
      pText: '' //'2022-04-30 00:00'

    },
    finishparams:{
      placeholder: '请选择时间', 
      startDateTime: '2020-01-01 00:00',
      endDateTime: '2040-01-01 00:00',
      // pText: Util.formatTimePicker(new Date()) //'2022-04-30 00:00'
      pText: ''
    },
  },
  bindStartparamsChange(e){
    let startparams =  this.data.startparams;
    let endparams =  this.data.finishparams;
    startparams.pText = e.detail
    this.setData({
      startparams:startparams
    })
    let currTime = new Date(e.detail);

    console.info(currTime)
    let curTime3 = new Date(currTime);
    let estime = new Date(curTime3.setMinutes(curTime3.getMinutes() + parseInt(this.data.minReservTime)));
    let curTime4 = new Date(currTime);
    let eetime = new Date(curTime4.setMinutes(curTime4.getMinutes() + parseInt(this.data.maxReservTime)));

    if(endparams?.pText){
      let currEndTime = new Date(endparams.pText);
      if(currEndTime<=currTime){
        this.selectComponent("#end").changeDateTime('')
        // this.selectComponent("#end").initData()
      }
    }

    this.selectComponent("#end").data.startDateTime = Util.formatTimePicker(estime);
    this.selectComponent("#end").data.endDateTime = Util.formatTimePicker(eetime),
    this.selectComponent("#end").initData()

  
  },

  bindFinishparamsChange(e){
    let startparams =  this.data.startparams;
    let finishparams =  this.data.finishparams;
    finishparams.pText = e.detail
    this.setData({
      finishparams:finishparams
    })
    console.info("finishparams " + finishparams.pText)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initCar();

      let {
        brandName,
        chargStationId,
        chargPileName,
        chargPileId,
        chargPileIdId,
        userId,
        chargStationName,
        earliestReservTime,
        advanceReservDays,
        minReservTime,
        maxReservTime,
        maxEarlyExerciseTime,
        maxReservOvertime
      } = options;
  
      console.info(options);
      if(options.brandName == 'null' || options.brandName == ''){
        brandName = null
      }
      let curTime = new Date();
      let sstime = new Date(curTime.setMinutes(curTime.getMinutes() + parseInt(earliestReservTime)));
      let curTime2 = new Date();
      let setime = new Date(curTime2.setMinutes(curTime2.getMinutes() + (parseInt(advanceReservDays)) * 60 * 24));
      setime.setHours(23, 59, 0)
     
      let curTime3 = new Date(sstime);
      let estime = new Date(curTime3.setMinutes(curTime3.getMinutes() + parseInt(minReservTime)));
      let curTime4 = new Date(setime);
      let eetime = new Date(curTime4.setMinutes(curTime4.getMinutes() + parseInt(maxReservTime)));
      this.setData({
          brandName,
          chargStationId,
          chargPileName,
          chargPileId,
          chargPileIdId,
          userId,
          chargStationName,
          earliestReservTime,
          advanceReservDays,
          minReservTime,
          maxReservTime,
          maxEarlyExerciseTime,
          maxReservOvertime,
          "startparams.startDateTime":Util.formatTimePicker(sstime),
          // "startparams.pText":Util.formatTimePicker(sstime),
          "startparams.endDateTime":Util.formatTimePicker(setime),
          "finishparams.startDateTime":Util.formatTimePicker(estime),
          // "finishparams.pText":Util.formatTimePicker(estime),
          "finishparams.endDateTime":Util.formatTimePicker(eetime),
        }
      );

  },
  SubmitBtn(){
    // 预约充电站ID   * 预约充电桩ID  * 预约用户ID   * 预约充电开始时间  * 预约充电结束时间
    let reservChargeStartTime = this.data.startparams.pText
    let reservChargeEndTime = this.data.finishparams.pText
    let reservPileId = this.data.chargPileIdId;
    let reservStationId = this.data.chargStationId;
    let sysUserId = this.data.userId;
    let plateNumber = this.data.plateNumber;

    if(!plateNumber){
        wx.showModal({
            title: '提示',
            content: '请先选择车牌',
            showCancel:false,
            confirmColor:'#00AADD',
          });
          return;
    }

    if(!reservChargeStartTime){
      wx.showModal({
        title: '提示',
        content: '请先选择预约开始充电时间',
        showCancel:false,
        confirmColor:'#00AADD',
      });
      return;
    }
    reservChargeStartTime = reservChargeStartTime + ':00'
    if(!reservChargeEndTime){
      wx.showModal({
        title: '提示',
        content: '请先选择预约结束充电时间',
        showCancel:false,
        confirmColor:'#00AADD',
      });
      return;
    }
    reservChargeEndTime = reservChargeEndTime + ':00'
    wx.requestSubscribeMessage({
      tmplIds: ['DA80dlHRpvYMfZVICp-SVFEqo6v7_7S9wPM4nndHyjE'],
      success (res) { 
        wx.request({
          // url: getApp().globalData.postHeadAgreement +'/restapi/wechat/reserv2?reservChargeStartTime=' + reservChargeStartTime +
          // '&reservChargeEndTime=' + reservChargeEndTime + 
          // '&reservPile.id=' + reservPileId + 
          // '&reservStation.id=' + reservStationId + 
          // '&sysUser.id=' + sysUserId,
          url: getApp().globalData.postHeadAgreement +'/restapi/wechat/reserv',
          data: {
            reservPlateNum:plateNumber,
            reservChargeStartTime:reservChargeStartTime,
            reservChargeEndTime:reservChargeEndTime,
            reservPile:{
              id:reservPileId
            },
            reservStation:{
              id:reservStationId
            },
            sysUser:{
              userId:sysUserId
            },
          },
          method: 'POST',
          success(res) {
            console.info(res)
            if(res?.data?.msg=="操作成功"){
              wx.showModal({
                title: '提示',
                content: '预约成功',
                showCancel:false,
                confirmColor:'#00AADD',
                complete: (res2) => {
                  if (res2.cancel) {
                    
                  }
              
                  if (res2.confirm) {
                    wx.navigateBack()
                  }
                }
              });
            }else{
              wx.showModal({
                title: '提示',
                content: res?.data?.msg,
                showCancel:false,
                confirmColor:'#00AADD',
              });
            }
          }
        });
      }
    })

  },
  chooseCar(){
    let url = '/pages/ucenter/car/chooseCar'
    wx.setStorageSync('chooseCar_plateNumber',{plateNumber:this.data.plateNumber})
    wx.navigateTo({
      url
    });
  },
  initCar(){
    let that = this
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');

    // 读取默认车牌
    wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carList?loginName=' + userInfo.loginName,
        // data: {
        // loginName: userInfo.loginName
        // },
        method: 'POST',
        success: function (res) {
            let userCarList = res.data;
            userCarList = userCarList.filter(it=>it.defaultType==1);
            console.info(userCarList)
            if(userCarList.length==0 ){
            // wx.showModal({
            //     title: '提示',
            //     content: '当前充电站可减免停车费，是否绑定车牌信息？',
            //     confirmText: '绑定车牌',
            //     cancelText: '直接启动',
            //     showCancel:true,
            //     confirmColor:'#00AADD',
            //     cancelColor:'#00AADD',
            //     success: function (ress1) {
            //     if(ress1.confirm){
            //         let url = '/pages/ucenter/car/chooseCar'
            //         wx.navigateTo({
            //         url
            //         });
            //     }else{
            //         that.beginChargeInner(that,'')
            //     }
            //     }
            // });
            }else{
            let userCar = userCarList[0]
            that.setData({
                plateNumber: userCar.plateNumber
            })
            // wx.showModal({
            //     title: '提示',
            //     content: '确定使用' + userCar.plateNumber + '车牌信息减免停车费？',
            //     confirmText: '确定',
            //     cancelText: '其他车牌',
            //     showCancel:true,
            //     confirmColor:'#00AADD',
            //     cancelColor:'#00AADD',
            //     success: function (ress1) {
            //         if(ress1.confirm){
            //         that.beginChargeInner(that,userCar.plateNumber)
            //         }else{
            //         let url = '/pages/ucenter/car/chooseCar'
            //         wx.navigateTo({
            //             url
            //         });
            //         }
            //         return;
    
            //     },
            //     fail: function(res1){
            //     console.info(res1)
            //     }
            // });
            }
    
        },
        fail(e) {
        console.log("查询车辆失败");
        log.info('[查询车辆]','[fail]', e);
        }
        });
  },



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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let car = wx.getStorageSync('chooseCar_plateNumber')
    console.info(car)
    let plateNumber = ''
    if(car){
        plateNumber = car.plateNumber;
        wx.removeStorageSync('chooseCar_plateNumber')
    }
    this.setData({
        plateNumber
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})