import {useContextSection, useQueryTableList} from '@hooks';
import {FC} from 'react';
import {context, pageContext, searchContext} from '../context';
import {getInventoryMainList} from '@apis';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {useBoolean} from 'ahooks';
import AddModal from './modal';
import {useColumn} from './hooks';
import DetailModal from './detail-modal';
import LogModal from './log-modal';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{isFetching, data, count}, {refetch}] = useQueryTableList({
    queryFn: getInventoryMainList,
    params,
    pageContext,
    searchContext,
  });
  const [visible, {setTrue, setFalse}] = useBoolean();
  const [
    {columns, detailVisible, detailId, logVisible, logId},
    {onDetailDetailClose, onLogClose},
  ] = useColumn();

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          onRefresh={refetch}
          isRefreshing={isFetching}
          onAdd={setTrue}
        />

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

      <AddModal visible={visible} onClose={setFalse} onFetch={refetch} />
      <DetailModal
        visible={detailVisible}
        onClose={onDetailDetailClose}
        uuid={detailId}
      />
      <LogModal visible={logVisible} uuid={logId} onClose={onLogClose} />
    </>
  );
};

export default TableList;
