import { ProColumns, ProTable } from '@ant-design/pro-components';
import { Tag } from 'antd';
import { listLogs } from '@/services/api';

const columns: ProColumns[] = [
  { title: '操作人', dataIndex: 'username' },
  { title: '模块', dataIndex: 'module' },
  { title: '操作', dataIndex: 'operation' },
  { title: '方法', dataIndex: 'method', search: false, render: (v) => <Tag>{v}</Tag> },
  { title: '路径', dataIndex: 'path', search: false, ellipsis: true },
  { title: 'IP', dataIndex: 'ip', search: false },
  { title: '耗时(ms)', dataIndex: 'duration', search: false },
  {
    title: '结果', dataIndex: 'status', search: false,
    render: (_, r) => <Tag color={r.status === 1 ? 'success' : 'error'}>{r.status === 1 ? '成功' : '失败'}</Tag>,
  },
  { title: '操作时间', dataIndex: 'createdAt', valueType: 'dateTime', search: false },
];

const LogsPage: React.FC = () => (
  <ProTable
    headerTitle="操作日志"
    rowKey="logId"
    columns={columns}
    request={async (params) => {
      const res = await listLogs({
        page: params.current, pageSize: params.pageSize,
        username: params.username, module: params.module, operation: params.operation,
      });
      return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 };
    }}
    search={{ defaultCollapsed: false }}
  />
);

export default LogsPage;
