<template>
  <el-dialog
    class="modify-student"
    :visible.sync="modalIsShow"
    :title="title"
    top="10px"
    width="448px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    destroy-on-close
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-position="top"
    >
      <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="collegeId" label="学院：">
        <college-select
          v-model="modalForm.collegeId"
          placeholder="学院"
          style="width: 100%;"
        ></college-select>
      </el-form-item>
      <el-form-item prop="majorId" label="专业：">
        <major-select
          v-model="modalForm.majorId"
          :college-id="modalForm.collegeId"
          cascader
          placeholder="请选择专业"
          style="width: 100%;"
          @change="updateClazz"
        ></major-select>
      </el-form-item>
      <el-form-item prop="clazzId" label="班级：">
        <el-select
          v-model="modalForm.clazzId"
          placeholder="请选择班级"
          style="width: 100%;"
          filterable
        >
          <el-option
            v-for="item in clazzList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </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 { updateStudent, unitQueryByType } from "../api";

const initModalForm = {
  id: null,
  studentName: "",
  studentCode: "",
  phoneNumber: "",
  collegeId: "",
  majorId: "",
  clazzId: ""
};

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: [
          {
            required: false,
            pattern: /^1\d{10}$/,
            message: "请输入合适的手机号码",
            trigger: "change"
          }
        ],
        collegeId: [
          {
            required: false,
            message: "请选择机构",
            trigger: "change"
          }
        ],
        majorId: [
          {
            required: false,
            message: "请选择专业",
            trigger: "change"
          }
        ],
        clazzId: [
          {
            required: false,
            message: "请选择班级",
            trigger: "change"
          }
        ]
      },
      clazzList: []
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
        this.getClazz();
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    updateClazz() {
      this.modalForm.clazzId = "";
      this.getClazz();
    },
    async getClazz() {
      this.clazzList = [];
      if (!this.modalForm.majorId) return;
      const res = await unitQueryByType(
        {
          majorId: this.modalForm.majorId
        },
        "CLAZZ"
      );
      this.clazzList = res;
    },
    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>
