import {FC} from 'react';
import {RawMaterialOutStreamListData} 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 {exportRawOut, getRawOutList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<RawMaterialOutStreamListData> = [
  {
    title: '出库编号',
    dataIndex: 'storageCode',
    key: 'storageCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '领料单编号',
    dataIndex: 'askGoodsCode',
    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: 'num',
    key: 'num',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '领用部门',
    dataIndex: 'department',
    key: 'department',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '库位名称',
    dataIndex: 'storageLocationName',
    key: 'storageLocationName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '所属公司',
    dataIndex: 'accountName',
    width: COMPANY_COL_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'accountSleeve',
    width: SMALL_TABLE_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: 'supplierName',
    key: 'supplierName',
    width: SUPPLIER_COL_WIDTH,
  },
  {
    title: '连番号',
    dataIndex: 'serial',
    key: 'serial',
    width: SERIAL_COL_WIDTH,
  },
  {
    title: 'WBS编号',
    dataIndex: 'wbs',
    key: 'wbs',
    width: WBS_COL_WIDTH,
  },
  {
    title: '类型',
    dataIndex: 'type',
    key: 'type',
    width: NORMAL_TABLE_WIDTH,
  },
];

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

  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    queryFn: getRawOutList,
    params,
    pageContext,
    searchContext,
  });

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

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

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

export default TableList;
