<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 v-if="$attrs.revoke && !$attrs.readOnly">
          <el-form-item label="销假时间">
            <el-date-picker
              v-model="nowTime"
              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>
    <TaskResolve v-if="!$attrs.readOnly && !$attrs.revoke" :taskId="this.$attrs.taskId" :instanceId="instanceId"
                 :id="id" v-on="this.$listeners"></TaskResolve>
  </div>
</template>

<script>
import {getLeave, cancelLeave} from '@/api/smartSchool/indexMould/myTask/studentLeaveApproval'
import TaskResolve from './TaskResolve'
import {formatDate} from "@/utils";

export default {
  name: 'StudentLeaveInfo',
  dicts: ['biz_leave_type'],
  components: {
    TaskResolve
  },
  props: {
    id: {
      type: [String, Number],
      default: ''
    },
    instanceId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    const limitStartTime = {
      disabledDate: time => {
        if (this.infoForm.realityEndTime) {
          return Date.parse(time) > Date.parse(this.infoForm.realityEndTime)
        }
        return false
        // return Date.parse(time) < Date.parse(new Date());
      }
    }
    const limitEndTime = {
      disabledDate: time => {
        if (this.infoForm.realityStartTime) {
          return Date.parse(time) < Date.parse(this.infoForm.realityStartTime)
        }
        return false
      }
    }
    return {
      limitEndTime,
      limitStartTime,
      nowTime: formatDate(new Date()),
      infoForm: {},
      rules: {
        realityStartTime: [{required: true, message: '实际开始时间不能为空', trigger: 'change'}],
        realityEndTime: [{required: true, message: '实际结束时间不能为空', trigger: 'change'}]
      }
    }
  },
  created() {
    this.getDetailInfo()
  },
  methods: {
    /** 获取详情 */
    getDetailInfo() {
      getLeave(this.id).then(response => {
        this.infoForm = response.data
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      })
    },

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

<style scoped></style>
