// pages/set_time/set_time.js
import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js'
import { VacationSetSave, VacationList } from '../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, saveUserInfo, getWxloginCode, checkStatus } = getApp()
const { baseImgUrl, thumbnail } = globalData
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelectTime:true,
    startDate:'',
    endDate:'',
    btnIndex:0,
    hiddenModel:false,
    list:[
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {type}=options
    wx.setNavigationBarTitle({
      title: `${type == 1 ? '选择寒假放假时间' : type == 2 ? '选择五一放假时间' : type == 3 ? '选择暑假放假时间' :'选择国庆放假时间'}`,
    })
    console.log(options)
    this.setData(options)
    this.selectDay()
  },

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

  },

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

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      console.log('监听页面隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('监听页面卸载')
  },

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

  },

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

  },

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

  },
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false})
  },
  /**
   * 提示框弹出
   */
  childShowModel: function () {
    this.setData({ hiddenModel: true })
  }, 
  /**
 * 设置节假日时间
 */
  sureVacationSetSave: function (cb) {
    const continuousFn = { fn: this.sureVacationSetSave, param: { ...arguments } }
    const { startDate, endDate, setState,id,type } = this.data;
    // 判断不能超出七天
    const startTimestamp = new Date(startDate).getTime();
    const endTimestamp = new Date(endDate).getTime();
    if (type==2||type==4){
      if ((endTimestamp - startTimestamp) > 7 * 24 * 60 * 60 * 1000) {
        wx.showToast({
          title: '假期范围不能超过一周',
          icon: 'none'
        })
        return;
      } 
    }
     if ((endTimestamp - startTimestamp) <0){
      wx.showToast({
        title: '结束时间不能早于开始时间',
        icon: 'none'
      })
      return;
    }
    console.log(startDate, endDate,setState,id)
    VacationSetSave({ data: { startDate, endDate, setState:Number(setState), id }, continuousFn }).then(res => {
      wx.showToast({
        title: '保存成功',
        icon: 'none'
      })
      wx.navigateBack({})
      // this.setData({ isSelectTime:true})
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
   /**
 * 设置开关
 */
  selectDay:function(){
    const { startDate, endDate,list,btnIndex,id, type, istype,time,index }=this.data;
    // var temp = {};
    // temp[`startDate`] = list[index].startDate;
    // temp[`endDate`] = list[index].endDate;
    // this.setData(temp)
    const today = new Date().getTime();
    if(time){
      var start = new Date(time).getTime();
    }else{
      var start = new Date().getTime();
    }
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    if(btnIndex==1){
      var year = date.getFullYear()+1;
    }else{
      var year = date.getFullYear();
    }
    if (istype == 1 && (start - today) < 0 || (start - today)==0&&!time){
      
      if(type==1){
        this.setData({ 
          star: year + '-' + '01' + '-' +'01',
          end: year + '-' + '02' + '-' + 28,
        })
      } else if (type == 2){
        this.setData({
          star: year + '-' + '04' + '-' + '01',
          end: year + '-' + '05' + '-' + 31,
        })
      } else if (type == 3) {
        this.setData({
          star: year + '-' + '07' + '-' + '01',
          end: year + '-' + '08' + '-' + 31,
        })
      } else if (type == 4) {
        this.setData({
          star: year + '-' + '09' + '-' + '01',
          end: year + '-' + 10 + '-' + 31,
        })
      }
      this.setData({setState: istype == 1 ? '2' : '1'})
    }else{
      this.setData({setState: istype == 1 ? '2' : '1'})
      this.sureVacationSetSave()
    }
    
  },
   /**
 * 选择开始时间
 */
  selectStartDate: function (e) {
    const { endDate} = this.data;
    const end = new Date(endDate).getTime();
    const today = new Date().getTime();
    const start = new Date(e.detail.value).getTime();
    if ((start - today) < 0) {
      wx.showToast({
        title: '开始时间不能早于今天',
        icon: 'none'
      })
      return;
    }
    if ((start-end) >0) {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
      return;
    }
    this.setData({
      startDate: e.detail.value
    })
  },
  /**
* 选择结束时间
*/
  selectEndDate: function (e) {
    const { startDate} = this.data;
    const start = new Date(startDate).getTime();
    const end = new Date(e.detail.value).getTime();
    if ((end - start) < 0) {
      wx.showToast({
        title: '结束时间不能早于开始时间',
        icon: 'none'
      })
      return;
    }
    this.setData({
      endDate: e.detail.value
    })
  },
})