import {ColumnsType} from 'antd/es/table';
import {RelocationOrderListData} from '@models';
import {
  CODE_COL_WIDTH,
  COMPANY_COL_WIDTH,
  ENTYR_NUMBER_COL_WIDTH,
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SMALL_TABLE_WIDTH,
  WBS_COL_WIDTH,
} from '@utils';
import {useSupertube, useTableDeleteEvent} from '@hooks';
import {deleRelocationOrder} from '@apis';
import {Button} from 'antd';

const columns: ColumnsType<RelocationOrderListData> = [
  {
    title: '移库单编号',
    dataIndex: 'warehouseTransferCode',
    key: 'warehouseTransferCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '物料编号',
    dataIndex: 'materialCode',
    key: 'materialCode',
    width: MATERIAL_CODE_COL_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'materialName',
    key: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '要货仓库',
    dataIndex: 'askGoodsWarehouseName',
    key: 'askGoodsWarehouseName',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '出货仓库',
    dataIndex: 'supplyWarehouseName',
    key: 'supplyWarehouseName',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '移库数量',
    dataIndex: 'num',
    key: 'num',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '出库数量',
    dataIndex: 'outNum',
    key: 'outNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '所属公司',
    dataIndex: 'companyName',
    key: 'companyName',
    width: COMPANY_COL_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'accountSleeve',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '移库类型',
    dataIndex: 'warehouseTransferType',
    key: 'warehouseTransferType',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: 'WBS编号',
    dataIndex: 'wbs',
    key: 'wbs',
    width: WBS_COL_WIDTH,
  },
  {
    title: '分录号',
    dataIndex: 'entryNumber',
    width: ENTYR_NUMBER_COL_WIDTH,
  },
  {
    title: '状态',
    dataIndex: 'type',
    key: 'type',
    width: SMALL_TABLE_WIDTH,
    render(_, {type}) {
      return type === '1' ? '已移库' : '未移库';
    },
    align: 'center',
  },
  {
    title: '移库日期',
    dataIndex: 'documentTime',
    key: 'documentTime',
    width: NORMAL_TABLE_WIDTH,
  },
];

export function useColumns(refetch: () => void) {
  const [pendingId, onDelete] = useTableDeleteEvent(
    deleRelocationOrder,
    refetch,
    {
      label: `移库单`,
    },
  );

  const delColumns: ColumnsType<RelocationOrderListData> = [
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      fixed: 'right',
      width: SMALL_TABLE_WIDTH,
      render(_, {id, warehouseTransferCode}) {
        return (
          <>
            <Button
              type='text'
              danger
              loading={pendingId === id}
              onClick={onDelete(id, warehouseTransferCode)}
            >
              删除
            </Button>
          </>
        );
      },
    },
  ];

  const isSuper = useSupertube();
  return isSuper ? [...columns, ...delColumns] : columns;
}
