<template>
  <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
              disabled
              :value="infoForm.teacherName"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加班天数">
            <el-input
              disabled
              :value="infoForm.days"></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.overtime_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-->
            <!--  disabled-->
            <!--  style="width: 100%;"-->
            <!--  :value="infoForm.startTime"-->
            <!--  type="datetime"-->
            <!--  value-format="yyyy-MM-dd HH:mm:ss"-->
            <!--  placeholder="">-->
            <!--</el-date-picker>-->
            <el-input disabled v-model="infoForm.startTime"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加班结束时间">
            <!--<el-date-picker-->
            <!--  disabled-->
            <!--  style="width: 100%;"-->
            <!--  :value="infoForm.endTime"-->
            <!--  type="datetime"-->
            <!--  value-format="yyyy-MM-dd HH:mm:ss"-->
            <!--  placeholder="">-->
            <!--</el-date-picker>-->
            <el-input disabled v-model="infoForm.endTime"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="加班原因">
            <el-input
              disabled
              :value="infoForm.reason"
              type="textarea"
              resize="none"
              :autosize="{minRows: 5}"
            />
          </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>
  </div>
</template>

<script>
import {
  listTeacherOvertime,
  getTeacherOvertime,
  delTeacherOvertime,
  addTeacherOvertime,
  updateTeacherOvertime,
  listHistory,
  viewSchedule,
  submitApply, cancelLeave
} from "@/api/smartSchool/teacherManage/teacherOvertime";

export default {
  name: "StudentLeaveInfo",
  dicts: ['overtime_type'],
  props: {
    id: {
      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() {
      getTeacherOvertime(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>
