import {FC} from 'react';
import {WarehousingListData} from '@models';
import {ColumnsType} from 'antd/es/table';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {Card} from 'antd';
import {
  HUGE_TABLE_WIDTH,
  LARGE_TABLE_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SMALL_TABLE_WIDTH,
} from '@utils';
import {exportWarehousing, getWarehousingFlowList} from '@apis';
import {useContextSection, useQueryTableList, useTableExportEvent} from '@hooks';

const columns: ColumnsType<WarehousingListData> = [
  {
    title: '入库单编号',
    dataIndex: 'storageCode',
    key: 'storageCode',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '采购订单号',
    dataIndex: 'orderCode',
    key: 'orderCode',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '物料编号',
    dataIndex: 'wllbCode',
    key: 'wllbCode',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'materialName',
    key: 'materialName',
    width: HUGE_TABLE_WIDTH,
  },
  {
    title: '供应商名称',
    dataIndex: 'supplierName',
    key: 'supplierName',
    width: LARGE_TABLE_WIDTH,
  },
  {
    title: '所属公司',
    dataIndex: 'accountName',
    width: LARGE_TABLE_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'accountSleeve',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '生产批次',
    dataIndex: 'producDate',
    key: 'producDate',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '入库数量',
    dataIndex: 'capacity',
    key: 'capacity',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '采购数量',
    dataIndex: 'purchaseNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '库位名称',
    dataIndex: 'storageLocationName',
    key: 'storageLocationName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '工号',
    dataIndex: 'userName',
    key: 'userName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '姓名',
    dataIndex: 'realName',
    key: 'realName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: 'WBS编号',
    dataIndex: 'wbs',
    key: 'wbs',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '类型',
    dataIndex: 'type',
    key: 'type',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '入库日期',
    dataIndex: 'scrq',
    key: 'scrq',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '连番号',
    dataIndex: 'serial',
    key: 'serial',
    width: SMALL_TABLE_WIDTH,
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);

  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    queryFn: getWarehousingFlowList,
    params,
    pageContext,
    searchContext,
  });
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportWarehousing,
  });

  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='raw_in_stream_table'
      />
    </Card>
  );
};

export default TableList;
