import {FC} from 'react';
import {ColumnsType} from 'antd/es/table';
import {FinishProductListStreamInListData} from '@models';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {
  CODE_COL_WIDTH,
  COMPANY_COL_WIDTH,
  LARGE_TABLE_WIDTH,
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SERIAL_COL_WIDTH,
  SMALL_TABLE_WIDTH,
  WBS_COL_WIDTH,
} from '@utils';
import {
  getFinishProductInStreamExport,
  getFinishProductInStreamList,
} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<FinishProductListStreamInListData> = [
  {
    title: '入库单编号',
    dataIndex: 'storageCode',
    key: 'storageCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '生产订单号',
    dataIndex: 'productionCode',
    key: 'productionCode',
    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: 'producBatch',
    key: 'producBatch',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '库位名称',
    dataIndex: 'storageLocationCode',
    key: 'storageLocationCode',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '所属公司',
    dataIndex: 'accountName',
    width: COMPANY_COL_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'companyNumber',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: 'WBS编号',
    dataIndex: 'wbs',
    key: 'wbs',
    width: WBS_COL_WIDTH,
  },
  {
    title: '工号',
    dataIndex: 'userName',
    key: 'userName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '姓名',
    dataIndex: 'realName',
    key: 'realName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '入库时间',
    dataIndex: 'scrq',
    key: 'scrq',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '连番号',
    dataIndex: 'serial',
    key: 'serial',
    width: SERIAL_COL_WIDTH,
  },
  {
    title: '属性',
    dataIndex: 'attribute',
    key: 'attribute',
    width: LARGE_TABLE_WIDTH,
  },
];

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

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

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

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

export default TableList;
