<template>
  <div class="baseinfo">
    <form class="baseinfo-box" @submit='commit'>
      <div class="list-input">
        <span>开始时间</span>
        <div class="datebox">
          <picker class="time" v-model="startTime" mode='time' :value='startTime' @change="pickerConfirm($event,'time')">
            <div hover-class='hover' :class="{unset:startTime==''}">{{startTime || '选择时间'}}</div>
          </picker>
          <picker class="date" v-model="startDate" mode='date' :value='startDate' @change="pickerConfirm($event,'date')">
            <div hover-class='hover' :class="{unset:startDate==''}">{{startDate || '选择日期'}}</div>
          </picker>
        </div>
      </div>
      <div class="list-input">
        <span>结束时间</span>
        <div class="datebox">
          <picker class="time" v-model="endTime" mode='time' :value='endTime' @change="pickerConfirm2($event,'time')">
            <div hover-class='hover' :class="{unset:endTime==''}">{{endTime || '选择时间'}}</div>
          </picker>
          <picker class="date" v-model="endDate" mode='date' :value='endDate' @change="pickerConfirm2($event,'date')">
            <div hover-class='hover' :class="{unset:endDate==''}">{{endDate || '选择日期'}}</div>
          </picker>
        </div>
      </div>
      <div class="list-input">
        <span>请假事由</span>
        <!--<input placeholder="请输入请假事由" v-model="cause" name='cause' :value="cause" @input='cause=service.Input($event)' placeholder-style='color:#9B9B9B'>-->
      </div>
      <div class="longtext">
        <textarea placeholder="请输入请假事由" v-model="cause" name='cause' :value="cause" ></textarea>
      </div>
      <div class="list-input">
        <span>审批人</span>
        <input name=""  value="lawPrincipal" v-model="lawPrincipal" readonly disabled>
      </div>
      <div class='commit'>
        <button form-type='submit' hover-class='fade' :disabled='commiting' bindtap='submit'>提交</button>
      </div>
      <!--<div class='commit'>-->
        <!--<div @click='submit1'>订阅消息</div>-->
      <!--</div>-->
    </form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        startDate: '',
        startTime: '',
        endDate: '',
        endTime: '',
        cause: '',
        lawPrincipal: '',
        approverId : '',
        commiting: false
      }
    },
    methods: {
      commit(e){
        if(this.commiting) return
        console.log(`${this.startDate} ${this.startTime}:00`)
        let inputs = this.service.filter(e.mp.detail.value)
        if(!this.service.checkEmptyInput([this.startDate,this.startTime,this.startDate,this.endTime,this.cause],['开始日期','开始时间','结束日期','结束时间','请假事由'])){
          return
        }
        if(!this.service.checkBadWords([this.cause],['请假事由'])){
          return
        }
        wx.showModal({
          title:'',
          content:'确定要提交吗?',
          success: res => {
            if(res.confirm){
              this.execCommit(e)
            }
          }
        })
      },
      execCommit(e){
        console.log(this.approverId)
        this.commiting = true
        const startDate=this.startDate+' '+this.startTime+':00'
        const endDate=this.endDate + ' '+this.endTime+':00'
        wx.showLoading({ title: '正在提交' })
        wx.request({
          url: this.rootUrl + 'leave/add',
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            startDate: startDate ,
            endDate:endDate ,
            cause: this.cause,
            Approver_id: this.approverId,
            sessionID: wx.getStorageSync('sessionID')
          },
          success: res => {
            console.log(res)
            if (res.statusCode == '500') {
              this.service.getUnionId(this.rootAvatar, this.rootUrl).then(res => this.submit_exec(content))
            } else {
              wx.hideLoading()
              if (res.data === 1) {
                wx.showModal({
                  title: '提交成功',
                  content: '',
                  showCancel: false
                })
                this.text = ''
                wx.hideLoading()
                this.toHomelist()
              } else {
                wx.showToast({ title: '提交失败', icon: 'none' })
              }
              this.commiting = false
            }
          }
        })
      },
      //回到首页
      toHomelist: function () {
        wx.navigateBack({
          url: '../leaveList/main'
          // delta: 1
        })
        // wx.redirectTo({ url: '../leaveList/main?Refresh=true' })
        // const pages = getCurrentPages()
        // pages[pages.length -2 ].onload()
        // pages[pages.length -2 ].refresh()
      },
      pickerConfirm(e, s) {
        switch (s) {
          case 'time': this.startTime = e.mp.detail.value;break
          case 'date': this.startDate = e.mp.detail.value;break
        }
      },
      pickerConfirm2(e, s) {
        switch (s) {
          case 'time': this.endTime = e.mp.detail.value;break
          case 'date': this.endDate = e.mp.detail.value;break
        }
      },
      // submit1 (){
      //   wx.requestSubscribeMessage({
      //     tmplIds: ['N7I9WVwRXIRzAUOG6aVK7_Zo2EVJFCKBYhqsWip-T8g'],
      //     success (res) {
      //       if (res['N7I9WVwRXIRzAUOG6aVK7_Zo2EVJFCKBYhqsWip-T8g'] === 'accept'){
      //         wx.showToast({
      //           title: '订阅OK！',
      //           duration: 1000,
      //           success(data) {
      //             console.log(res)
      //           }
      //         })
      //       }
      //     },
      //     fail (res) {
      //       console.log(res)
      //     }
      //   })
      // },
      loadArticles () {
       this.getlaw()
      },
      getlaw(){
        wx.showLoading({ title: '正在加载' })
        wx.request({
          url: this.rootUrl + 'leave/getLawPrincipal',
          method: 'get',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            sessionID: wx.getStorageSync('sessionID')
          },
          success: res => {
            if (res.statusCode == '500') {
              // this.service.getUnionId(this.rootAvatar, this.rootUrl).then(res => this.submit_exec(content))
            } else {
              console.log(res.data)
              this.lawPrincipal = res.data.name
              this.approverId = res.data.liid
              console.log(this.lawPrincipal)
              wx.hideLoading()
            }
          }
        })
      }
    },
    onLoad(){
      this.getlaw()
    },
    onShow() {
      this.loadArticles()
    },
    onUnload(){
      this.lawPrincipal = ''
      this.approverId = ''
      this.loaded = false
      this.startTime = ''
      this.startDate = ''
      this.endTime = ''
      this.endDate = ''
    }
  }
</script>
<style lang="stylus" scoped>
  .baseinfo
    position absolute
    Height_Width(100%)
    Background()
    .baseinfo-box
      Background()
    div
      text-align right
  .datebox
    margin-right 0 !important
    width 100%
  .date
    float right
    width 180rpx
  .time
    float right
    width 120rpx
  .longtext
    margin-top -25rpx
    width 100%
    padding 25rpx
    box-sizing border-box
    background-color #fff
    border-bottom 1rpx solid #eee
  textarea
    width 100%
    height 170rpx
    padding 10rpx
    box-sizing border-box
    background-color #eee
    border-radius 10rpx
    font-size 30rpx
    line-height 50rpx
    text-align left !important
</style>

