<template>
  <el-dialog
    class="modify-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    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="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="courseName" label="课程名称：">
        <el-input
          v-model.trim="modalForm.courseName"
          placeholder="请输入课程名称"
          clearable
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="courseCode" label="课程代码：">
        <el-input
          v-model.trim="modalForm.courseCode"
          placeholder="请输入课程代码"
          clearable
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="collegeName" label="学院：">
        <el-input
          v-model.trim="modalForm.collegeName"
          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-item prop="teacher" label="任课老师：">
        <el-input
          v-model.trim="modalForm.teacher"
          placeholder="请输入任课老师"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="teachClass" label="教学班：">
        <el-input
          v-model.trim="modalForm.teachClass"
          placeholder="请输入教学班"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="examRoom" label="考场：">
        <el-input
          v-model.trim="modalForm.examRoom"
          placeholder="请输入考场"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="score" label="成绩：">
        <el-input-number
          v-model="modalForm.score"
          :min="0"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="remark" label="备注：">
        <el-input
          v-model.trim="modalForm.remark"
          placeholder="请输入备注"
          clearable
        ></el-input>
      </el-form-item>
      <!-- 自定义字段 -->
      <template v-if="extendFields.length">
        <el-form-item
          v-for="field in extendFields"
          :key="field.code"
          :label="`${field.name}：`"
        >
          <el-input
            v-model.trim="modalForm[field.code]"
            :placeholder="`请输入${field.name}`"
            clearable
          ></el-input>
        </el-form-item>
      </template>
    </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 { updateStudent } from "../api";

const initModalForm = {
  id: null,
  semesterId: "",
  examId: "",
  courseName: "",
  courseCode: "",
  studentName: "",
  studentCode: "",
  collegeName: "",
  majorName: "",
  className: "",
  teacher: "",
  teachClass: "",
  examRoom: "",
  score: "",
  remark: "",
};

export default {
  name: "modify-student",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
    extendFields: {
      type: Array,
      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: "请输入姓名",
            trigger: "change",
          },
          {
            message: "姓名不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        studentCode: [
          {
            required: true,
            message: "请输入学号",
            trigger: "change",
          },
          {
            message: "学号只能由数字、字母和下划线组成，长度1-30个字符",
            pattern: /^[a-zA-Z0-9_-]{1,30}$/,
            trigger: "change",
          },
        ],
        collegeName: [
          {
            message: "学院不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        majorName: [
          {
            message: "专业不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        className: [
          {
            message: "班级不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        teacher: [
          {
            message: "任课老师不能超过30字符",
            max: 30,
            trigger: "change",
          },
        ],
        teachClass: [
          {
            message: "教学班不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        courseName: [
          {
            required: true,
            message: "请输入课程名称",
            trigger: "change",
          },
          {
            message: "课程名称不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        courseCode: [
          {
            required: true,
            message: "请输入课程代码",
            trigger: "change",
          },
          {
            message: "课程代码不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        examRoom: [
          {
            message: "考场不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
        remark: [
          {
            message: "备注不能超过50字符",
            max: 50,
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      const modalForm = this.$objAssign(initModalForm, val);
      modalForm.score = modalForm.score || undefined;
      if (this.extendFields.length) {
        const extendFieldDict = val.extendFieldDict || {};
        this.extendFields.forEach((field) => {
          modalForm[field.code] = extendFieldDict[field.code] || "";
        });
      }
      this.modalForm = modalForm;
    },
    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.$objAssign(initModalForm, this.modalForm);
      if (this.extendFields.length) {
        const extendFieldsData = this.extendFields.map((field) => {
          return { ...field, value: this.modalForm[field.code] };
        });
        datas.extendFields = JSON.stringify(extendFieldsData);
      }
      const data = await updateStudent(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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