<template>
  <el-dialog
    class="modify-organization"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <div class="part-box part-box-pad part-box-border">
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item prop="orgName" label="机构名称：">
          <el-input
            style="width:282px;"
            v-model.trim="modalForm.orgName"
            placeholder="请输入机构名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="orgCode" label="机构代码：">
          <el-input
            style="width:282px;"
            v-model.trim="modalForm.orgCode"
            placeholder="请输入机构代码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="上级机构：">
          <el-input
            style="width:282px;"
            v-model.trim="modalForm.parentName"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="启用/禁用：">
          <el-select
            v-model="modalForm.enable"
            style="width: 100px;"
            placeholder="请选择"
          >
            <el-option
              v-for="(val, key) in ABLE_TYPE"
              :key="key"
              :value="key * 1"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <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 { ABLE_TYPE } from "@/constants/enumerate";
import { updateOrganization } from "../api";

const initModalForm = {
  id: null,
  orgName: "",
  orgCode: "",
  parentId: null,
  parentName: "",
  enable: 1
};

export default {
  name: "modify-organization",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "机构";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      ABLE_TYPE,
      rules: {
        orgName: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,30}$/,
            message: "机构名称只能输入汉字、数字和字母，长度不能超过30",
            trigger: "change"
          }
        ],
        orgCode: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z_-]{3,30}$/,
            message: "机构代码只能由数字字母短横线组成，长度在3-30之间",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    visibleChange() {
      this.initData(this.instance);
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    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 datas = { ...this.modalForm };
      const data = await updateOrganization(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$emit("confirm", this.modalForm);
      this.cancel();
    }
  }
};
</script>
