<template>
  <el-dialog
    class="modify-campus"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="448px"
    :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-position="top"
    >
      <el-form-item prop="campusName" label="校区名称：">
        <el-input
          v-model.trim="modalForm.campusName"
          placeholder="请输入校区名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="campusCode" label="校区编码：">
        <el-input
          v-model.trim="modalForm.campusCode"
          placeholder="请输入校区编码"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item prop="printHouseId" label="印刷室">
        <print-room-select
          v-model.trim="modalForm.printHouseId"
          style="width:100%;"
          clearable
        ></print-room-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 { updateCampus, getCode } from "../api";

const initModalForm = {
  id: null,
  campusName: "",
  campusCode: "",
  printHouseId: ""
};

export default {
  name: "modify-campus",
  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: {
        campusName: [
          {
            required: true,
            // pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
            // message: "校区名称只能输入汉字、数字和字母，长度不能超过20",
            message: "校区名称不能超过30个字",
            max: 30,
            trigger: "change"
          }
        ],
        campusCode: [
          {
            required: true,
            // pattern: /^[0-9a-zA-Z_-]{3,30}$/,
            // message: "校区编码只能由数字字母短横线组成，长度在3-30之间",
            message: "校区名称不能超过30个字",
            max: 30,
            trigger: "change"
          }
        ],
        printHouseId: [
          {
            required: true,
            message: "请选择要关联的印刷室",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
        this.createCode();
      }
    },
    async createCode() {
      this.modalForm.campusCode = await getCode("CAMPUS_CODE");
    },
    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 updateCampus(this.modalForm).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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