import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { OrderMainVO, OrderMainForm, OrderMainQuery } from '@/api/order/orderMain/types';

/**
 * 查询订单主信息列表
 * @param query
 * @returns {*}
 */

export const listOrderMain = (query?: OrderMainQuery): AxiosPromise<OrderMainVO[]> => {
  return request({
    url: '/order/orderMain/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询分配订单信息列表
 * @param query
 * @returns {*}
 */

export const listOrderAssignment = (query?: OrderMainQuery): AxiosPromise<OrderMainVO[]> => {
  return request({
    url: '/order/orderMain/assignmentList',
    method: 'get',
    params: query
  });
};

/**
 * 查询订单主信息详细
 * @param id
 */
export const getOrderMain = (id: string | number): AxiosPromise<OrderMainVO> => {
  return request({
    url: '/order/orderMain/' + id,
    method: 'get'
  });
};

/**
 * 新增订单主信息
 * @param data
 */
export const addOrderMain = (data: OrderMainForm) => {
  return request({
    url: '/order/orderMain',
    method: 'post',
    data: data
  });
};

/**
 * 修改订单主信息
 * @param data
 */
export const updateOrderMain = (data: OrderMainForm) => {
  return request({
    url: '/order/orderMain',
    method: 'put',
    data: data
  });
};

/**
 * 确认订单
 * @param data
 */
export const orderAffirm = (data: OrderMainForm) => {
  return request({
    url: '/order/orderMain/orderAffirm',
    method: 'put',
    data: data
  });
};

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

/**
 * 关闭订单
 * @param id
 */
export const closeOrderMain = (id: string | number | Array<string | number>) => {
  const ids = Array.isArray(id) ? id : [id];

  return request({
    url: '/order/orderMain/closeOrder',
    method: 'post',
    params: { ids }
  });
};

/**
 * 查询数量
 * @param query
 * @returns {*}
 */

export const queryOrderStatusStats = () => {
  return request({
    url: '/order/orderMain/queryOrderStatusStats',
    method: 'get'
  });
};

/**
 * 修改审核状态
 * @param id
 * @param checkStatus
 */
export function changeCheckStatus(id: string | number, checkStatus: string) {
  const data = {
    id,
    checkStatus
  };
  return request({
    url: '/order/orderMain/checkStatus',
    method: 'put',
    data: data
  });
}

/**
 * 修改订单状态
 * @param id
 * @param orderStatus
 */
export function changeStatus(id: string | number, orderStatus: string) {
  const data = {
    id,
    orderStatus
  };
  return request({
    url: '/order/orderMain/changeStatus',
    method: 'put',
    data: data
  });
}

/**
 * 根据多个订单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/orderMain/getCustomerOrderProductList?' + params.toString(),
    method: 'get'
  });
}
