// 添加/修改用户库
<template>
  <el-dialog
    :title="isEdit ? '编辑应用类型' : '添加应用类型'"
    width="500px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="90px"
    >
      <el-form-item prop="id" style="margin-bottom: 0">&nbsp;</el-form-item>
      <el-form-item label="应用类型" prop="applicationType" required>
        <el-input v-model="form.applicationType" placeholder="请输入应用类型" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { applicationTypeEdit } from "@/api/configuration";

export default {
  data() {
    return {
      form: {
        applicationType: null,
      },
      rules: {
        applicationType: [
          {
            required: true,
            whitespace: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
    };
  },
  created() {
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  methods: {
    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj;
          this.isEdit = true;
        } else {
          this.form = JSON.parse(JSON.stringify(this.form_copy));
          this.isEdit = false;
        }
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        this.submitLoading = true;
        try {
          await applicationTypeEdit(this.form);
          this.visible = false;
          this.$emit("success", this.isEdit);
        } catch (e) {
          console.info("add", e);
        } finally {
          this.submitLoading = false;
        }
      });
    },
  },
};
</script>
