import {FC} from 'react';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {
  getFinishProductOutStreamExport,
  getFinishProductOutStreamList,
} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';
import {useColumns} from './hooks';
import EditModal from './modal';

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

  const [{tableColumns: columns, editId, visible}, {onClose}] = useColumns();

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

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

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

export default TableList;
