import axios from './axios'
import type { BasePaginationData, BaseRespData } from '@/types/api'
import type {
  BatchManageData,
  FactoryOrderNewListData,
  LogListData,
  operateOrderListData,
  PickCompleteData,
  PickFailData,
  ProductListData,
  RestockData,
  SearchForm,
  StatusTreeNode,
} from '@/types/api/factoryOrderNew'
import { ResultInfoDataItem } from '@/types/api/order/common'
import type { OrderData } from '@/types/api/podMakeOrder'
export function getPodOrderStateGroupListApi() {
  return axios.get<never, BaseRespData<StatusTreeNode[]>>(
    'factory/podOrder/findStateGroupList',
  )
}

export function getFactoryOrderNewListApi(
  data: SearchForm,
  currentPage: number,
  pageSize: number,
  status?: string,
  subStatus?: number,
) {
  return axios.post<never, BasePaginationData<FactoryOrderNewListData>>(
    'factory/podOrder/list_page',
    {
      ...data,
      currentPage,
      pageSize,
      status,
      acceptedStatus: subStatus,
    },
  )
}

/** 请求体与 `factory/podOrder/list_page` 一致 */
export function getPodOrderAcceptedStatisticsApi(
  data: SearchForm,
  currentPage: number,
  pageSize: number,
  status?: string,
  subStatus?: number,
) {
  return axios.post<
    never,
    BaseRespData<{
      acceptedOutOfStockCount?: number
      pendingCount?: number
      totalCount?: number
    }>
  >('factory/podOrder/acceptedStatistics', {
    ...data,
    currentPage,
    pageSize,
    status,
    acceptedStatus: subStatus,
  })
}

export function getFactoryOrderNewDetailApi(id: number | string) {
  return axios.get<never, BaseRespData<ProductListData[]>>(
    'factory/podOrderProduct/getListByPodOrderId',
    {
      params: { id },
    },
  )
}

export function getFactoryOrderNewLogApi(id: number | string) {
  return axios.get<never, BaseRespData<LogListData[]>>(
    'factory/podOrderLog/getPodOrderLog',
    {
      params: { id },
    },
  )
}

export function confirmOrderApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>('factory/orderNew/confirm', {
    ids,
  })
}

export function cancelOrderApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>('factory/orderNew/cancel', {
    ids,
  })
}

export function refreshProductInfoApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderProduct/refreshProductInfo',
    { ids },
  )
}

export function transferOldFlowApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/orderNew/transferOldFlow',
    { ids },
  )
}

export function confirmOrderWithWarehouseApi(
  ids: (number | string)[],
  warehouseId: number | string,
  warehouseName: string,
) {
  return axios.post<never, BaseRespData<ResultInfoDataItem[]>>(
    'factory/podOrder/ordersAccepted',
    {
      podOrderIds: ids,
      warehouseId,
      warehouseName,
    },
  )
}

export function cancelOrderWithReasonApi(
  ids: (number | string)[],
  reason: string,
) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrder/cancelOrders',
    {
      podOrderIds: ids,
      cancelReason: reason,
    },
  )
}

export function suspendOrderApi(
  ids: (number | string)[],
  reason: string,
  needCustomerHandle: string,
) {
  return axios.post<never, BaseRespData<void>>('factory/orderNew/suspend', {
    podOrderIds: ids,
    pauseReason: reason,
    needCustomerHandle,
  })
}

export function cancelSuspendApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>('factory/podOrder/unSuspend', {
    podOrderIds: ids,
  })
}

export function archiveOrderApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>('factory/orderNew/archive', {
    ids,
  })
}

export function printPickOrderApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderOperation/printPickPdf',
    { ids },
  )
}

export function applyReplenishApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderOperation/applyForReplenishment',
    ids,
  )
}

export function batchDeleteApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderBatchDownload/delete',
    {
      ids: ids.join(','),
    },
  )
}

export function restockCheckApi(id: number | string) {
  return axios.post<never, BaseRespData<void>>(
    'factory/orderNew/restockCheck',
    { id },
  )
}

export function pickCompleteByIdsDataApi(ids: (number | string)[]) {
  return axios.post<
    never,
    BaseRespData<{
      overallMessage?: string
      pickingSituationList?: PickCompleteData[]
      allAvailableOrderIds?: number[]
      partialAvailableOrderIds?: number[]
      unavailableOrderIds?: number[]
    }>
  >('factory/podOrderOperation/getPickingSituation', ids)
}

export function pickCompleteApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderOperation/pickingComplete',
    ids,
  )
}

export function replenishmentCompleteApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrderOperation/replenishmentComplete',
    ids,
  )
}

export function pickFailApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<PickFailData[]>>(
    'factory/podOrderOperation/listByIds',
    { ids },
  )
}

export function getCardLayoutListApi(
  data: Record<string, unknown>,
  currentPage: number,
  pageSize: number,
) {
  return axios.post<never, BasePaginationData<FactoryOrderNewListData>>(
    'factory/podOrderOperation/list_page',
    { ...data, currentPage, pageSize },
  )
}

export function getSuspendListApi(
  data: SearchForm,
  currentPage: number,
  pageSize: number,
  statusCode?: string,
) {
  return axios.post<never, BasePaginationData<FactoryOrderNewListData>>(
    'factory/podOrderPauseControl/list_page',
    { ...data, currentPage, pageSize, statusCode },
  )
}

export function getSuspendDetailApi(id: number | string) {
  return axios.get<never, BaseRespData<ProductListData[]>>(
    'factory/podOrderPauseControl/get',
    { params: { id } },
  )
}

export function getSuspendLogApi(id: number | string) {
  return axios.get<never, BaseRespData<LogListData[]>>(
    'factory/podOrderPauseControl/getLog',
    { params: { id } },
  )
}

export function getRestockListApi(
  data: Record<string, unknown>,
  currentPage: number,
  pageSize: number,
) {
  return axios.post<never, BasePaginationData<RestockData>>(
    'factory/podOrder/awaitingRestockListPage',
    { ...data, currentPage, pageSize },
  )
}

export function getBatchManageListApi(
  data: Record<string, unknown>,
  currentPage: number,
  pageSize: number,
) {
  return axios.post<never, BasePaginationData<BatchManageData>>(
    'factory/podOrderBatchDownload/list_page',
    { ...data, currentPage, pageSize },
  )
}

export function createOutboundOrderApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<void>>(
    'factory/orderNew/createOutboundOrder',
    { ids },
  )
}
// 获取跟踪号
export function getTrackingNumberApi(orderIds: (string | number)[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/getTrackingNumber',
    orderIds,
  )
}
// 获取打印面单
export function getfaceSimplexFileApi(orderIds: (string | number)[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/getfaceSimplexFile',
    orderIds,
  )
}
// 取消物流订单
export function cancelLogisticsOrderApi(orderIds: (string | number)[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/cancelLogisticsOrder',
    orderIds,
  )
}
export function arrangeFinishApi(
  url: string,
  params: {
    productIdList: number[]
    templateWidth?: number
    type?: string
  },
) {
  return axios.post<never, BaseRespData<never>>(url, {
    operationIdList: params.productIdList,
    templateWidth: params.templateWidth,
    type: params.type,
  })
}

export function downloadMaterialApi(id: number[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/downloadDesignImages',
    id,
  )
}

export function downloadOperationMaterialApi(id: number[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrderOperation/downloadDesignImages',
    id,
  )
}

export function downloadBatchMaterialApi(id: number | string) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrderBatchDownload/download',
    id,
  )
}

export function composingNewPodOrderDesignImages(
  data: number[],
  type?: string,
  templateWidth?: number,
) {
  return axios.post<never, BaseRespData<never>>(
    `factory/podOrderOperation/replenishmentComposingDesignImages?type=${type}&templateWidth=${templateWidth}`,
    data,
  )
}

export function printNewPodOrderProductionOrderApi(ids: number[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrderOperation/printProducePdf',
    ids,
  )
}

export function syncReceiverAddress(data: number[]) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/syncReceiverAddress',
    data,
  )
}

export function updateRemarkApi(id: number | string, remark: string) {
  return axios.post<never, BaseRespData<void>>(
    'factory/podOrder/updateRemark',
    { id, remark },
  )
}

export function applyForReplenishByIdApi(
  ids: (number | string)[],
  url: string,
) {
  return axios.post<never, BaseRespData<operateOrderListData[]>>(url, ids)
}

export function completeDeliveryApi(ids: (number | string)[]) {
  return axios.post<never, BaseRespData<ResultInfoDataItem[]>>(
    'factory/podOrderOperation/completeDelivery',
    ids,
  )
}

export function getByOperationNoLogApi(operationNo: string) {
  return axios.get<never, BaseRespData<operateOrderListData>>(
    'factory/podOrderOperation/getByOperationNoLog',
    {
      params: { operationNo },
    },
  )
}

export function listByNoPodOrderApi(params: {
  type: string
  no: string
  logisticsCompanyCode?: string
}) {
  return axios.get<never, BaseRespData<never>>(
    'factory/podOrder/listByNo',
    { params },
  )
}

export function orderWeighingPodOrderApi(
  params: unknown,
) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrder/orderWeighing',
    params as {
      podOrderWeighingParams: { id?: string; outWarehouseWeight?: string }[]
    },
  )
}

// 单件打单：根据 SKU/单号查询明细
export function getSingleQueryPodOrderApi(
  code: string,
  factoryNo: number,
  warehouseId: number | string,
) {
  return axios.get<never, BaseRespData<OrderData>>(
    'factory/podOrder/getDetailsBySkuOrNo',
    {
      params: {
        podOrderNo: code,
        factoryNo,
        warehouseId,
      },
    },
  )
}

// 单件打单完成
export function submitPodPrintOrderCompleteApi(
  data: { orderParamList: { id: number }[] },
  warehouseId: number | string,
  boxIndex: number | null = 0,
) {
  const params = new URLSearchParams()
  params.set('warehouseId', String(warehouseId))
  if (boxIndex !== undefined && boxIndex !== null) {
    params.set('box', String(boxIndex))
  }

  return axios.post<never, BaseRespData<never>>(
    `factory/podOrderPacking/podPrintOrderComplete?${params.toString()}`,
    data,
  )
}
