<template>
  <Modal
    class="modify-student"
    v-model="modalIsShow"
    :title="title"
    :mask-closable="false"
    @on-visible-change="visibleChange"
  >
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      :label-width="100"
    >
      <FormItem prop="name" label="姓名">
        <Input
          v-model.trim="modalForm.name"
          placeholder="请输入姓名"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="examNumber" label="考号">
        <Input
          v-model.trim="modalForm.examNumber"
          placeholder="请输入考号"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="areaCode" label="考区">
        <Select v-model="modalForm.areaCode" placeholder="请选择考区">
          <Option value=""></Option>
        </Select>
      </FormItem>
      <FormItem prop="schoolId" label="学校">
        <Select v-model="modalForm.schoolId" placeholder="请选择学校">
          <Option value=""></Option>
        </Select>
      </FormItem>
      <FormItem prop="examRoom" label="考场">
        <Select v-model="modalForm.examRoom" placeholder="请选择考场">
          <Option value=""></Option>
        </Select>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="text" @click="cancel">取消</Button>
      <Button type="primary" :disabled="isSubmit" @click="submit">确认</Button>
    </div>
  </Modal>
</template>

<script>
import { updateStudent } from "@/api";

const initModalForm = {
  id: "",
  name: "",
  examNumber: "",
  areaCode: "",
  schoolID: "",
  examRoom: ""
};

export default {
  name: "modify-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: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change"
          }
        ],
        examNumber: [
          {
            required: true,
            message: "请输入考号",
            trigger: "change"
          }
        ],
        areaCode: [
          {
            required: true,
            message: "请选择考区",
            trigger: "change"
          }
        ],
        schoolId: [
          {
            required: true,
            message: "请选择学校",
            trigger: "change"
          }
        ],
        examRoom: [
          {
            required: true,
            message: "请选择考场",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange(visible) {
      if (visible) {
        this.initData(this.instance);
      }
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

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

      if (!data) return;

      this.isSubmit = false;
      this.$Message.success(this.title + "成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
