import {Table, TableTools} from '@components';
import {Card} from 'antd';
import {FC} from 'react';
import {ColumnsType} from 'antd/es/table';
import {GSInterfaceData} from '@models';
import {
  MAXIMAL_TABLE_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
} from '@utils';
import {context, pageContext, searchContext} from '../context';
import {exportGSInterface, getGSInterfaceList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<GSInterfaceData> = [
  {
    title: '接口名称',
    dataIndex: 'type',
    key: 'type',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '接口参数',
    dataIndex: 'data',
    key: 'data',
    width: MAXIMAL_TABLE_WIDTH,
  },
  {
    title: '接口类型',
    dataIndex: 'accessType',
    key: 'accessType',
    width: NORMAL_TABLE_WIDTH,
    render(_, {accessType}) {
      return accessType === '1' ? '接出' : '接入';
    },
  },
  {
    title: '接口时间',
    dataIndex: 'scrq',
    key: 'scrq',
    width: MIDDLE_TABLE_WIDTH,
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}] = useQueryTableList({
    queryFn: getGSInterfaceList,
    params,
    pageContext,
    searchContext,
  });

  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportGSInterface,
  });

  return (
    <Card className='table-wrapper'>
      <TableTools
        pageContext={pageContext}
        searchContext={searchContext}
        onExport={onExport}
        isExporting={isExporting}
      />

      <Table
        columns={columns}
        data={data}
        count={count}
        pageContext={pageContext}
        searchContext={searchContext}
        data-testid='gs_interface_log_table'
      />
    </Card>
  );
};

export default TableList;
