<template>
  <el-dialog
    class="modify-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :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="studentName" label="姓名：">
        <el-input
          v-model.trim="modalForm.studentName"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="studentCode" label="学号：">
        <el-input
          v-model.trim="modalForm.studentCode"
          placeholder="请输入学号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="phoneNumber" label="手机号：">
        <el-input
          v-model.trim="modalForm.phoneNumber"
          placeholder="请输入手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="campusId" label="校区：">
        <campus-select
          v-model.trim="modalForm.campusId"
          clearable
        ></campus-select>
      </el-form-item>
      <el-form-item prop="clazz" label="班级信息：">
        <el-input
          v-model.trim="modalForm.clazz"
          placeholder="请输入班级信息"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateStudent } from "../api";
import { phone } from "@/plugins/formRules";

const initModalForm = {
  id: null,
  studentName: "",
  studentCode: "",
  phoneNumber: "",
  campusId: "",
  clazz: ""
};

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: {
        studentName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change"
          },
          {
            message: "姓名不能超过50个字",
            max: 50,
            trigger: "change"
          }
        ],
        studentCode: [
          {
            required: true,
            message: "请输入学号",
            trigger: "change"
          },
          {
            message: "学号不能超过50个字",
            max: 50,
            trigger: "change"
          }
        ],
        phoneNumber: phone,
        campusId: [
          {
            required: true,
            message: "请选择校区",
            trigger: "change"
          }
        ],
        clazz: [
          {
            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;
      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>
