import {deleteOtherOut} from '@apis';
import {useSupertube, useTableDeleteEvent} from '@hooks';
import {OtherOutListData} 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<OtherOutListData> = [
  {title: '出库单编号', dataIndex: 'askGoodsCode', 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: 'outNum',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
  {title: '所属公司', dataIndex: 'companyName', width: COMPANY_COL_WIDTH},
  {title: '公司编号', dataIndex: 'companyNumber', width: SMALL_TABLE_WIDTH},
  {title: '领用部门', dataIndex: 'department', width: NORMAL_TABLE_WIDTH},
  {title: '通知时间', dataIndex: 'sqrq', width: NORMAL_TABLE_WIDTH},
  {title: 'WBS编号', dataIndex: 'wbs', width: WBS_COL_WIDTH},
];

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

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

  return isSuper ? tableColumns : columns;
}
