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

const columns: ColumnsType<NoticeListData> = [
  {
    title: '生产订单号',
    dataIndex: 'productionCode',

    width: CODE_COL_WIDTH,
  },
  {title: '报工单编号', dataIndex: 'noticeCode', width: CODE_COL_WIDTH},
  {title: '物料编号', dataIndex: 'wllbCode', width: MATERIAL_CODE_COL_WIDTH},
  {
    title: '物料名称',
    dataIndex: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {title: '数量', dataIndex: 'num', width: SMALL_TABLE_WIDTH, align: 'right'},
  {title: '报工时间', dataIndex: 'noticeTime', width: NORMAL_TABLE_WIDTH},
  {title: '分录号', dataIndex: 'entryNumber', width: ENTYR_NUMBER_COL_WIDTH},
  {title: '所属公司', dataIndex: 'companyName', width: COMPANY_COL_WIDTH},
  {title: '公司编号', dataIndex: 'companyNumber', width: SMALL_TABLE_WIDTH},
  {title: '移动类型', dataIndex: 'moveType', width: NORMAL_TABLE_WIDTH},
  {title: '来源类型', dataIndex: 'sourceType', width: NORMAL_TABLE_WIDTH},
  {
    title: '状态',
    dataIndex: 'type',

    render(_, {type}) {
      return type === '0' ? '未入库' : '已入库';
    },
    width: SMALL_TABLE_WIDTH,
    align: 'center',
  },
];

export function useColumns(refetch: () => void) {
  const [pendingId, onDelete] = useTableDeleteEvent(deleNotice, refetch, {
    label: '产成品报工单',
  });

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

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