<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>基本信息</span>
      </div>
      <el-form ref="form" :model="infoForm" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="studentName">
              <el-input disabled :value="infoForm.studentName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题">
              <el-input type="textarea" disabled :autosize="true" :value="infoForm.title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="审批人">
              <el-input disabled :value="infoForm.handUserName" tyle="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假类型">
              <el-select disabled :value="infoForm.type" style="width: 100%" placeholder="">
                <el-option v-for="dict in dict.type.biz_leave_type" :key="dict.value" :value="dict.value"
                           :label="dict.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="请假开始时间">
              <el-date-picker style="width: 100%" disabled :value="infoForm.startTime" type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss" placeholder=""></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假结束时间">
              <el-date-picker style="width: 100%" disabled :value="infoForm.endTime" type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss" placeholder=""></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="请假原因">
              <el-input disabled :value="infoForm.leaveReason" type="textarea" placeholder="" resize="none"
                        :autosize="{ minRows: 5 }"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="销假时间">
            <!--            <el-input disabled :value="nowDate"/>-->
            <el-date-picker
              v-model="nowDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row class="mb20" type="flex" justify="center" v-if="$attrs.revoke && !$attrs.readOnly">
          <el-button type="primary" @click="handleRevoke">确认销假</el-button>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {cancelLeave, getLeave, getStuLeaveOne} from '@/api/smartSchool/classAdviser/stuLeaveView.js'
import {formatDate} from "@/utils";

export default {
  name: 'StudentLeaveInfo',
  dicts: ['biz_leave_type'],
  props: {
    id: {
      type: [String, Number],
      default: ''
    },
    instanceId: {
      type: [String, Number],
      default: ''
    },
    taskId: {
      type: [String, Number],
      default: ''
    },

  },
  data() {
    const limitStartTime = {
      disabledDate: time => {
        if (this.infoForm.realityEndTime) {
          return Date.parse(time) > Date.parse(this.infoForm.realityEndTime)
        }
        return false
      }
    }
    const limitEndTime = {
      disabledDate: time => {
        if (this.infoForm.realityStartTime) {
          return Date.parse(time) < Date.parse(this.infoForm.realityStartTime)
        }
        return false
      }
    }
    return {
      nowDate: formatDate(new Date()),
      limitEndTime,
      limitStartTime,
      infoForm: {
        revokeTime: new Date()
      },
      rules: {
        realityStartTime: [{required: true, message: '实际开始时间不能为空', trigger: 'change'}],
        realityEndTime: [{required: true, message: '实际结束时间不能为空', trigger: 'change'}]
      }
    }
  },
  created() {
    this.getDetailInfo()
  },
  methods: {
    /** 获取详情 */
    getDetailInfo() {
      getStuLeaveOne(this.id).then(response => {
        // getLeave(this.id).then(response => {
        this.infoForm = response.data
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      })
    },

    /** 销假 */
    handleRevoke() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.infoForm.revokeTime = this.nowDate
          this.$modal.loading('正在销假，请稍等...')
          cancelLeave(this.taskId, this.instanceId, this.infoForm)
            .then(response => {
              this.$modal.closeLoading()
              this.$message.success('销假成功')
              this.$emit('submit')
            })
            .catch(error => {
              this.$modal.closeLoading()
            })
        }
      })
    }
  }
}
</script>
