import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

import {FC} from 'react';
import {context, pageContext, searchContext} from '../context';
import {exportGSErrorInterface, getGSErrorInterface} from '@apis';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {useColumns} from './hooks';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getGSErrorInterface,
    params,
    pageContext,
    searchContext,
  });
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportGSErrorInterface,
  });
  const columns = useColumns(refetch);

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

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

export default TableList;
