import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';
import {FC} from 'react';
import {context, pageContext, searchContext} from '../context';
import {exportInventoryLog, getInventoryDetailedList} from '@apis';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {ColumnsType} from 'antd/es/table';
import {InventoryLogListData} from '@models';
import {
  HUGE_TABLE_WIDTH,
  MODAL_PAGE_SIZE_LIST,
  NORMAL_TABLE_WIDTH,
} from '@utils';

const columns: ColumnsType<InventoryLogListData> = [
  {title: '物料编号', dataIndex: 'wllbCode', width: NORMAL_TABLE_WIDTH},
  {title: '物料名称', dataIndex: 'name', width: HUGE_TABLE_WIDTH},
  {
    title: '库位名称',
    dataIndex: 'storageLocationName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '系统库存',
    dataIndex: 'total',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '实际库存',
    dataIndex: 'amount',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
];

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

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

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

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

export default TableList;
