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: object) =>
  http.request<ResultTable>("get", "/replenishment-orders/list", { params });

/** 获取补货单详情 */
export const getOrderDetail = (id: string) =>
  http.request<Result>("get", `/replenishment-orders/${id}`);

/** 获取补货单明细列表 */
export const getOrderItems = (id: string) =>
  http.request<Result>("get", `/replenishment-orders/${id}/items`);

/** 创建补货单 */
export const createOrder = (data: object) =>
  http.request<Result>("post", "/replenishment-orders", { data });

/** 更新补货单 */
export const updateOrder = (data: object) =>
  http.request<Result>("put", "/replenishment-orders", { data });

/** 删除补货单 */
export const deleteOrder = (id: string) =>
  http.request<Result>("delete", `/replenishment-orders/${id}`);

/** 提交补货单 */
export const submitOrder = (id: string) =>
  http.request<Result>("post", `/replenishment-orders/${id}/submit`);

/** 同步到ERP */
export const syncToErp = (id: string) =>
  http.request<Result>("post", `/replenishment-orders/${id}/sync-erp`);

/** 完成补货单 */
export const completeOrder = (id: string) =>
  http.request<Result>("post", `/replenishment-orders/${id}/complete`);

/** 取消补货单 */
export const cancelOrder = (id: string) =>
  http.request<Result>("post", `/replenishment-orders/${id}/cancel`);

/** 获取补货建议（根据设备ID批量计算标准库存与当前库存差值） */
export const getReplenishmentSuggestions = (deviceIds: string[]) =>
  http.request<Result>("get", "/replenishment-orders/suggestions", {
    params: { deviceIds: deviceIds.join(",") }
  });
