<template>
  <el-dialog
    class="modify-app-env"
    :visible.sync="modalIsShow"
    :title="title"
    top="10px"
    width="540px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item prop="name" label="名称：">
        <el-input
          v-model.trim="modalForm.name"
          placeholder="请输入名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="code" label="编码：">
        <el-input
          v-model.trim="modalForm.code"
          placeholder="请输入编码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="type" label="分类：">
        <el-select
          v-model="modalForm.type"
          placeholder="请选择分类"
          clearable
          :disabled="isEdit"
        >
          <el-option
            v-for="item in types"
            :key="item.code"
            :value="item.code"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="userId" label="维护人：">
        <user-select
          v-model="modalForm.userId"
          placeholder="请选择维护人"
          :filter-data="{ enable: true }"
        ></user-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="danger" @click="cancel" plain>取消</el-button>
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { appEnvInsertOrUpdate } from "../api";

const initModalForm = {
  id: "",
  appId: null,
  name: "",
  code: "",
  type: "",
  userId: null
};

export default {
  name: "modify-app-env",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    types: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "应用环境";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "change"
          },
          {
            max: 50,
            message: "名称不能超过50字符",
            trigger: "change"
          }
        ],
        code: [
          {
            required: true,
            message: "请输入编码",
            trigger: "change"
          },
          {
            max: 50,
            message: "编码不能超过50字符",
            trigger: "change"
          }
        ],
        type: [
          {
            required: true,
            message: "请选择分类",
            trigger: "change"
          }
        ],
        userId: [
          {
            required: true,
            message: "请选择维护人",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
      this.$refs.modalFormComp.clearValidate();
    },
    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;
      let datas = { ...this.modalForm };
      const data = await appEnvInsertOrUpdate(datas).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

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