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

const columns: ColumnsType<OtherInListData> = [
  {title: '入库单编号', dataIndex: 'noticeCode', width: CODE_COL_WIDTH},
  {
    title: '物料编号',
    dataIndex: 'materialCode',
    width: MATERIAL_CODE_COL_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '入库数量',
    dataIndex: 'num',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '已入库数量',
    dataIndex: 'warehousingNum',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
  {title: '所属公司', dataIndex: 'companyName', width: COMPANY_COL_WIDTH},
  {title: '公司编号', dataIndex: 'companyNumber', width: SMALL_TABLE_WIDTH},
  {title: '通知时间', dataIndex: 'noticeTime', width: NORMAL_TABLE_WIDTH},
  {title: 'WBS编号', dataIndex: 'wbs', width: WBS_COL_WIDTH},
];

export function useColumns(refetch: () => void) {
  const isSuper = useSupertube();
  const [pendingId, onDelete] = useTableDeleteEvent(deleteOtherIn, refetch, {
    label: '其他入库',
  });

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

  return isSuper ? tableColumns : columns;
}
