import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { OrderDeliverVO, OrderDeliverForm, OrderDeliverQuery } from '@/api/order/orderDeliver/types';

/**
 * 查询订单发货主列表
 * @param query
 * @returns {*}
 */

export const listOrderDeliver = (query?: OrderDeliverQuery): AxiosPromise<OrderDeliverVO[]> => {
  return request({
    url: '/order/orderDeliver/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询订单发货主详细
 * @param id
 */
export const getOrderDeliver = (id: string | number): AxiosPromise<OrderDeliverVO> => {
  return request({
    url: '/order/orderDeliver/' + id,
    method: 'get'
  });
};

/**
 * 新增订单发货主
 * @param data
 */
export const addOrderDeliver = (data: OrderDeliverForm) => {
  return request({
    url: '/order/orderDeliver',
    method: 'post',
    data: data
  });
};

/**
 * 修改订单发货主
 * @param data
 */
export const updateOrderDeliver = (data: OrderDeliverForm) => {
  return request({
    url: '/order/orderDeliver',
    method: 'put',
    data: data
  });
};

/**
 * 删除订单发货主
 * @param id
 */
export const delOrderDeliver = (id: string | number | Array<string | number>) => {
  return request({
    url: '/order/orderDeliver/' + id,
    method: 'delete'
  });
};

/**
 * 查询订单发货物流信息
 * @param query
 * @returns {*}
 */

export const queryTrack = (query?: OrderDeliverQuery): AxiosPromise<OrderDeliverVO[]> => {
  return request({
    url: '/order/orderDeliver/queryTrack',
    method: 'get',
    params: query
  });
};

/**
 * 根据客户ID查询发货单（分页）
 * @param customerId 客户ID
 * @param pageQuery 分页参数
 */
export const getCustomerDeliverOrders = (customerId: string | number, pageQuery?: { pageNum?: number; pageSize?: number }) => {
  return request({
    url: '/order/orderDeliver/getCustomerDeliverOrders/' + customerId,
    method: 'get',
    params: pageQuery
  });
};

/**
 * 根据多个订单ID查询对应的订单商品列表
 * @param orderIds
 */
export function getCustomerOrderProductList(orderIds: (string | number)[]) {
  if (!Array.isArray(orderIds) || orderIds.length === 0) {
    return Promise.reject(new Error('订单ID不能为空'));
  }

  // 手动构造查询参数，确保格式为 orderId=1&orderId=2&orderId=3
  const params = new URLSearchParams();
  orderIds.forEach((id) => {
    params.append('orderId', String(id));
  });

  return request({
    url: '/order/orderDeliver/getCustomerOrderProductList?' + params.toString(),
    method: 'get'
  });
}
