<template>
  <Modal
    class="modify-student"
    v-model="modalIsShow"
    :title="title"
    :mask-closable="false"
    width="450"
    @on-visible-change="visibleChange"
  >
    <Form
      ref="modalFormComp"
      class="modal-form"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      :label-width="100"
      style="width: 340px;"
    >
      <FormItem prop="name" label="姓名">
        <Input
          size="large"
          v-model.trim="modalForm.name"
          placeholder="请输入姓名"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="examNumber" label="考号">
        <Input
          size="large"
          v-model.trim="modalForm.examNumber"
          placeholder="请输入考号"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="areaCode" label="考区">
        <Select
          size="large"
          v-model="modalForm.areaCode"
          @on-change="areaChange"
          placeholder="请选择考区"
        >
          <Option
            v-for="area in cascadeList"
            :key="area.areaCode"
            :value="area.areaCode"
            :label="area.areaName"
          ></Option>
        </Select>
      </FormItem>
      <FormItem prop="school" label="学校">
        <Select
          size="large"
          v-model="modalForm.school"
          @on-change="schoolChange"
          placeholder="请选择学校"
        >
          <Option
            v-for="(item, index) in schools"
            :key="index"
            :value="item.school"
            :label="item.school"
          ></Option>
        </Select>
      </FormItem>
      <FormItem prop="examRoom" label="考场">
        <Select
          size="large"
          v-model="modalForm.examRoom"
          placeholder="请选择考场"
        >
          <Option
            v-for="(room, index) in rooms"
            :key="index"
            :value="room"
            :label="room"
          ></Option>
        </Select>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
        >确认</Button
      >
      <Button shape="circle" @click="cancel">取消</Button>
    </div>
  </Modal>
</template>

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

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

export default {
  name: "modify-student",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    cascadeList: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "考生信息";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      schools: [],
      rooms: [],
      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) {
      this.$refs.modalFormComp.resetFields();
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange(visible) {
      if (visible) {
        this.initData(this.instance);
      }
    },
    areaChange() {
      const curArea = this.cascadeList.find(
        item => item.areaCode === this.modalForm.areaCode
      );
      this.schools = curArea.schools;
      this.rooms = [];
      this.modalForm.examRoom = null;
      if (curArea.schools.length === 1) {
        this.modalForm.school = curArea.schools[0].school;
        this.schoolChange();
      }
    },
    schoolChange() {
      const curSchool = this.schools.find(
        item => item.school === this.modalForm.school
      );
      this.rooms = curSchool.rooms;
      this.modalForm.examRoom = null;
    },
    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.$Message.success(this.title + "成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
