<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type {IConfigSearch} from "@/interface/page/config/IConfigSearch.ts";
import {onMounted, ref} from "vue";
import type {IAppConfigTable} from "@/model/table/IAppConfigTable.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import {ConfigApi} from "@/api/config.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import ConfigDetailComponent from "@/components/page/config/ConfigDetailComponent.vue";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const configSearch = ref<IConfigSearch>({}); // 搜索框数据
const tableData = ref<IAppConfigTable[]>([]); // 表格的数据
const showDetail = ref<boolean>(false); // 是否展示细节弹窗
const detailData = ref<IAppConfigTable>({}); // 发送到细节弹窗的数据

onMounted(() => {
  query();
});

// 查询表格数据
const query = () => {
  const loadingInstance = MessageUtils.loading('正在查询');
  ConfigApi.queryConfig(configSearch.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IAppConfigTable[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data;
      MessageUtils.successMessage('加载配置信息成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  })
}

// 重置查询参数
const resetInput = () => {
  configSearch.value = {};
}

// 编辑按钮
const handleEdit = (index: number, row: any) => {
  detailData.value = {
    configKey: row.configKey,
    configValue: row.configValue,
    configName: row.configName,
    tagText: row.tagText,
    updateTime: row.updateTime,
    remark: row.remark
  };
  showDetail.value = true;
}

// 重载配置
const reloadConfig = () => {
  ConfigApi.reloadConfig().then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeResponse;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
      MessageUtils.successMessage('重载成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {});() => {}
}

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>系统配置项管理</h3>
    <el-text type="warning">请勿随意修改，否则可能导致系统运行异常</el-text>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="configSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="配置键：" style="width: 250px;">
            <el-input v-model="configSearch.configKey" placeholder="请输入配置键" clearable></el-input>
          </el-form-item>
          <el-form-item label="配置名称：" style="width: 250px;">
            <el-input v-model="configSearch.configName"  placeholder="请输入配置名称" clearable></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <el-button type="primary" @click="query">查询/刷新</el-button>
          <el-button type="warning" @click="resetInput">重置查询参数</el-button>
          <el-button type="success" @click="reloadConfig">重载配置</el-button>
        </el-button-group>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 290px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip/>
        <el-table-column prop="configKey" label="配置键" width="250" align="center" show-overflow-tooltip />
        <el-table-column prop="configName" label="配置名称" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="configValue" label="配置值" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="tagText" label="标记" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="updateTime" label="上次更新时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" min-width="150" align="left" show-overflow-tooltip/>
        <el-table-column label="操作" width="80" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
<!--  展示细节的弹窗-->
  <ConfigDetailComponent v-model="showDetail" :form-data="detailData" @reLoadingTableData="query"></ConfigDetailComponent>
</template>

<style scoped>
.searchCard {
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.dataTable {
  margin: 10px 10px 0 auto;
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.searchForm {
  margin: 5px 10px 0 10px;
  width: 100%;
}
.searchForm :deep(.el-form-item__label){
  font-size: 13px;
}
</style>