<template>
  <ele-page>
    <operation-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 #action="{ row }">
          <el-link type="primary" underline="never" @click="openDetail(row)">
            详情
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 详情弹窗 -->
    <operation-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 OperationRecordSearch from './components/operation-record-search.vue';
  import OperationRecordDetail from './components/operation-record-detail.vue';
  import { pageOperationRecords } from '@/api/system/operation-record';
  import type {
    OperationRecord,
    OperationRecordParam
  } from '@/api/system/operation-record/model';

  defineOptions({ name: 'SystemOperationRecord' });

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

  /** 表格列配置 */
  const columns = ref<Columns>([
    {
      type: 'index',
      columnKey: 'index',
      width: 50,
      align: 'center'
    },
    {
      prop: 'contextId',
      label: 'ContextId',
      width: 280
    },
    {
      prop: 'operateUserId',
      label: '操作人',
      width: 110
    },
    {
      prop: 'location',
      label: '地址',
      minWidth: 160
    },
    {
      prop: 'event',
      label: '事件',
      width: 120
    },
    {
      prop: 'description',
      label: '操作描述',
      minWidth: 210
    },
    {
      prop: 'createTime',
      label: '操作时间',
      sortable: 'custom',
      align: 'center',
      width: 180
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 90,
      align: 'center',
      slot: 'action' /* ,
      fixed: 'right' */,
      hideInPrint: true,
      hideInExport: true
    }
  ]);

  /** 当前选中数据 */
  const current = ref<OperationRecord>({
    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 pageOperationRecords({
      ...where,
      ...orders,
      ...filters,
      ...pages
    });
  };

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

  /** 详情 */
  const openDetail = (row: OperationRecord) => {
    current.value = row;
    showInfo.value = true;
  };
</script>
