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

const columns: ColumnsType<SellOrderListData> = [
  {
    title: '销售单编号',
    dataIndex: 'deliveryCode',
    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: 'gsDeliveryNum',
    key: 'gsDeliveryNum',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '出库数量',
    dataIndex: 'outNum',
    key: 'outNum',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '取消数量',
    dataIndex: 'gsCancelNum',
    key: 'gsCancelNum',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '库存数量',
    dataIndex: 'amount',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '客户名称',
    dataIndex: 'customerName',
    key: 'customerName',
    width: SUPPLIER_COL_WIDTH,
  },
  {
    title: '所属公司',
    dataIndex: 'companyName',
    key: 'companyName',
    width: COMPANY_COL_WIDTH,
  },
  {
    title: '公司编号',
    dataIndex: 'companyNumber',
    width: SMALL_TABLE_WIDTH,
  },
  {
    title: '销售日期',
    dataIndex: 'billsTime',
    key: 'billsTime',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: 'WBS编号',
    dataIndex: 'wbs',
    key: 'wbs',
    width: WBS_COL_WIDTH,
  },
  {
    title: '来源类型',
    dataIndex: 'sourceType',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '移动类型',
    dataIndex: 'moveType',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '交货类型',
    dataIndex: 'deliveryType',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '分录号',
    dataIndex: 'entryNumber',
    width: ENTYR_NUMBER_COL_WIDTH,
  },
];

export function useColumns(refetch: () => void) {
  const [pendingId, onDelete] = useTableDeleteEvent(deleSellOrder, refetch, {
    label: `销售单`,
  });

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

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