<template>
  <el-dialog
    class="modify-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10px"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    destroy-on-close
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="80px"
    >
      <el-form-item prop="semesterId" label="学期：">
        <semester-select
          v-model="modalForm.semesterId"
          placeholder="学期"
        ></semester-select>
      </el-form-item>
      <el-form-item prop="examId" label="考试：">
        <exam-select
          v-model="modalForm.examId"
          :semester-id="modalForm.semesterId"
          placeholder="考试"
        ></exam-select>
      </el-form-item>
      <el-form-item prop="courseCode" label="课程：">
        <course-select
          v-model.trim="modalForm.courseCode"
          :semester-id="modalForm.semesterId"
          :exam-id="modalForm.examId"
          placeholder="课程"
          clearable
        ></course-select>
      </el-form-item>
      <el-form-item prop="examStudentName" label="姓名：">
        <el-input
          v-model.trim="modalForm.examStudentName"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="studentCode" label="学号：">
        <el-input
          v-model.trim="modalForm.studentCode"
          placeholder="请输入学号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="学院：">
        <el-input
          v-model.trim="modalForm.college"
          placeholder="学院"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="专业：">
        <el-input
          v-model.trim="modalForm.major"
          placeholder="专业"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="教学班：">
        <el-input
          v-model.trim="modalForm.teachClazz"
          placeholder="教学班"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="teacherName" label="任课老师：">
        <el-input
          v-model.trim="modalForm.teacherName"
          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 { updateExamStudent } from "../api";

const initModalForm = {
  id: null,
  semesterId: "",
  examId: "",
  courseCode: "",
  examStudentName: "",
  studentCode: "",
  college: "",
  major: "",
  teachClazz: "",
  teacherName: "",
};

export default {
  name: "modify-exam-student",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "考生";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        semesterId: [
          {
            required: true,
            message: "请选择学期",
            trigger: "change",
          },
        ],
        examId: [
          {
            required: true,
            message: "请选择考试",
            trigger: "change",
          },
        ],
        courseCode: [
          {
            required: true,
            message: "请选择课程",
            trigger: "change",
          },
        ],
        examStudentName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change",
          },
          {
            message: "姓名不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        studentCode: [
          {
            required: true,
            message: "请输入学号",
            trigger: "change",
          },
          {
            pattern: /^[a-zA-Z0-9]{1,50}$/,
            message: "学号只能由数字、字母组成",
            trigger: "change",
          },
          {
            message: "学号不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        college: [
          {
            required: false,
            message: "学院不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        major: [
          {
            required: false,
            message: "专业不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        teachClazz: [
          {
            required: false,
            message: "教学班不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        teacherName: [
          {
            required: false,
            message: "任课老师不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    updateClazz() {
      this.modalForm.clazzId = "";
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await updateExamStudent(this.modalForm).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

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