<script setup lang="ts">

import type {IBaseApiData} from "@/model/table/IBaseApiData.ts";
import {ref} from "vue";
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 formData = ref<IBaseApiData>({}); // 要添加的数据
const close = () => {
  modelValue.value = false;
};
// 添加
const add = () => {
  MessageUtils.confirmMessageBox('确定添加以上数据？', '添加确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    ApiApi.addApi(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).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" placeholder="请填写API键"></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="add">确定添加</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>