<template>
  <el-dialog
    class="modify-black-item"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="configKey" label="参数名称：">
        <span v-if="isEdit">{{ modalForm.configKey }}</span>
        <el-input
          v-else
          v-model.trim="modalForm.configKey"
          placeholder="请输入参数名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="configValue" label="参数值：">
        <el-input
          v-model.trim="modalForm.configValue"
          placeholder="请输入参数值"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="configName" label="描述：">
        <el-input
          v-model.trim="modalForm.configName"
          placeholder="请输入描述"
          clearable
        ></el-input>
      </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 { objAssign } from "@/utils/utils";
import { saveSystemConfig } from "@/api/system";

const initModalForm = {
  id: null,
  configKey: "", //参数名称
  configName: "", //描述
  configValue: "", //参数值,
  enable: null,
};

export default {
  name: "modify-config-item",
  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: {
        configKey: [
          {
            required: true,
            message: "请设置参数名称",
            trigger: "change",
          },
          {
            message: "参数名称不能超过100个字",
            max: 100,
            trigger: "change",
          },
        ],
        configValue: [
          {
            required: true,
            message: "请设置参数值",
            trigger: "change",
          },
          {
            message: "参数值不能超过200个字",
            max: 200,
            trigger: "change",
          },
        ],
        configName: [
          {
            required: true,
            message: "请输入描述",
            trigger: "change",
          },
          {
            message: "描述不能超过100个字",
            max: 100,
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    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 saveSystemConfig(datas).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

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