
var app = getApp()
var api = require('../../utils/api.js');
Page({
  data: {
    date: '',
    typeEnum: [
      '短假', '长假'
    ],
    selectedType: 0
  },
  onLoad: function () {
    let date = this.getNowFormatDate();
    this.setData({
      date: date
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      selectedType: e.detail.value
    })
  },
  getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if(month >= 1 && month <= 9) {
      month = "0" + month;
    }
        if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  formSubmit: function (e) {
    let value = e.detail.value
    value.days = parseInt(value.days)
    if(value.type == 0) {
      if (value.days <= 0 || value.days > 7) {
        wx.showModal({
          title: '请假失败',
          content: '短假可请天数为1到7天',
        })
        return false;
      }
    } else {
      if (!(value.days >= 7 && value.days <= 60)) {
        wx.showModal({
          title: '请假失败',
          content: '长假可请天数为7到60天',
        })
        return false;
      }
    }
    wx.request({
      url: api.applyLeaveUrl,
      method: 'GET',
      data: {
        'student_id': wx.getStorageSync('pt_student').id,
        'date': value.date,
        'type': value.type == 0 ? '1' : '2',
        'days': value.days,
        'remark': value.remark
      },
      success: res => {
        if (res.data.status == 'success') {
          wx.showModal({
            title: '请假成功',
            content: '请假成功',
            success: function (res) {
              wx.switchTab({
                url: '/pages/userinfo/userinfo',
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.info,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  }
})
