<template>
  <el-dialog
    class="modify-scan-task"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="110px"
    >
      <el-form-item label="学期：">
        <semester-select
          v-model="modalForm.semesterId"
          placeholder="学期"
        ></semester-select>
      </el-form-item>
      <el-form-item label="档案：">
        <archives-select
          v-model="modalForm.paperArchivesId"
          :semester-id="modalForm.semesterId"
          semester-strict
          placeholder="档案"
        >
        </archives-select>
      </el-form-item>
      <el-form-item label="课程代码：" prop="courseCode">
        <el-input
          v-model.trim="modalForm.courseCode"
          placeholder="请输入课程代码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="课程名称：" prop="courseName">
        <el-input
          v-model.trim="modalForm.courseName"
          placeholder="请输入课程名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="scanTaskName" label="任务名称：">
        <el-input
          v-model.trim="modalForm.scanTaskName"
          placeholder="请输入任务名称"
          clearable
        ></el-input>
      </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 { updateScanTask } from "../api";

const initModalForm = {
  id: "",
  semesterId: "",
  paperArchivesId: "",
  scanTaskName: "",
  courseCode: "",
  courseName: ""
};

export default {
  name: "modify-scan-task",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "任务";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        courseCode: [
          {
            required: true,
            message: "请输入科目代码",
            trigger: "change"
          },
          {
            max: 30,
            message: "科目代码不能超过30字符",
            trigger: "change"
          }
        ],
        courseName: [
          {
            required: true,
            message: "请输入科目名称",
            trigger: "change"
          },
          {
            max: 50,
            message: "科目名称不能超过50字符",
            trigger: "change"
          }
        ],
        scanTaskName: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "change"
          },
          {
            max: 50,
            message: "任务名称不能超过50字符",
            trigger: "change"
          }
        ],
        paperArchivesId: [
          {
            required: true,
            message: "请选择档案",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
        this.$nextTick(() => {
          this.$refs.modalFormComp.clearValidate();
        });
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    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;

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

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