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 {
  CODE_COL_WIDTH,
  COMPANY_COL_WIDTH,
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SERIAL_COL_WIDTH,
  SMALL_TABLE_WIDTH,
  SUPPLIER_COL_WIDTH,
  WBS_COL_WIDTH,
} from '@utils';
import {exportWarehousing, getWarehousingFlowList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<WarehousingListData> = [
  {
    title: '入库单编号',
    dataIndex: 'storageCode',
    key: 'storageCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '采购订单号',
    dataIndex: 'orderCode',
    key: 'orderCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '物料编号',
    dataIndex: 'wllbCode',
    key: 'wllbCode',
    width: MATERIAL_CODE_COL_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'materialName',
    key: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '入库数量',
    dataIndex: 'capacity',
    key: 'capacity',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '采购数量',
    dataIndex: 'purchaseNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '供应商',
    dataIndex: 'supplierName',
    key: 'supplierName',
    width: SUPPLIER_COL_WIDTH,
  },
  {
    title: '所属公司',
    dataIndex: 'accountName',
    width: COMPANY_COL_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'accountSleeve',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '生产批次',
    dataIndex: 'producDate',
    key: 'producDate',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    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: WBS_COL_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: SERIAL_COL_WIDTH,
  },
];

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

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

  return (
    <Card className='table-wrapper'>
      <TableTools
        pageContext={pageContext}
        searchContext={searchContext}
        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;
