import httpApi from "@/api/indexApi";

const BASE_BOOKING = "/alienStore/store/booking/category";

/** 分类列表（预约/桌位分类，支持分页） */
export const scheduleList = (params: { storeId: number; pageNum?: number; pageSize?: number; [key: string]: any }) => {
  return httpApi.get(`${BASE_BOOKING}/list`, params);
};

/** 新增分类 */
export const scheduleAddSave = (data: {
  storeId?: number | string;
  categoryName?: string;
  isDisplay?: number;
  maxBookingTime?: number;
  [key: string]: any;
}) => {
  return httpApi.post(`${BASE_BOOKING}/add`, data);
};

/** 删除分类（application/x-www-form-urlencoded） */
export const scheduleDel = (params: { id: number | string }) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key as keyof typeof params]))}`)
    .join("&");
  return httpApi.post(`${BASE_BOOKING}/delete`, formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

/** 分类排序 */
export const scheduleSort = (data: { categoryIds: (number | string)[]; storeId: number | string }) => {
  return httpApi.post(`${BASE_BOOKING}/updateSort`, data);
};

/** 新增/编辑分类（保存或更新） */
export const scheduleAdd = (data: any) => {
  return httpApi.post(`${BASE_BOOKING}/add`, data);
};

/** 新增/编辑分类（保存或更新） */
export const scheduleEdit = (data: any) => {
  return httpApi.post(`${BASE_BOOKING}/update`, data);
};
//编辑详情
export const scheduleDetail = (params: { id: number | string }) => {
  return httpApi.get(`${BASE_BOOKING}/detail`, params);
};

// ==================== 桌位管理 ====================
const BASE_TABLE = "/alienStore/store/booking/table";

/** 桌位列表（可按位置/分类筛选，支持分页） */
export const tableList = (params: {
  storeId: number;
  categoryId?: number | string;
  pageNum?: number;
  pageSize?: number;
  [key: string]: any;
}) => {
  return httpApi.get(`${BASE_TABLE}/list`, params);
};

/** 桌位详情（编辑回显） */
export const tableDetail = (params: { id: number | string }) => {
  return httpApi.get(`${BASE_TABLE}/detail`, params);
};

/** 新增桌位（与 merchant addTableNumber 一致：storeId + categoryId + tables[]） */
export const tableAdd = (data: {
  storeId?: number | string;
  categoryId?: number | string;
  tables?: { tableNumber: string; seatingCapacity: number }[];
  [key: string]: any;
}) => {
  return httpApi.post(`${BASE_TABLE}/add`, data);
};

/** 编辑桌位（与 merchant 一致：id + categoryId + storeId + tableNumber + seatingCapacity） */
export const tableEdit = (data: {
  id: number | string;
  storeId?: number | string;
  categoryId?: number | string;
  tableNumber?: string;
  seatingCapacity?: number;
  [key: string]: any;
}) => {
  return httpApi.post(`${BASE_TABLE}/update`, data);
};

/** 删除桌位 */
export const tableDel = (params: { id: number | string }) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key as keyof typeof params]))}`)
    .join("&");
  return httpApi.post(`${BASE_TABLE}/delete`, formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

// ==================== 预约信息 ====================
const BASE_RESERVATION = "/alienStore/store/reservation";

/** 预约列表（分页，支持预订人姓名、订单状态、预订日期筛选） */
export const reservationList = (params: {
  storeId?: number | string;
  reservationUserName?: string;
  orderStatus?: number | string;
  startDate?: string;
  endDate?: string;
  dateFrom?: string;
  dateTo?: string;
  pageNum?: number;
  pageSize?: number;
  [key: string]: any;
}) => {
  return httpApi.get(`${BASE_RESERVATION}/list`, params);
};

/** 加时（与 uni 商家端一致：reservationId、addTimeMinutes、addTimeStart） */
export const reservationAddTime = (params: {
  reservationId?: number | string;
  addTimeMinutes?: number;
  addTimeStart?: string;
  [key: string]: any;
}) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
    .join("&");
  return httpApi.post(`${BASE_RESERVATION}/addTime`, formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

/** 取消预约（与 uni scheduledInfo：reservationId + cancelReason） */
export const reservationCancel = (params: {
  id?: number | string;
  reservationId?: number | string;
  cancelReason?: string;
  [key: string]: any;
}) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
    .join("&");
  return httpApi.post(`${BASE_RESERVATION}/cancel`, formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

/** 删除预约 */
export const reservationDelete = (params: { reservationId: number | string }) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key as keyof typeof params]))}`)
    .join("&");
  return httpApi.post(`${BASE_RESERVATION}/delete`, formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

/** 退款 */
export const reservationRefund = (params: { id: number | string; [key: string]: any }) => {
  const formData = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
    .join("&");
  return httpApi.post("/alienStore/user/reservation/payment/refund", formData, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  });
};

// ==================== 预约信息设置 ====================
const BASE_SETTINGS = "/alienStore/store/booking/settings";

/** 信息设置详情（回显） */
export const bookingSettingsDetail = (params: { storeId: number | string }) => {
  return httpApi.get(`${BASE_SETTINGS}/detail`, params);
};

/** 信息设置保存 */
export const bookingSettingsSave = (data: Record<string, any>) => {
  return httpApi.post(`${BASE_SETTINGS}/save`, data);
};

/** 预约营业时间（正常/特殊，用于回显预订时间） */
export const getBookingBusinessHours = (params: { settingsId?: number | string }) => {
  return httpApi.get("/alienStore/store/reservation/getBookingBusinessHours", params);
};

/** 分类下桌号是否有预定（true 则不可编辑/删除分类） */
export const getHasReservation = (params: any) => {
  return httpApi.get(`/alienStore/store/booking/category/hasReservation`, params);
};

/** 桌位是否有预定（true 则不可编辑/删除该桌） */
export const getTableHasReservation = (params: any) => {
  return httpApi.get(`/alienStore/store/booking/table/hasReservation`, params);
};
