/**
 * 文件职责：订单管理（全部订单）接口与类型定义。
 */
import { requestClient } from '#/api/request';

/** 全部订单状态筛选值。 */
export type OrderAllStatusFilter =
  | 'all'
  | 'cancelled'
  | 'completed'
  | 'delivering'
  | 'making'
  | 'pending'
  | 'pickup'
  | 'refunded';

/** 全部订单渠道筛选值。 */
export type OrderAllChannelFilter = 'all' | 'delivery' | 'dine_in' | 'pickup';

/** 全部订单支付方式筛选值。 */
export type OrderAllPaymentFilter =
  | 'alipay'
  | 'all'
  | 'balance'
  | 'card'
  | 'cash'
  | 'wechat';

/** 全部订单筛选参数。 */
export interface OrderAllFilterQuery {
  channel?: OrderAllChannelFilter;
  endDate?: string;
  keyword?: string;
  paymentMethod?: OrderAllPaymentFilter;
  startDate?: string;
  status?: OrderAllStatusFilter;
  storeId: string;
}

/** 全部订单列表查询参数。 */
export interface OrderAllListQuery extends OrderAllFilterQuery {
  page: number;
  pageSize: number;
}

/** 全部订单列表行。 */
export interface OrderAllListItemDto {
  amount: number;
  channel: string;
  customer: string;
  isDimmed: boolean;
  itemsSummary: string;
  orderNo: string;
  orderedAt: string;
  status: string;
}

/** 全部订单列表结果。 */
export interface OrderAllListResultDto {
  items: OrderAllListItemDto[];
  page: number;
  pageSize: number;
  total: number;
}

/** 全部订单统计。 */
export interface OrderAllStatsDto {
  averageAmount: number;
  refundCount: number;
  totalAmount: number;
  totalOrders: number;
}

/** 全部订单详情商品行。 */
export interface OrderAllDetailItemDto {
  name: string;
  quantity: number;
  spec: string;
  subTotal: number;
  unitPrice: number;
}

/** 全部订单详情时间线。 */
export interface OrderAllTimelineDto {
  label: string;
  time: string;
}

/** 全部订单详情。 */
export interface OrderAllDetailDto {
  channel: string;
  customerAddress: string;
  customerName: string;
  customerPhone: string;
  deliveryFee: number;
  discountAmount: number;
  finishedAt?: null | string;
  items: OrderAllDetailItemDto[];
  itemsAmount: number;
  orderNo: string;
  orderedAt: string;
  paidAmount: number;
  paidAt?: null | string;
  paymentMethod: string;
  remark: string;
  status: string;
  timeline: OrderAllTimelineDto[];
}

/** 全部订单导出回执。 */
export interface OrderAllExportDto {
  fileContentBase64: string;
  fileName: string;
  totalCount: number;
}

/** 查询全部订单列表。 */
export async function getOrderAllListApi(params: OrderAllListQuery) {
  return requestClient.get<OrderAllListResultDto>('/order/all/list', {
    params,
  });
}

/** 查询全部订单统计。 */
export async function getOrderAllStatsApi(params: OrderAllFilterQuery) {
  return requestClient.get<OrderAllStatsDto>('/order/all/stats', { params });
}

/** 查询全部订单详情。 */
export async function getOrderAllDetailApi(params: {
  orderNo: string;
  storeId: string;
}) {
  return requestClient.get<OrderAllDetailDto>('/order/all/detail', { params });
}

/** 导出全部订单 CSV。 */
export async function exportOrderAllCsvApi(params: OrderAllFilterQuery) {
  return requestClient.get<OrderAllExportDto>('/order/all/export', { params });
}
