<template>
  <el-dialog
    class="student-breach-dialog"
    :visible.sync="dialogVisible"
    width="640px"
    title="违纪处理"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    @open="opened"
  >
    <div class="tips-info tips-error">
      <i class="el-icon-warning"></i>
      <p v-if="modalForm.breachStatus">
        本操作记录考生的违规事实，并对考生进行违纪处理，请监考老师仔细核对好考生的信息，谨慎操作！
      </p>
      <p v-else>
        本操作将撤销考生撤销违纪处理，记录撤销的原因。请监考老师仔细核对好考生的信息，谨慎操作！
      </p>
    </div>
    <div class="student-info">
      <div class="student-info-item">
        <span>证件号：</span>
        <span>{{ modalForm.identity }}</span>
      </div>
      <div class="student-info-item">
        <span>姓名：</span>
        <span>{{ modalForm.examStudentName }}</span>
      </div>
      <div class="student-info-item">
        <span>科目（代码）：</span>
        <span>{{ modalForm.courseNameCode }}</span>
      </div>
    </div>
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="type" :label="reasonTitle">
        <el-select v-model="modalForm.type" placeholder="请选择" clearable>
          <el-option
            v-for="item in typeList"
            :key="item.key"
            :value="item.key"
            :label="item.val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="descTitle">
        <el-input
          type="textarea"
          v-model.trim="modalForm.description"
          placeholder="请输入"
          :rows="5"
          :maxlength="200"
          show-word-limit
        ></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 { updateBreachInfo } from "@/api/invigilation";
import { BREACH_REASON_TYPE, BREACH_REPEAL_TYPE } from "@/constant/constants";

const initModalForm = {
  examStudentName: "",
  identity: "",
  courseNameCode: "",
  description: "",
  examRecordId: [],
  breachStatus: 0,
  status: 1,
  type: "",
};

export default {
  name: "student-breach-dialog",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogVisible: false,
      isSubmit: false,
      reasonTitle: "",
      descTitle: "",
      typeList: [],
      modalForm: {},
      rules: {
        type: [
          {
            required: true,
            message: "请选择违规类型",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    opened() {
      // breachStatus: 0:违纪,1:正常
      this.modalForm = Object.assign({}, initModalForm, this.instance);
      this.reasonTitle = this.modalForm.breachStatus
        ? "违规类型："
        : "撤销原因：";
      this.descTitle = this.modalForm.breachStatus
        ? "违规描述："
        : "原因详述：";
      this.rules.type[0].message = this.modalForm.breachStatus
        ? "请选择违规类型"
        : "请选择撤销原因";
      const options = this.modalForm.breachStatus
        ? BREACH_REASON_TYPE
        : BREACH_REPEAL_TYPE;
      let typeList = [];
      Object.entries(options).map(([key, val]) => {
        typeList.push({
          key,
          val,
        });
      });
      this.typeList = typeList;
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.dialogVisible = false;
    },
    open() {
      this.dialogVisible = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const result = await updateBreachInfo(this.modalForm).catch(() => {});

      this.isSubmit = false;
      if (!result) return;

      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
