<template>
  <el-dialog
    class="modify-app-baseline-item"
    :visible.sync="modalIsShow"
    title="应用配置基线配置项修改"
    top="10vh"
    width="540px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form ref="modalFormComp" :model="modalForm" label-width="80px">
      <el-form-item prop="key" label="配置项：">
        {{ modalForm.key }}
      </el-form-item>
      <el-form-item prop="mode" label="模式：">
        <el-select v-model="modalForm.mode" placeholder="请选择模式" clearable>
          <el-option
            v-for="item in modes"
            :key="item.code"
            :value="item.code"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="comment" label="注释：">
        <el-input
          v-model.trim="modalForm.comment"
          placeholder="请输入编码"
          clearable
        ></el-input>
      </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 { appConfigBaselineItemUpdate } from "../api";

const initModalForm = {
  appId: "",
  moduleId: "",
  versionId: "",
  key: "",
  mode: "",
  comment: "",
};

export default {
  name: "modify-app-baseline-item",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
    modes: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
    };
  },
  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 appConfigBaselineItemUpdate(datas).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

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