<template>
  <ele-page>
    <request-record-search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        :highlight-current-row="true"
        :export-config="{ fileName: '请求日志数据' }"
        cache-key="systemOperationRecordTable"
      >

        <template #requestPath="{ row }">
          <el-tag size="small">{{ row.requestMethod }}</el-tag>
          <span style="margin-left: 10px;">{{ row.requestPath }}</span>
        </template>
        <template #responseCode="{ row }">
          <el-tag
            size="small"
            :type="row.responseCode === 200 ? 'success' : 'danger'"
            :disable-transitions="true"
          >
            {{ row.responseCode }}
          </el-tag>
        </template>
        <template #action="{ row }">
          <el-link type="primary" underline="never" @click="openDetail(row)">
            详情
          </el-link>
        </template>
        <template #client="{ row }">
          {{ row.clientName }} - {{ row.clientVersion }}
        </template>

      </ele-pro-table>
    </ele-card>
    <!-- 详情弹窗 -->
    <request-record-detail v-model="showInfo" :data="current" />
  </ele-page>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import type { EleProTable } from 'ele-admin-plus';
  import type {
    DatasourceFunction,
    Columns
  } from 'ele-admin-plus/es/ele-pro-table/types';
  import RequestRecordSearch from './components/request-record-search.vue';
  import RequestRecordDetail from './components/request-record-detail.vue';
  import {
    getRequestRecordInfo,
    pageRequestRecords
  } from '@/api/system/request-record';
  import type {
    RequestRecord,
    RequestRecordParam
  } from '@/api/system/request-record/model';
  import {formatBytes} from "@/utils/common";

  defineOptions({ name: 'SystemRequestRecord' });

  /** 表格实例 */
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  /** 表格列配置 */
  const columns = ref<Columns>([
    {
      type: 'index',
      columnKey: 'index',
      width: 50,
      align: 'center'
    },
    {
      prop: 'clientId',
      label: '客户端ID',
      hideInTable: true,
      width: 280
    },
    {
      prop: 'clientName',
      slot: 'client',
      label: '客户端',
      width: 130
    },
    {
      prop: 'contextId',
      label: 'ContextId',
      width: 280
    },
    {
      prop: 'requestPath',
      label: 'Request',
      slot: 'requestPath',
      minWidth: 220
    },
    {
      prop: 'ruleName',
      label: '规则名称',
      minWidth: 110
    },
    {
      prop: 'ruleRoute',
      label: '路由地址',
      hideInTable: true,
      minWidth: 210
    },
    {
      prop: 'responseCode',
      label: '状态码',
      width: 100,
      align: 'center',
      slot: 'responseCode',
      filters: [
        {
          text: '正常',
          value: '1'
        },
        {
          text: '异常',
          value: '2'
        }
      ],
      filterMultiple: false,
      formatter: (row) => (row.responseCode == 200 ? '正常' : '异常')
    },
    {
      prop: 'runningTime',
      label: '耗时',
      sortable: 'custom',
      width: 100,
      formatter: (row) => `${row.runningTime / 1000}s`,
      align: 'center'
    },
    {
      prop: 'runningMemoryUsage',
      label: '内存消耗',
      width: 110,
      sortable: 'custom',
      align: 'center',
      formatter: (row) => formatBytes(row.runningMemoryUsage),

    },
    {
      prop: 'requestTime',
      label: '请求时间',
      align: 'center',
      width: 180
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 90,
      align: 'center',
      slot: 'action' /* ,
      fixed: 'right' */,
      hideInPrint: true,
      hideInExport: true
    }
  ]);

  /** 当前选中数据 */
  const current = ref<RequestRecord>({
    module: '',
    description: '',
    url: '',
    requestMethod: '',
    method: '',
    params: '',
    result: '',
    error: '',
    spendTime: 0,
    os: '',
    device: '',
    browser: '',
    ip: '',
    status: 0,
    createTime: '',
    nickname: '',
    username: ''
  });

  /** 是否显示查看弹窗 */
  const showInfo = ref(false);

  /** 表格数据源 */
  const datasource: DatasourceFunction = ({
    pages,
    where,
    orders,
    filters
  }) => {
    return pageRequestRecords({
      ...where,
      ...orders,
      ...filters,
      ...pages
    });
  };

  /** 刷新表格 */
  const reload = (where?: RequestRecordParam) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 详情 */
  const openDetail = (row: RequestRecord) => {
    getRequestRecordInfo(row.id).then((data)=>{
      current.value = data;
      showInfo.value = true;
    });
  };

</script>
