<template>
  <el-dialog
    class="modify-exam"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="650px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <el-form-item prop="name" label="考试名称：">
        <el-input
          v-model.trim="modalForm.name"
          placeholder="请输入考试名称"
          clearable
        ></el-input>
        <p class="tips-info">示例：期末考试/期中考试/模拟考试</p>
      </el-form-item>
      <el-form-item prop="semesterId" label="学年学期：">
        <semester-select
          v-model="modalForm.semesterId"
          placeholder="学年学期"
          clearable
          style="width:100%"
        ></semester-select>
      </el-form-item>
      <el-form-item prop="category" label="考试类型：">
        <el-radio-group v-model="modalForm.category">
          <el-radio v-for="(val, key) in EXAM_TYPE" :key="key" :label="key">{{
            val
          }}</el-radio>
        </el-radio-group>
        <div class="tips-info">
          <p>说明：</p>
          <p>线下考试指纸笔传统考试，需要印刷试卷/题卡</p>
          <p>线上考试指无纸化考试，如网考/机考</p>
        </div>
      </el-form-item>
      <el-form-item
        v-if="modalForm.category === 'OFFLINE'"
        prop="examModel"
        label=""
      >
        <p class="tips-info mb-2">请选择符合此考试的业务模式</p>
        <el-radio-group v-model="modalForm.examModel">
          <el-radio
            v-for="(val, key) in EXAM_MODE_TYPE"
            :key="key"
            :label="key"
            class="el-radio-v"
            >{{ val }}</el-radio
          >
        </el-radio-group>
      </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 { updateExam } from "../api";
import { EXAM_TYPE, EXAM_MODE_TYPE } from "@/constants/enumerate";

const initModalForm = {
  id: null,
  name: "",
  semesterId: "",
  category: "OFFLINE",
  examModel: "MODEL1"
};

export default {
  name: "modify-exam",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "考试";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      EXAM_TYPE,
      EXAM_MODE_TYPE,
      rules: {
        name: [
          {
            required: true,
            message: "请输入考试名称",
            trigger: "change"
          },
          {
            message: "考试名称不能超过100个字符",
            max: 100,
            trigger: "change"
          }
        ],
        semesterId: [
          {
            required: true,
            message: "请选择学年学期",
            trigger: "change"
          }
        ],
        category: [
          {
            required: true,
            message: "请选择考试类型",
            trigger: "change"
          }
        ],
        examModel: [
          {
            required: true,
            message: "请选择考试模式",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    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;
      let datas = { ...this.modalForm };
      const data = await updateExam(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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