<template>
  <el-dialog
    class="apply-reexam-dialog"
    :visible.sync="dialogVisible"
    width="640px"
    title="设置重考"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    @open="visibleChange"
    @close="closed"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="model" label="重考方式：">
        <el-select v-model="modalForm.model" placeholder="请选择" clearable>
          <el-option
            v-for="(val, key) in REEXAM_TYPE"
            :key="key"
            :value="key * 1"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="examStudentId" label="证件号/姓名：">
        <multiple-select-student
          data-key="examStudentId"
          :data-list="dataList"
          :seleted-ids="modalForm.examStudentId"
          :show-add="true"
          @selected="idsChange"
          ref="MultipleSelectStudent"
          v-if="dataReady"
        ></multiple-select-student>
      </el-form-item>
      <el-form-item prop="reason" label="重考原因：">
        <el-select v-model="modalForm.reason" placeholder="请选择" clearable>
          <el-option
            v-for="(val, key) in REEXAM_REASON"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注详情：">
        <el-input
          type="textarea"
          v-model.trim="modalForm.remark"
          placeholder="请输入"
          :maxlength="200"
          :rows="5"
          show-word-limit
          clearable
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel" plain>取消</el-button>
      <el-button type="primary" @click="confirm" :disabled="isSubmit"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { applyReexam } from "@/api/invigilation";
import { REEXAM_TYPE, REEXAM_REASON } from "@/constant/constants";
import MultipleSelectStudent from "../common/MultipleSelectStudent";

const initModalForm = {
  examStudentId: [],
  model: 0,
  reason: null,
  remark: "",
};

export default {
  name: "apply-reexam-dialog",
  components: { MultipleSelectStudent },
  props: {
    students: {
      type: Array,
      default() {
        return [];
      },
    },
    dataList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    const recordIdValidator = (rule, value, callback) => {
      if (!value || !value.length) {
        callback(new Error("请选择学生"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      dataReady: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      REEXAM_TYPE,
      REEXAM_REASON,
      rules: {
        model: [
          {
            required: true,
            message: "请选择重考方式",
            trigger: "change",
          },
        ],
        examStudentId: [
          {
            required: true,
            validator: recordIdValidator,
            trigger: "change",
          },
        ],
        reason: [
          {
            required: true,
            message: "请选择重考原因",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { ...initModalForm };
      this.modalForm.examStudentId = this.students.map(
        (item) => item.examStudentId
      );
      this.dataReady = true;
    },
    closed() {
      this.dataReady = false;
    },
    cancel() {
      this.dialogVisible = false;
    },
    open() {
      this.dialogVisible = true;
    },
    idsChange(ids) {
      this.modalForm.examStudentId = ids;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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

      if (!data) return;

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