<template>
  <el-dialog
    class="modify-template"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :rules="rules"
      :model="modalForm"
      label-width="110px"
    >
      <el-form-item prop="semesterId" label="学期：">
        <semester-select
          v-model="modalForm.semesterId"
          class="width-400"
          @change="semesterChange"
        ></semester-select>
      </el-form-item>
      <el-form-item prop="examTypeId" label="考试类型：">
        <exam-type-select
          v-model="modalForm.examTypeId"
          class="width-400"
          @change="examTypeChange"
        ></exam-type-select>
      </el-form-item>
      <el-form-item prop="startTime" label="定时任务时间：">
        <el-date-picker
          v-model="createTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="定时任务开始时间"
          end-placeholder="定时任务结束时间"
          value-format="timestamp"
          align="right"
          unlink-panels
          @change="dateChange"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateTimeTask } from "../api";
import pickerOptions from "@/constants/datePickerOptions";

const initModalForm = {
  id: null,
  semesterId: null,
  semesterName: null,
  examTypeId: null,
  examTypeName: null,
  startTime: "",
  endTime: "",
};

export default {
  name: "modify-task",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "定时任务";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      createTime: [],
      pickerOptions,
      rules: {
        semesterId: [
          {
            required: true,
            message: "请选择学期",
            trigger: "change",
          },
        ],
        examTypeId: [
          {
            required: true,
            message: "请选择考试",
            trigger: "change",
          },
        ],
        startTime: [
          {
            required: true,
            message: "请选择学校",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
        this.createTime = [this.modalForm.startTime, this.modalForm.endTime];
      } else {
        this.modalForm = { ...initModalForm };
        this.createTime = [];
      }
    },
    visibleChange() {
      this.initData(this.instance);

      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    dateChange() {
      if (this.createTime) {
        this.modalForm.startTime = this.createTime[0];
        this.modalForm.endTime = this.createTime[1];
      } else {
        this.modalForm.startTime = "";
        this.modalForm.endTime = "";
      }
    },
    semesterChange(val) {
      this.modalForm.semesterName = val.name;
    },
    examTypeChange(val) {
      this.modalForm.examTypeName = val.name;
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = {
        ...this.modalForm,
      };
      const data = await updateTimeTask(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success("保存成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
