import {Card} from 'antd';
import {FC} from 'react';
import {useHandle} from './hooks';
import {context, pageContext, searchContext} from '../context';
import UserModal from './modal';
import {Table, TableTools} from '@components';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';
import {exportUser, getUserList} from '@apis';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getUserList,
    params,
    pageContext,
    searchContext,
  });
  const [{columns, visible, editId}, {onAdd, onClose}] = useHandle(refetch);
  const [isExporting, onExport] = useTableExportEvent({
    fn: exportUser,
    pageContext,
    context,
  });

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

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

      <UserModal
        visible={visible}
        onClose={onClose}
        onFetch={refetch}
        id={editId}
      />
    </>
  );
};

export default TableList;
