<template>
  <el-dialog
    class="modify-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10px"
    width="540px"
    :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="120px"
    >
      <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-common-select
          v-model="modalForm.courseCode"
          placeholder="课程"
          clearable
        ></course-common-select>
      </el-form-item>
      <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
        ></el-input>
      </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="major" label="专业：">
        <el-input
          v-model.trim="modalForm.major"
          placeholder="请输入专业"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="teachClazz" 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-item prop="teacherCode" label="任课老师工号：">
        <el-input
          v-model.trim="modalForm.teacherCode"
          placeholder="请输入任课老师工号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="paperNumber" label="试卷编号：">
        <el-input
          v-model.trim="modalForm.paperNumber"
          placeholder="请输入试卷编号"
          clearable
          @change="updateRules"
        ></el-input>
      </el-form-item>
      <el-form-item prop="examStartTime" label="考试时间：">
        <el-date-picker
          v-model="createDate"
          type="date"
          value-format="timestamp"
          placeholder="考试日期"
          style="width: 150px; margin-bottom: 5px"
          :editable="false"
          @change="timeChange"
        >
        </el-date-picker>
        <el-time-picker
          v-model="createTime"
          is-range
          range-separator="至"
          start-placeholder="考试开始时间"
          end-placeholder="考试结束时间"
          placeholder="选择时间范围"
          value-format="timestamp"
          :editable="false"
          @change="timeChange"
        >
        </el-time-picker>
      </el-form-item>
      <el-form-item prop="examPlace" label="考点(校区)：">
        <el-input
          v-model.trim="modalForm.examPlace"
          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>
    <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";
import { getTimeDatestamp } from "@/plugins/utils";

const initModalForm = {
  id: null,
  semesterId: "",
  examId: "",
  courseCode: "",
  studentName: "",
  studentCode: "",
  college: "",
  major: "",
  teachClazz: "",
  teacherCode: "",
  teacherName: "",
  paperNumber: "",
  examEndTime: "",
  examStartTime: "",
  examPlace: "",
  examRoom: "",
};

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: {},
      // date-picker
      curCreateTime: [],
      createDate: "",
      createTime: [],
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
        const { examStartTime, examEndTime } = val;
        if (examStartTime && examEndTime) {
          this.createTime = [examStartTime, examEndTime];
          this.createDate = getTimeDatestamp(examStartTime);
          this.modalForm.examStartTime = this.createTime[0];
          this.modalForm.examEndTime = this.createTime[1];
        } else {
          this.getInitCreateTime();
        }
      } else {
        this.modalForm = { ...initModalForm };
        this.getInitCreateTime();
        this.initExamTime();
      }
      this.updateRules();
    },
    getInitCreateTime() {
      const curDate = getTimeDatestamp(Date.now());
      const hour = 60 * 60 * 1000;
      this.createTime = [curDate + 8 * hour, curDate + 10 * hour];
    },
    visibleChange() {
      this.initData(this.instance);
    },
    updateRules() {
      this.rules = {
        semesterId: [
          {
            required: true,
            message: "请选择学期",
            trigger: "change",
          },
        ],
        examId: [
          {
            required: true,
            message: "请选择考试",
            trigger: "change",
          },
        ],
        courseCode: [
          {
            required: true,
            message: "请选择课程",
            trigger: "change",
          },
        ],
        studentName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change",
          },
          {
            message: "姓名不能超过50个字",
            max: 50,
            trigger: "change",
          },
        ],
        studentCode: [
          {
            required: true,
            message: "请输入学号",
            trigger: "change",
          },
          {
            pattern: /^[a-zA-Z0-9-_]{1,30}$/,
            message: "学号只能由数字、字母、短横线和下划线组成",
            trigger: "change",
          },
          {
            message: "学号不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        college: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入学院",
            trigger: "change",
          },
          {
            message: "学院不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        major: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入专业",
            trigger: "change",
          },
          {
            message: "专业不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        teachClazz: [
          {
            required: true,
            message: "请输入教学班",
            trigger: "change",
          },
          {
            message: "教学班不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        teacherName: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入任课老师名称",
            trigger: "change",
          },
          {
            message: "任课老师名称不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        teacherCode: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入任课老师工号",
            trigger: "change",
          },
          {
            message: "任课老师工号不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        paperNumber: [
          {
            message: "只能由数字、字母、短横线和下划线组成",
            pattern: /^[a-zA-Z0-9_].+$/,
            trigger: "change",
          },
          {
            required: false,
            message: "试卷编码不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        examStartTime: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入考试时间",
            trigger: "change",
          },
        ],
        examPlace: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入考点(校区)",
            trigger: "change",
          },
          {
            message: "考点(校区)不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        examRoom: [
          {
            required: !!this.modalForm.paperNumber,
            message: "请输入考场(考试教室)",
            trigger: "change",
          },
          {
            message: "考场(考试教室)不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
      };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    updateClazz() {
      this.modalForm.clazzId = "";
    },
    initExamTime() {
      const curDate = getTimeDatestamp(Date.now());
      const hour = 60 * 60 * 1000;
      this.curCreateTime = [curDate + 8 * hour, curDate + 10 * hour];
      this.createTime = [...this.curCreateTime];
    },
    timeChange() {
      if (!this.createDate || !this.createTime) {
        this.modalForm.examStartTime = null;
        this.modalForm.examEndTime = null;
        return;
      }

      const curDate = getTimeDatestamp(this.createDate);
      const timeDate = getTimeDatestamp(this.createTime[0]);

      this.modalForm.examStartTime = curDate + this.createTime[0] - timeDate;
      this.modalForm.examEndTime = curDate + this.createTime[1] - timeDate;
    },
    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>
