/**
 * 订单管理相关API
 */

import { get, post } from '@/utils/request';

export interface OrderQueryParams {
  page?: number;
  pageSize?: number;
  orderNo?: string;
  deviceId?: string;
  payStatus?: string;
  status?: number;
  statusList?: number[];
  startDate?: string;
  endDate?: string;
}

export interface OrderListResponse {
  list: any[];
  total: number;
  page: number;
  pageSize: number;
}

/**
 * 获取订单列表
 */
export async function getOrderList(params: OrderQueryParams = {}): Promise<OrderListResponse> {
  const query = { ...params };
  if (query.status === -1) {
    delete (query as any).status;
  }
  return get('/order/list', query);
}

/**
 * 获取订单详情
 */
export async function getOrderDetail(orderId: string): Promise<any> {
  return get(`/order/${orderId}`);
}

/**
 * 获取订单统计
 */
export async function getOrderStats(params?: { startDate?: string; endDate?: string }): Promise<any> {
  return get('/order/statistics', params || {});
}

/**
 * 处理退款（支持部分退款、自定义金额、全额退款）
 */
export async function handleRefund(
  orderId: string,
  reason: string,
  data?: { products?: { productId: string; productName: string; quantity: number; price: number }[]; refundAmount?: number }
): Promise<any> {
  const body: any = { reason };
  if (data?.products?.length) body.products = data.products;
  if (data?.refundAmount) body.refundAmount = data.refundAmount;
  return post(`/order/${orderId}/refund`, body);
}

/**
 * 获取订单开门前后图片/视频（用哈哈平台 activityId，路径 id 填 0）
 */
export async function getOrderMedia(activityId: string): Promise<any> {
  return get(`/order/0/media`, { activityId });
}
