// pages/reservation/reservation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeList:[],
    chargStationId:'',
    chargPileName:'加载中',
    chargPileId:'',
    chargPileIdId:'',
    userId:'',
    chargStationName:'',
    earliestReservTime:'',
    advanceReservDays:'',
    minReservTime:'',
    maxReservTime:'',
    maxEarlyExerciseTime:'',
    maxReservOvertime:'',
    current:[],
    second:[],
    three:[],
    four:[],
    dayIndex:1,
    dayFlag1:'',
    dayFlag2:'',
    dayFlag3:'',
    dayFlag4:'',
    reservationBtn: false,
    breakPunishCount: 0
  },
  goReservation(){
    let {
      brandName,
      chargStationId,
      chargPileName,
      chargPileId,
      chargPileIdId,
      chargStationName,
      userId,
      earliestReservTime,
      advanceReservDays,
      minReservTime,
      maxReservTime,
      maxEarlyExerciseTime,
      maxReservOvertime
    } = this.data;

    let url = `/pages/reservation/reservationEdit?chargStationId=` + chargStationId + `&chargPileName=` + chargPileName + 
    `&chargPileId=` + chargPileId + 
    `&chargPileIdId=` + chargPileIdId + 
    "&chargStationName=" + chargStationName + 
    "&earliestReservTime=" + earliestReservTime + 
    "&advanceReservDays=" + advanceReservDays+ 
    "&minReservTime=" + minReservTime + 
    "&maxReservTime=" + maxReservTime +
    "&maxEarlyExerciseTime=" + maxEarlyExerciseTime + 
    "&maxReservOvertime=" + maxReservOvertime + 
    "&userId=" + userId+ 
    "&brandName=" + brandName;
    wx.navigateTo({
      url
    });
  },
  switchDay(e){
    let dayIndex = e.currentTarget.dataset.idx;
    let timeList = [];
    if(dayIndex==1){
      timeList = this.data.current;
    }else if(dayIndex==2){
      timeList = this.data.second;
    }else if(dayIndex==3){
      timeList = this.data.three;
    }else if(dayIndex==4){
      timeList = this.data.four;
    };
    this.setData({
      dayIndex,
      timeList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    let {
      brandName,
      chargStationId,
      chargPileName,
      chargPileId,
      chargPileIdId,
      userId,
      chargStationName
    } = options;

    this.setData({
        brandName,
        chargStationId,
        chargPileName,
        chargPileId,
        chargPileIdId,
        userId,
        chargStationName
      }
    )


  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    let {
      chargStationId,
      chargPileName,
      chargPileId,
      chargPileIdId,
      userId,
      chargStationName
    } = this.data;
    wx.request({
      url: getApp().globalData.postHeadAgreement +'/restapi/wechat/ReservOrder?userId=' + userId + '&pileId=' + chargPileIdId + '&stationId=' +  chargStationId + '&reservPileNo=' +chargPileId ,
      data: {
      },
      method: 'POST',
      success(res) {
        if(res?.data?.result?.reservationConfig){
          that.setData({
            reservationBtn: true
          })
        }

        let earliestReservTime = res?.data?.result?.reservationConfig?.earliestReservTime
        earliestReservTime = earliestReservTime?earliestReservTime:0;
        let advanceReservDays = res?.data?.result?.reservationConfig?.advanceReservDays
        advanceReservDays = advanceReservDays?advanceReservDays:0;
        let minReservTime = res?.data?.result?.reservationConfig?.minReservTime
        minReservTime = minReservTime?minReservTime:0;
        let maxReservTime = res?.data?.result?.reservationConfig?.maxReservTime
        maxReservTime = maxReservTime?maxReservTime:0;
        let maxEarlyExerciseTime = res?.data?.result?.reservationConfig?.maxEarlyExerciseTime
        maxEarlyExerciseTime = maxEarlyExerciseTime?maxEarlyExerciseTime:0;
        let maxReservOvertime = res?.data?.result?.reservationConfig?.maxReservOvertime
        maxReservOvertime = maxReservOvertime?maxReservOvertime:0;
        let breakPunishCount = res?.data?.result?.reservationConfig?.breakPunishCount
        breakPunishCount = breakPunishCount?breakPunishCount:0;



        let nowZero = new Date();
        nowZero.setMilliseconds(0)
        nowZero.setSeconds(0)
        nowZero.setMinutes(0)
        nowZero.setHours(0)
        let currentStart = new Date(nowZero)
        let currentEnd =  new Date(nowZero.setDate(nowZero.getDate() + 1 ));
        let secondStart = new Date(currentEnd)
        let secondEnd =  new Date(nowZero.setDate(nowZero.getDate() + 1 ));
        let threeStart = new Date(secondEnd)
        let threeEnd =  new Date(nowZero.setDate(nowZero.getDate() + 1 ));
        let fourStart = new Date(threeEnd);
        let fourEnd =  new Date(nowZero.setDate(nowZero.getDate() + 1 ));

        let current = res?.data?.result?.current;
        current.forEach(series => {
          series.starttime = series.reservChargeStartTime.substr(11,5);
          series.endtime = series.reservChargeEndTime.substr(11,5);
          let reservChargeStartTime = new Date(series.reservChargeStartTime);
          let reservChargeEndTime = new Date(series.reservChargeEndTime);
          if(currentStart > reservChargeStartTime){
            series.starttime =  "(前日) " + series.starttime
          }
          if(currentEnd < reservChargeEndTime){
            series.endtime =  series.endtime + " (次日)"
          }
        });
        current.sort((a,b)=>{
          return new Date(a.reservChargeStartTime) - new Date(b.reservChargeStartTime)
        })
        let second = res?.data?.result?.second;
        second.forEach(series => {
          series.starttime = series.reservChargeStartTime.substr(11,5);
          series.endtime = series.reservChargeEndTime.substr(11,5);
          let reservChargeStartTime = new Date(series.reservChargeStartTime);
          let reservChargeEndTime = new Date(series.reservChargeEndTime);
          if(secondStart > reservChargeStartTime){
            series.starttime =  "(前日) " + series.starttime
          }
          if(secondEnd < reservChargeEndTime){
            series.endtime =  series.endtime + " (次日)"
          }
        });
        second.sort((a,b)=>{
          return new Date(a.reservChargeStartTime) - new Date(b.reservChargeStartTime)
        })
        let three = res?.data?.result?.three;
        three.forEach(series => {
          series.starttime = series.reservChargeStartTime.substr(11,5);
          series.endtime = series.reservChargeEndTime.substr(11,5);
          let reservChargeStartTime = new Date(series.reservChargeStartTime);
          let reservChargeEndTime = new Date(series.reservChargeEndTime);
          if(threeStart > reservChargeStartTime){
            series.starttime =  "(前日) " + series.starttime
          }
          if(threeEnd < reservChargeEndTime){
            series.endtime =  series.endtime + " (次日)"
          }
        });
        three.sort((a,b)=>{
          return new Date(a.reservChargeStartTime) - new Date(b.reservChargeStartTime)
        })
        let four = res?.data?.result?.four;
        four.forEach(series => {
          series.starttime = series.reservChargeStartTime.substr(11,5);
          series.endtime = series.reservChargeEndTime.substr(11,5);
          let reservChargeStartTime = new Date(series.reservChargeStartTime);
          let reservChargeEndTime = new Date(series.reservChargeEndTime);
          if(fourStart > reservChargeStartTime){
            series.starttime =  "(前日) " + series.starttime
          }
          if(fourEnd < reservChargeEndTime){
            series.endtime =  series.endtime + " (次日)"
          }
        });
        four.sort((a,b)=>{
          return new Date(a.reservChargeStartTime) - new Date(b.reservChargeStartTime)
        })
        let now = new Date();
        let dayFlag1 = (now.getMonth() + 1) + "." + now.getDate()
        now = new Date(now.setDate(now.getDate() + 1));
        let dayFlag2 = (now.getMonth() + 1) + "." + now.getDate()

        now = new Date(now.setDate(now.getDate() + 1));
        let dayFlag3 =  (now.getMonth() + 1) + "." + now.getDate()

        now = new Date(now.setDate(now.getDate() + 1));
        let dayFlag4 = (now.getMonth() + 1) + "." + now.getDate()

        that.setData({
          earliestReservTime,
          advanceReservDays,
          minReservTime,
          maxReservTime,
          maxEarlyExerciseTime,
          maxReservOvertime,
          breakPunishCount,
          current,
          second,
          three,
          four,
          timeList: current,
          dayFlag1,
          dayFlag2,
          dayFlag3,
          dayFlag4,
          dayIndex:1
        })
      }
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})