import {FC} from 'react';
import {ColumnsType} from 'antd/es/table';
import {DeadProductsListData} from 'models/response/queryList';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {Card} from 'antd';
import {
  COMPANY_COL_WIDTH,
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SMALL_TABLE_WIDTH,
  SUPPLIER_COL_WIDTH,
  WBS_COL_WIDTH,
} from '@utils';
import {exportDeadproduct, getDeadProductsList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<DeadProductsListData> = [
  {title: '物料编号', dataIndex: 'wllbCode', width: MATERIAL_CODE_COL_WIDTH},
  {
    title: '物料名称',
    dataIndex: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '库位名称',
    dataIndex: 'storageLocationName',

    width: NORMAL_TABLE_WIDTH,
  },
  {title: '所属公司', dataIndex: 'companyName', width: COMPANY_COL_WIDTH},
  {title: '公司编号', dataIndex: 'accountSleeve', width: SMALL_TABLE_WIDTH},
  {title: '供应商', dataIndex: 'ustomerName', width: SUPPLIER_COL_WIDTH},
  {title: 'WBS编号', dataIndex: 'wbs', width: WBS_COL_WIDTH},
  {
    title: '数量',
    dataIndex: 'amount',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {title: '入库时间', dataIndex: 'scrq', width: MIDDLE_TABLE_WIDTH},
];

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

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

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

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

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

export default TabelList;
