<template>
  <el-dialog
    class="modify-business-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="550px"
    :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="100px"
    >
      <el-form-item prop="studentName" label="姓名：">
        <el-input
          v-model.trim="modalForm.studentName"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="studentCode" label="学号：">
        <el-input
          v-model.trim="modalForm.studentCode"
          placeholder="请输入学号"
          clearable
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="examDetailCourseId" label="考场科目：">
        <el-select
          v-model="modalForm.examDetailCourseId"
          placeholder="考场科目"
          clearable
          @change="courseChange"
        >
          <el-option
            v-for="item in courses"
            :key="item.examDetailCourseId"
            :value="item.examDetailCourseId"
            :label="item.courseName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="paperType" label="卷型：">
        <el-select v-model="modalForm.paperType" placeholder="卷型" clearable>
          <el-option
            v-for="item in paperTypes"
            :key="item"
            :value="item"
            :label="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="college" label="学院：">
        <el-input
          v-model.trim="modalForm.college"
          placeholder="请输入学院"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="majorName" label="专业：">
        <el-input
          v-model.trim="modalForm.majorName"
          placeholder="请输入专业"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="className" label="班级：">
        <el-input
          v-model.trim="modalForm.className"
          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 { businessAddStudent, queryBusinessCoursePaperType } from "../api";

const initModalForm = {
  examDetailCourseId: "",
  paperType: "",
  studentCode: "",
  studentName: "",
  college: "",
  majorName: "",
  className: "",
};

export default {
  name: "modify-course",
  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: {
        studentName: [
          {
            required: true,
            message: "姓名不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        studentCode: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z_-]{3,30}$/,
            message: "学号只能由数字字母短横线组成，长度在3-30之间",
            trigger: "change",
          },
        ],
        examDetailCourseId: [
          {
            required: true,
            message: "请选择考场科目",
            trigger: "change",
          },
        ],
        paperType: [
          {
            required: true,
            message: "请选择卷型",
            trigger: "change",
          },
        ],
        college: [
          {
            required: true,
            message: "学院不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        majorName: [
          {
            required: true,
            message: "专业不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        className: [
          {
            required: true,
            message: "班级不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
      },
      courses: [],
      paperTypes: [],
    };
  },
  watch: {
    "instance.examDetailId": {
      immediate: true,
      handler() {
        this.getCourses();
      },
    },
  },
  methods: {
    initData(val) {
      this.modalForm = { ...initModalForm };
    },
    async getCourses() {
      if (!this.instance.examDetailId) return;
      const res = await queryBusinessCoursePaperType({
        examDetailId: this.instance.examDetailId,
      });
      this.courses = res || [];
    },
    courseChange() {
      this.paperTypes = [];
      this.modalForm.paperType = "";
      const curCourse = this.courses.find(
        (item) => item.examDetailCourseId === this.modalForm.examDetailCourseId
      );
      if (!curCourse) return;
      this.paperTypes = curCourse.paperTypes;
    },
    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;
      let datas = { ...this.modalForm };
      const data = await businessAddStudent(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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