<script setup lang="ts">
import {ref} from "vue";
import type {IBaseApiData} from "@/model/table/IBaseApiData.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import ApiApi from "@/api/api.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";

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

// 保存数据‘
const update = () => {
  MessageUtils.confirmMessageBox('确定要保存吗？', '请确认').then(() => {
    const loadingInstance = MessageUtils.loading('正在保存中...');
    ApiApi.updateApi(props.formData).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('保存成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(() => {})
};

// 删除数据
const deleteData = () => {
  MessageUtils.confirmMessageBox('确定要删除吗？', '请确认').then(() => {
    const loadingInstance = MessageUtils.loading('正在删除中...');
    ApiApi.deleteApi(props.formData.apiKey).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('删除成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch((err) => {
      MessageUtils.errMessageBox('请求服务器失败：' + err, '错误');
    }).finally(() => {
      loadingInstance.close();
    })
  }).catch(() => {})
}

</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="查看编辑 API 基础资料" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="API 键:" required>
            <el-input v-model="formData.apiKey" disabled></el-input>
          </el-form-item>
          <el-form-item label="API 名称:" required>
            <el-input v-model="formData.apiName" placeholder="请填写API名称"></el-input>
          </el-form-item>
          <el-form-item label="根键:">
            <el-input v-model="formData.rootKey" placeholder="请填写根键"></el-input>
          </el-form-item>
          <el-form-item label="地址:" required>
            <el-input v-model="formData.address" placeholder="请填写地址"></el-input>
          </el-form-item>
          <el-form-item label="标记:">
            <el-input v-model="formData.tag" placeholder="请填写标记"></el-input>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="update">保存数据</el-button>
          <el-button type="warning" @click="deleteData">删除记录</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>