import { overseasHttp } from '@/shared/api/overseasHttp' import { unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import { listSharedReceivingOverseasWarehouseOptions } from '@/modules/iam' import { normalizeOverseasPage, type OverseasPagePayload } from './lastMileOrders' export interface RelabelOrderQuery { page: number size: number relabelNo?: string skuCode?: string status?: string } export interface CreateRelabelItemPayload { skuCode: string productNameZh: string totalPackageCount: number notifiedPackageCount: number shipmentNo: string newSkuCode: string labelRequirement?: string productImageFileId?: string productImageFileName?: string productImageUrl?: string trackingCode?: string relabelFileId?: string relabelFileName?: string relabelFileUrl?: string } export interface CreateRelabelPayload { customerOrderNo: string overseasWarehouseId: string labelDeadline: string contactName: string contactPhone: string chargeAmount: number remark?: string idempotencyKey: string items: CreateRelabelItemPayload[] } export interface RelabelWarehouseOptionDto { id: string customerId: string warehouseCode: string warehouseName: string warehouseType: string status: string } export interface RelabelSummaryDto { id: string relabelNo: string lastlegOrderId?: string | null lastlegOrderNo?: string | null customerOrderNo?: string | null overseasWarehouseId?: string | null labelDeadline?: string | null contactName?: string | null contactPhone?: string | null status?: string | null itemCount?: number | null totalPackageCount?: number | null notifiedPackageCount?: number | null skuCodes?: string | null totalFileId?: string | null totalFileName?: string | null totalFileUrl?: string | null chargeAmount?: number | string | null chargeCurrency?: string | null walletStatus?: string | null walletErrorMessage?: string | null createdAt?: string | null } export interface RelabelItemDto { id: string skuCode?: string | null productNameZh?: string | null totalPackageCount?: number | null notifiedPackageCount?: number | null newSkuCode?: string | null labelRequirement?: string | null relabelFileId?: string | null relabelFileName?: string | null relabelFileUrl?: string | null shipmentNo?: string | null trackingCode?: string | null remark?: string | null cartonMark?: string | null fbaCartonMark?: string | null destinationWarehouseId?: string | null } export interface RelabelHistoryDto { fromStatus?: string | null toStatus?: string | null operatorId?: string | null remark?: string | null createdAt?: string | null } export interface RelabelDetailDto { application: RelabelSummaryDto remark?: string | null items: RelabelItemDto[] history: RelabelHistoryDto[] } export async function listRelabelOrders(query: RelabelOrderQuery) { const response = await overseasHttp.get>, ApiEnvelope>>( '/platform/v1/last-leg/relabel-applications', { params: query }, ) return normalizeOverseasPage(unwrapApiResponse(response)) } export async function getRelabelOrder(relabelNo: string) { const response = await overseasHttp.get, ApiEnvelope>( `/platform/v1/last-leg/relabel-applications/${encodeURIComponent(relabelNo)}`, ) return unwrapApiResponse(response) } export async function listRelabelWarehouseOptions() { const records: RelabelWarehouseOptionDto[] = (await listSharedReceivingOverseasWarehouseOptions()).map((warehouse) => ({ id: warehouse.id, customerId: '0', warehouseCode: warehouse.warehouseCode, warehouseName: warehouse.warehouseName, warehouseType: 'OVERSEAS', status: 'ACTIVE', })) return { records, page: 1, size: 200, total: records.length, pages: records.length ? 1 : 0 } } export async function createRelabelOrder(customerId: string, payload: CreateRelabelPayload) { const response = await overseasHttp.post, ApiEnvelope>( '/platform/v1/last-leg/relabel-applications', payload, { params: { customerId } }, ) return unwrapApiResponse(response) } export async function changeRelabelOrderStatus(relabelNo: string, status: string, remark?: string) { const response = await overseasHttp.put, ApiEnvelope>( `/platform/v1/last-leg/relabel-applications/${encodeURIComponent(relabelNo)}/status`, { status, remark }, ) return unwrapApiResponse(response) } export async function uploadRelabelPdf(relabelNo: string, file: File, scope: 'TOTAL' | 'ITEM', itemId?: string) { const body = new FormData() body.append('file', file) const response = await overseasHttp.post, ApiEnvelope>( `/platform/v1/last-leg/relabel-applications/${encodeURIComponent(relabelNo)}/files`, body, { params: { scope, itemId }, headers: { 'Content-Type': 'multipart/form-data' } }, ) return unwrapApiResponse(response) } export async function downloadRelabelPdf(relabelNo: string, fileId: string, scope: 'TOTAL' | 'ITEM', itemId?: string) { return overseasHttp.get( `/platform/v1/last-leg/relabel-applications/${encodeURIComponent(relabelNo)}/files/${encodeURIComponent(fileId)}/download`, { params: { scope, itemId }, responseType: 'blob' }, ) }