import {delProductionRequisition} from '@apis';
import {ColumnsType} from 'antd/es/table';
import {SemiDrawListData} from '@models';
import {Button} from 'antd';
import {
  CODE_COL_WIDTH,
  COMPANY_COL_WIDTH,
  DOUBLE_BTN_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, useTableModalEvent} from '@hooks';

const tableColumns: ColumnsType<SemiDrawListData> = [
  {
    title: '领料单编号',
    dataIndex: 'askGoodsCode',
    width: CODE_COL_WIDTH,
  },
  {
    title: '生产订单号',
    dataIndex: 'productionCode',
    key: 'productionCode',
    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: 'num',
    key: 'num',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '出库数量',
    dataIndex: 'outNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '库存数量',
    dataIndex: 'amount',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '要料部门',
    dataIndex: 'department',
    key: 'department',
    width: NORMAL_TABLE_WIDTH,
  },
  {title: '所属公司', dataIndex: 'companyName', width: COMPANY_COL_WIDTH},
  {title: '公司编号', dataIndex: 'companyNumber', width: SMALL_TABLE_WIDTH},
  {title: 'WBS编号', dataIndex: 'wbs', width: WBS_COL_WIDTH},
  {title: '分录号', dataIndex: 'entryNumber', width: ENTYR_NUMBER_COL_WIDTH},
  {title: '移动类型', dataIndex: 'moveType', width: NORMAL_TABLE_WIDTH},
  {title: '来源类型', dataIndex: 'sourceType', width: NORMAL_TABLE_WIDTH},
  {
    title: '状态',
    dataIndex: 'type',
    key: 'type',
    render(_, {type}) {
      return type === '0' ? '未出库' : '已出库';
    },
    width: SMALL_TABLE_WIDTH,
    align: 'center',
  },
  {title: '申请日期', dataIndex: 'sqrq', width: MIDDLE_TABLE_WIDTH},
];

export function useHandle(refetch: () => void) {
  const [deleteId, onDelete] = useTableDeleteEvent(
    delProductionRequisition,
    refetch,
    {
      label: '半成品领料单',
    },
  );
  const [{visible, editId: putOutId}, {onClose, onEdit}] = useTableModalEvent();

  const isSuper = useSupertube();
  const columns: ColumnsType<SemiDrawListData> = [
    ...tableColumns,
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      fixed: 'right',
      width: DOUBLE_BTN_WIDTH,
      render(_, {id, type, askGoodsCode}) {
        return (
          <>
            <Button
              type='text'
              className='ant-text-btn-color-primary'
              disabled={type !== '0' || deleteId === id}
              onClick={onEdit(id)}
            >
              {type === '0' ? '出库' : '已出库'}
            </Button>
            {isSuper && (
              <Button
                type='text'
                danger
                loading={deleteId === id}
                onClick={onDelete(id, askGoodsCode)}
              >
                删除
              </Button>
            )}
          </>
        );
      },
    },
  ];

  return [{columns, visible, putOutId}, {onClose}] as const;
}
