import {FC} from 'react';
import {Card} from 'antd';
import {Table} from '@components';
import {context, pageContext, searchContext} from '../context';
import {SemiDrawListData} from '@models';
import {NORMAL_TABLE_WIDTH, MIDDLE_TABLE_WIDTH, SMALL_TABLE_WIDTH} from '@utils';
import {ColumnsType} from 'antd/es/table';
import {getSemiManufacturesDrawList} from '@apis';
import {useContextSection, useQueryTableList} from '@hooks';

const columns: ColumnsType<SemiDrawListData> = [
  {title: '要货单编号', dataIndex: 'askGoodsId', key: 'askGoodsId', width: NORMAL_TABLE_WIDTH},
  {title: '物料编号', dataIndex: 'materialCode', key: 'materialCode', width: NORMAL_TABLE_WIDTH},
  {title: '物料名称', dataIndex: 'materialName', key: 'materialName', width: MIDDLE_TABLE_WIDTH},
  {title: '数量', dataIndex: 'num', key: 'num', width: SMALL_TABLE_WIDTH},
  {
    title: '生产订单号',
    dataIndex: 'productionCode',
    key: 'productionCode',
    width: NORMAL_TABLE_WIDTH,
  },
  {title: '要料部门', dataIndex: 'department', key: 'department', width: NORMAL_TABLE_WIDTH},
  {
    title: '状态',
    dataIndex: 'type',
    key: 'type',
    render(_, {type}) {
      return type === '0' ? '未出库' : '已出库';
    },
    width: SMALL_TABLE_WIDTH,
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, function ([{code, type}]) {
    return {
      partType: '产成品' as const,
      productionCode: code,
      type,
      askGoodsId: '',
    };
  });

  const [{data, count}] = useQueryTableList({
    queryFn: getSemiManufacturesDrawList,
    params: params as any,
    pageContext,
    searchContext,
  });

  return (
    <>
      <Card className='table-wrapper'>
        <Table
          data-testid='semi_draw_table'
          data={data}
          columns={columns}
          count={count}
          pageContext={pageContext}
          searchContext={searchContext}
        />
      </Card>
    </>
  );
};

export default TableList;
