<template>
  <el-dialog
    :visible.sync="modalIsShow"
    title="设置考生状态"
    width="440px"
    :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="60px"
    >
      <el-form-item prop="status" label="状态：">
        <el-select
          v-model="modalForm.status"
          class="width-full"
          placeholder="状态"
        >
          <el-option
            v-for="(val, key) in STUDENT_STATUS"
            :key="key"
            :value="key"
            :label="val"
          >
          </el-option>
        </el-select>
      </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 { updateExamStudentStatus } from "../api";
import { STUDENT_STATUS } from "@/constants/enumerate";

const initModalForm = {
  id: null,
  status: "",
};

export default {
  name: "modify-exam-student-status",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      STUDENT_STATUS,
      modalForm: { ...initModalForm },
      rules: {
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);

      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },

    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 data = await updateExamStudentStatus(this.modalForm).catch(
        () => {}
      );
      this.isSubmit = false;

      if (!data) return;

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