<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="teacherName">
              <el-input readonly :value="infoForm.teacherName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题">
              <el-input readonly :autosize="true" :value="infoForm.title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"></el-col>
          <el-col :span="12">
            <el-form-item label="请假类型">
              <el-select readonly :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" v-if="infoForm.applyType == 1">
          <el-col :span="12">
            <el-form-item label="请假开始时间" prop="startTime">
              <el-date-picker readonly style="width: 100%" v-model="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="请假结束时间" prop="endTime">
              <el-date-picker readonly style="width: 100%" v-model="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 :gutter="20" v-else>
          <!--          教师-->
          <el-col :span="7">
            <el-form-item label="请假开始时间" prop="startTime">
              <el-date-picker readonly v-model="infoForm.startTime" type="date" style="width: 100%"
                value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-select :value="infoForm.startSort" style="width: 100%" placeholder="请选择课程节次">
              <el-option v-for="dict in dict.type.course_arrange" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-col>
          <el-col :offset="2" :span="7">
            <el-form-item label="请假结束时间" prop="endTime">
              <el-date-picker readonly type="date" v-model="infoForm.endTime" value-format="yyyy-MM-dd"
                style="width: 100%" placeholder="选择日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-select :value="infoForm.endSort" style="width: 100%" placeholder="请选择课程节次">
              <el-option v-for="dict in dict.type.course_arrange" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="请假原因">
              <el-input readonly :value="infoForm.reasonForLeave" type="textarea" placeholder=""
                :autosize="{ minRows: 3 }" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="$attrs.revoke">
          <el-col :span="12">
            <el-form-item label="实际开始时间" prop="realityStartTime">
              <el-date-picker style="width: 100%" v-model="infoForm.realityStartTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="" :picker-options="limitStartTime"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际结束时间" prop="realityEndTime">
              <el-date-picker style="width: 100%" v-model="infoForm.realityEndTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="" :picker-options="limitEndTime"></el-date-picker>
            </el-form-item>
          </el-col>
        </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" :taskId="this.$attrs.taskId" :instanceId="instanceId" :id="id"
      v-on="this.$listeners"></TaskResolve>
  </div>
</template>

<script>
import {
  // listTeacherLeave,
  getTeacherLeave,
  // delTeacherLeave,
  // addTeacherLeave,
  // updateTeacherLeave,
  // listHistory,
  // viewSchedule,
  cancelLeave
} from "@/api/smartSchool/teacherManage/teacherLeave";
import TaskResolve from "./TaskResolve";

export default {
  name: 'StudentLeaveInfo',
  dicts: ['biz_leave_type', 'course_arrange'],
  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,
      infoForm: {},
      rules: {
        realityStartTime: [{ required: true, message: '实际开始时间不能为空', trigger: 'change' }],
        realityEndTime: [{ required: true, message: '实际结束时间不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDetailInfo()
  },
  methods: {
    /** 获取详情 */
    getDetailInfo() {
      getTeacherLeave(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('正在销假，请稍等...')
          cancelLeave(this.$attrs.taskId, this.infoForm)
            .then(response => {
              this.$modal.closeLoading()
              this.$message.success('销假成功')
              this.$emit('submit')
            })
            .catch(error => {
              this.$modal.closeLoading()
            })
        }
      })
    }
  }
}
</script>

<style scoped></style>
