<template>
  <el-dialog
    class="modify-organization"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="448px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-position="top"
    >
      <el-form-item label="上级机构：">
        <el-input v-model.trim="modalForm.parentName" disabled></el-input>
      </el-form-item>
      <el-form-item prop="name" label="机构名称：">
        <el-input
          v-model.trim="modalForm.name"
          placeholder="请输入机构名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="type" label="机构类型：">
        <el-select
          v-model="modalForm.type"
          style="width: 100%;"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in orgTypes"
            :key="item.type"
            :value="item.type"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="启用/禁用：">
          <el-switch
            v-model="modalForm.enable"
            
            inactive-color="#dcdfe6"
          >
          </el-switch>
        </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 { updateOrganization } from "../api";

const initModalForm = {
  id: null,
  name: "",
  type: "",
  parentId: null,
  parentName: "",
  enable: true
};

export default {
  name: "modify-organization",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    orgTypes: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "机构";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        name: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,30}$/,
            message: "机构名称只能输入汉字、数字和字母，长度不能超过30",
            trigger: "change"
          }
        ],
        type: [
          {
            required: true,
            message: "请选择机构类型",
            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>
