<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import {computed} from "vue";
import MessageUtils from "@/utils/MessageUtils.ts";
import {ConfigApi} from "@/api/config.ts";
import type {IAppConfigTable} from "@/model/table/IAppConfigTable.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const props = defineProps(['formData']);
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
}
const formData = computed<IAppConfigTable>(() => props.formData);

// 保存修改
const save = () => {
  MessageUtils.confirmMessageBox('确定修改配置？','提示').then(() => {
    const loadingInstance = MessageUtils.loading('请稍后');
    ConfigApi.updateConfig(formData.value).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('更新成功');
        close();
        emit('reLoadingTableData');
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{});
}

</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="配置项详细信息" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:40vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="配置键:" required>
            <el-text type="success">{{formData.configKey}}</el-text>
          </el-form-item>
          <el-form-item label="配置名称:" required>
            <el-input v-model="formData.configName" placeholder="请填写配置名称"></el-input>
          </el-form-item>
          <el-form-item label="配置值:" required>
            <el-input v-model="formData.configValue" placeholder="请填写配置值"></el-input>
          </el-form-item>
          <el-form-item label="标记:">
            <el-input v-model="formData.tagText" placeholder="标记"></el-input>
          </el-form-item>
          <el-form-item label="上次更新时间:">
            <el-text>{{ FormatterUtils.formatTime(formData.updateTime?.toString() ?? '') }}</el-text>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark" placeholder="请填写备注"></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="save">保存修改</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>