<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import {onMounted, ref} from "vue";
import RecordApi from "@/api/recordApi.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import type {IRecordApiResponseSearch} from "@/interface/page/recordApi/IRecordApiResponseSearch.ts";
import type {IRecordApiResponseData} from "@/model/table/IRecordApiResponseData.ts";
import RecordApiResponseDetail from "@/components/page/recordApi/RecordApiResponseDetail.vue";

const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const querySearch = ref<IRecordApiResponseSearch>({}); // 查询参数
const tableData = ref<IRecordApiResponseData[] | undefined>([]); // 表格数据
const showDetail = ref<boolean>(false); // 是否展示详情
const detailData = ref<IRecordApiResponseData>(); // 详情数据

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

// 查询数据
const query = () => {
  const loading = MessageUtils.loading();
  RecordApi.queryApiResponse(querySearch.value, pageSize.value, pageIndex.value).then(res => {
    const stringify = JSON.stringify(res.data);
    const response = JSON.parse(stringify) as AppServeDataResponse<PageDataResponse<IRecordApiResponseData>>;
    if (response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data.data;
      totalPages.value = response.data.totalCount;
      return;
    }
    MessageUtils.errMessage(response.msg);
  }).catch(() => {}).finally(() => {
    loading.close();
  });
};
// 重置查询参数
const resetInput = () => {
  querySearch.value = {};
};
// 分页切换
const paginationChange = (pageIndex: number) => {
  query();
};
// 查看详情数据
const handleEdit = (index: number, row: any) => {
  detailData.value = {
    recordId: row.recordId,
    path: row.path,
    method: row.method,
    mediaType: row.mediaType,
    responseCode: row.responseCode,
    clientAddress: row.clientAddress,
    urlParam: row.urlParam,
    requestMsg: row.requestMsg,
    responseMsg: row.responseMsg,
    requestTime: row.requestTime,
    responseTime: row.responseTime,
    useTime: row.useTime,
    errMsg: row.errMsg
  };
  showDetail.value = true;
};

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>API接收记录【外部系统向WCS请求】</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="querySearch" label-position="top" label-width="150px" inline>
          <el-form-item label="资源路径：" style="width: 250px;">
            <el-input v-model="querySearch.path" clearable></el-input>
          </el-form-item>
          <el-form-item label="关键字：" style="width: 250px;">
            <el-input v-model="querySearch.queryStr" clearable></el-input>
          </el-form-item>
          <el-form-item label="请求时间：" style="width: 250px;">
            <el-date-picker
                v-model="querySearch.requestTimes"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            />
          </el-form-item>
        </el-form>
        <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-group>
        </el-row>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 320px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="recordId" label="记录号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="path" label="资源路径" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="method" label="请求方法" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="responseCode" label="响应码" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="mediaType" label="媒体类型" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="clientAddress" label="客户端地址" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="urlParam" label="URL参数" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="requestMsg" label="请求数据" width="200" align="center" show-overflow-tooltip/>
        <el-table-column prop="responseMsg" label="响应数据" width="200" align="center" show-overflow-tooltip/>
        <el-table-column prop="requestTime" label="请求时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="responseTime" label="响应时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="useTime" label="耗时" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="errMsg" label="异常信息" min-width="250" align="center" show-overflow-tooltip />
        <el-table-column label="操作" width="80" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin: 10px"
                     background
                     v-model:current-page="pageIndex"
                     v-model:page-size="pageSize"
                     :page-sizes="[10, 100, 200, 300]"
                     size="small"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalPages"
                     @size-change="paginationChange"
                     @current-change="paginationChange"
      />
    </el-row>
  </div>
<!--  详情页-->
  <RecordApiResponseDetail v-model="showDetail" :form-data="detailData"/>
</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>