import { http } from "@/utils/http";

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

export const getOrderList = (params: {
  page?: number;
  pageSize?: number;
  orderNo?: string;
  phone?: string;
  deviceId?: string;
  payStatus?: string;
  status?: number;
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<ResultTable>("get", "/order/list", { params });
};

export const getOrderById = (id: number) => {
  return http.request<Result>("get", `/order/${id}`);
};

export const getOrderStatistics = () => {
  return http.request<Result>("get", "/order/statistics");
};

export const refundOrder = (
  id: number,
  data: {
    reason: string;
    products?: { productId: number; productName: string; quantity: number; price: number }[];
  }
) => {
  return http.request<Result>("post", `/order/${id}/refund`, { data });
};

export const getRefundApplicationList = (params: {
  page?: number;
  pageSize?: number;
  orderNo?: string;
  status?: number;
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<ResultTable>("get", "/refund/list", { params });
};

export const getRefundApplicationDetail = (id: number) => {
  return http.request<Result>("get", `/refund/${id}`);
};

export const reviewRefundApplication = (
  id: number,
  data: { approved: boolean; remark?: string }
) => {
  return http.request<Result>("post", `/refund/${id}/review`, { data });
};

export const cancelPayScoreOrder = (data: {
  outOrderNo: string;
  reason?: string;
}) => {
  return http.request<Result>("post", "/order/payscore/cancel", { data });
};

// 获取订单图像（开门前后图片/视频）
export const getOrderMedia = (params: { orderId?: string; activityId?: string }) => {
  return http.request<Result>("get", "/order/0/media", { params });
};

// 从哈哈平台同步查询订单
export const queryOrderFromHaha = (params: { orderId?: string; activityId?: string }) => {
  return http.request<Result>("get", "/order/query-from-haha", { params });
};

// 获取静态柜分层识别结果
export const getLayerRecognize = (params: { orderId?: string; activityId?: string }) => {
  return http.request<Result>("get", "/order/layer-recognize", { params });
};

// 修复指定订单的支付分扣费（orderId/outOrderNo 以字符串传输，避免长整型精度丢失）
export const repairPayScorePayment = (orderId: string, outOrderNo?: string) => {
  return http.request<Result>("post", "/order/repair-payscore", { data: { orderId, outOrderNo } });
};

// 查询回调投递状态
export const getCallbackInfo = (params: { type: string; actId: string }) => {
  return http.request<Result>("get", "/callback/info", { params });
};