import { ResPage, StoreUser } from "@/api/interface/index";
import { PORT_NONE } from "@/api/config/servicePort";
import http from "@/api";
import httpApi from "@/api/indexApi";
import { uploadFormDataSimpleCompat } from "@/api/upload.js";

/**
 * @name 商铺用户模块
 */

//获取商铺经营板块列表
export const getBusinessSection = () => {
  return httpApi.get(`/alienStorePlatform/businessSection/getFirstLevelList`, {}, { loading: false });
};

//获取商铺经营种类列表
export const getBusinessSectionTypes = params => {
  return httpApi.get(`/alienStorePlatform/businessSection/getSecondLevelList`, params, { loading: false });
};

//获取商铺经营分类
export const getThirdLevelList = params => {
  return httpApi.get(`/alienStorePlatform/businessSection/getThirdLevelList`, params, { loading: false });
};
//所在地区
export const getDistrict = (params?: any) => {
  return httpApi.get(`/alienStore/gaode/getDistrict`, params, { loading: false });
};

export const getStoreDetail = params => {
  return httpApi.get(`/alienStorePlatform/storePlatformRenovation/getDecorationDetail`, params);
};

// 获取门店装修需求列表（分页）
export const getDecorationPage = (params: any) => {
  return httpApi.get(`/alienStore/renovation/requirement/getPage`, params);
};

// 获取装修需求详情
export const getDecorationDetail = (params: { id: number | string }) => {
  return httpApi.get(`/alienStore/renovation/requirement/getDetail`, params);
};

// ==================== 聊天相关（与商家端 message API 一致） ====================
// 获取聊天记录
export const getChatRecord = (params: { receiverId: string; senderId: string }) => {
  return httpApi.get(`/alienStore/message/getMessageListByReceiverId`, params, { loading: false });
};

// 消息已读
export const messageRead = (params: { receiverId: string; senderId: string }) => {
  return httpApi.get(`/alienStore/message/read`, params, { loading: false });
};

// WebSocket 连接状态
export const socketStatus = () => {
  return httpApi.get(`/alienStore/websocket/getTokenStatus`, {}, { loading: false });
};

// 保存或更新装修需求
export const saveOrUpdateDecoration = (params: any) => {
  return httpApi.post(`/alienStore/renovation/requirement/saveOrUpdate`, params);
};

// 上传房屋图纸（统一走 @/api/upload.js → /upload/simple）
export const uploadDecorationImage = (params: FormData) => uploadFormDataSimpleCompat(params);

// 聊天图片/视频上传
export const uploadChatFile = (params: FormData) => uploadFormDataSimpleCompat(params);

// 删除装修需求
export const deleteDecoration = (params: { id: number | string }) => {
  return httpApi.post(`/alienStore/renovation/requirement/delete`, {}, { params });
};

//保存店铺信息
export const saveStoreInfo = (params: any) => {
  return httpApi.post(`/alienStore/store/info/saveOrUpdate`, params);
};

//编辑店铺信息
export const editStoreInfo = (params: any) => {
  return httpApi.post(`/alienStore/store/info/saveOrUpdate`, params);
};
// 入口图查询
export const getEntranceImg = (data: any) => {
  return httpApi.get(`/alienStore/img/getByStoreId?storeId=${data}&imgType=1`);
};
//入口图保存
export const saveEntranceImg = (params: any) => {
  return httpApi.post(`/alienStore/img/saveOrUpdateImg`, params);
};
// 门店头图查询
export const getStoreHeadImg = (data: any, imgType: any) => {
  return httpApi.get(`alienStore/img/getByStoreId?storeId=${data}&imgType=${imgType}`);
};
//门店头图保存
export const saveStoreHeadImg = (params: any) => {
  return httpApi.post(`/alienStore/img/saveOrUpdateImg`, params);
};

//批量新增或编辑营业时间
export const addOrEditBusinessTime = (params: any) => {
  return httpApi.post(`/alienStore/businessInfo/saveOrUpdateList`, params);
};

//特殊时间
export const getHolidayList = (params: any) => {
  return httpApi.get(`alienStore/coupon/getHolidayList`, params);
};

export const getStoreInfoBusinessHours = (params: any) => {
  return httpApi.get(`alienStore/store/info/getStoreInfoBusinessHours`, params);
};

//获取营业时间列表
export const getBusinessTimeList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/storePlatformBusinessInfo/getByStoreId`, params);
};

//根据门店ID和区域分类查询设施列表
export const getListByStoreIdAndCategory = (params: any) => {
  return httpApi.get(`/alienStorePlatform/bathFacilityService/getListByStoreIdAndCategory`, params);
};

//创建/更新官方相册
export const createOrUpdateOfficialAlbum = (params: any) => {
  return httpApi.post(`/alienStorePlatform/storePlatformOfficial/createOrUpdateOfficialAlbum`, params);
};
//删除官方相册
export const deleteOfficialAlbum = (params: any) => {
  return httpApi.post(`/alienStorePlatform/storePlatformOfficial/deleteOfficialAlbum`, params);
};
//获取官方相册列表
export const getOfficialAlbumList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/storePlatformOfficial/getOfficialAlbumList`, params);
};
//相册内图片存储
export const saveOfficialImg = (params: any) => {
  return httpApi.post(`/alienStore/img/saveOrUpdate`, params);
};
//相册内图片删除
export const deleteOfficialImg = (params: any) => {
  return httpApi.post(`alienStore/img/delete`, params);
};
//相册内图片查询
export const getOfficialImgList = (businessId, storeId, imgType = 2) => {
  return httpApi.get(`/alienStore/img/getByBusinessId?businessId=${businessId}&imgType=${imgType}&storeId=${storeId}`);
};
/** 门店官方视频列表 GET /alienStore/video/getByStoreId?storeId= */
export const getOfficialVideoByStoreId = (storeId: number | string) => {
  return httpApi.get(`/alienStore/video/getByStoreId`, { storeId: Number(storeId) });
};

/** 官方相册视频保存/批量保存 POST /alienStore/video/saveOrSaveBatch（参数与商家端 albumDetail 一致） */
export const saveOfficialVideoSaveOrSaveBatch = (params: {
  videoUrls: string[];
  videoIds: (number | string | null)[];
  coverUrls: string[];
  storeId: number;
  businessId: number;
}) => {
  return httpApi.post(`/alienStore/video/saveOrSaveBatch`, params);
};
//新建或修改菜品
export const createOrUpdateDish = (params: any) => {
  return httpApi.post(`/alienStorePlatform/menuPlatform/saveOrUpdate`, params);
};
//获取门店菜品列表
export const getDishList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/menuPlatform/getMenuByStoreId`, params);
};
//删除菜单
export const deleteDish = (params: any) => {
  return httpApi.get(`/alienStorePlatform/menuPlatform/delete`, params);
};
//获取菜品详情
export const getDishDetail = (params: any) => {
  return httpApi.get(`/alienStorePlatform/menuPlatform/getMenuInfo`, params);
};
// ==================== 酒单管理相关接口（新接口） ====================
//根据门店ID和菜单类型查询酒单列表
export const getBarMenuList = (params: { storeId: number; dishMenuType?: number }) => {
  return httpApi.get(`/alienStorePlatform/barMenu/getListByStoreIdAndType`, params);
};
//新增或修改酒单内容
export const saveOrUpdateBarMenu = (params: any) => {
  return httpApi.post(`/alienStorePlatform/barMenu/saveOrUpdate`, params);
};
//删除酒单
export const deleteBarMenu = (params: { id: number }) => {
  return httpApi.get(`/alienStorePlatform/barMenu/delete`, params);
};
//下载酒单Excel模板
export const downloadBarMenuTemplate = () => {
  return httpApi.get(`/alienStorePlatform/barMenu/downloadTemplate`, {}, { responseType: "blob" });
};
//导入酒单Excel
export const importBarMenuExcel = (formData: FormData, storeId: string | number) => {
  return httpApi.service.request({
    method: "POST",
    url: `/alienStorePlatform/barMenu/importMenu`,
    params: { storeId },
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};
//获取所有标签类型
export const getAllTagType = (params?: any) => {
  return httpApi.get(`/alienStorePlatform/storePlatformTag/getBusinessRelationTagList`, params);
};
/** 门店标签列表/数据：GET /alienStore/store/tag/getTagList */
export const getTagList = (params?: Record<string, any>) => {
  return httpApi.get(`/alienStore/store/tag/getTagList`, params, { loading: false });
};
//获取所有所选标签
export const getAllTag = (params: any) => {
  return httpApi.get(`/alienStorePlatform/storePlatformTag/getTagStoreRelationByStoreId`, params);
};
//保存标签
export const saveTag = (params: any) => {
  return httpApi.post(`/alienStorePlatform/storePlatformTag/createOrUpdateTagStoreRelation`, params);
};
//新建或修改人员配置
export const createOrUpdatePersonnel = (params: any) => {
  return httpApi.post(`/alienStore/storeStaffConfig/addOrUpdateStaffConfig`, params);
};
//获取门店人员列表
export const getPersonnelList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/storeStaffConfig/getListByStoreId`, params);
};
//删除人员
export const deletePersonnel = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffConfig/deleteStaffConfig`, params);
};
//获取人员详情
export const getPersonnelDetail = (params: any) => {
  return httpApi.get(`/alienStorePlatform/storeStaffConfig/getById`, params);
};
// ==================== 设施与服务相关接口 ====================
// 服务管理相关接口
//获取服务列表
export const getServiceList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/servicePlatform/getServiceList`, params);
};
//新建或修改服务
export const createOrUpdateService = (params: any) => {
  return httpApi.post(`/alienStorePlatform/bathFacilityService/saveOrUpdate`, params);
};
//获取服务详情
export const getServiceDetail = (params: any) => {
  return httpApi.get(`/alienStorePlatform/bathFacilityService/getById`, params);
};
//删除服务
export const deleteService = (params: any) => {
  return httpApi.get(`/alienStorePlatform/bathFacilityService/delete`, params);
};
//下载服务Excel模板
export const downloadServiceTemplate = () => {
  return httpApi.get(`/alienStorePlatform/bathFacilityService/downloadTemplate`, {}, { responseType: "blob" });
};
//导入服务Excel
export const importServiceExcel = (formData: FormData, storeId: string | number) => {
  return httpApi.service.request({
    method: "POST",
    url: `/alienStorePlatform/bathFacilityService/importFacility`,
    params: { storeId },
    data: formData
  });
};
// ==================== 设施区域管理（与商家端 facilityOrService 一致，alienStore 前缀） ====================
// 获取设施区域列表（标签） /alienStore/sports/facility/area/list
export const getSportsFacilityAreaList = (params: { storeId: number }) => {
  return httpApi.get(`/alienStore/sports/facility/area/list`, params, { loading: false });
};
// 新建设施区域
export const createSportsFacilityArea = (params: { storeId: number; facilityCategoryName: string }) => {
  return httpApi.post(`/alienStore/sports/facility/area/create`, params);
};
// 编辑设施区域
export const updateSportsFacilityArea = (params: { areaId: number; areaName: string }) => {
  return httpApi.post(`/alienStore/sports/facility/area/update`, params);
};
// 更新设施区域实景图片
export const updateSportsFacilityAreaHeadUrl = (params: { areaId: number; areaHeadUrl: string }) => {
  return httpApi.post(`/alienStore/sports/facility/area/updateHeadUrl`, params);
};
// 批量删除设施区域
export const batchDeleteSportsFacilityArea = (params: { storeId: number; areaIds: (string | number)[] }) => {
  return httpApi.post(`/alienStore/sports/facility/area/batchDelete`, params);
};
// 根据区域ID获取设施列表 /alienStore/sports/equipment/facility/listByArea
export const getSportsEquipmentFacilityListByArea = (params: { storeId: number; areaId: number }) => {
  return httpApi.get(`/alienStore/sports/equipment/facility/listByArea`, params);
};
// 新增运动设施
export const saveSportsEquipmentFacility = (params: any) => {
  return httpApi.post(`/alienStore/sports/equipment/facility/save`, params);
};
// 修改运动设施
export const updateSportsEquipmentFacility = (params: any) => {
  return httpApi.post(`/alienStore/sports/equipment/facility/update`, params);
};
// 删除运动设施
export const deleteSportsEquipmentFacility = (params: { equipmentId: number }) => {
  return httpApi.post(`/alienStore/sports/equipment/facility/delete`, params);
};

// 设施管理相关接口（兼容旧接口，使用 facilityCategory）
//获取健身设施列表
export const getFacilityList = (params: any) => {
  return httpApi.get(`/alienStorePlatform/sportsEquipmentFacility/getListByStoreIdAndCategory`, params);
};
//新建或修改健身设施
export const createOrUpdateFacility = (params: any) => {
  return httpApi.post(`/alienStorePlatform/sportsEquipmentFacility/saveOrUpdate`, params);
};
//获取健身设施详情
export const getFacilityDetail = (params: any) => {
  return httpApi.get(`/alienStorePlatform/sportsEquipmentFacility/getById`, params);
};
//删除设施
export const deleteFacility = (params: any) => {
  return httpApi.get(`/alienStorePlatform/sportsEquipmentFacility/delete`, params);
};
//下载设施Excel模板
export const downloadFacilityTemplate = () => {
  return httpApi.get(`/alienStorePlatform/sportsEquipmentFacility/downloadTemplate`, {}, { responseType: "blob" });
};
//导入设施Excel
export const importFacilityExcel = (formData: FormData, storeId: string | number) => {
  return httpApi.service.request({
    method: "POST",
    url: `/alienStorePlatform/bathFacilityService/importFacility`,
    params: { storeId },
    data: formData
  });
};
//下载菜单Excel模板
export const downloadExcelTemplate = () => {
  return httpApi.get(`/alienStorePlatform/menuPlatform/downloadTemplate`, {}, { responseType: "blob" });
};
//导入菜单Excel
export const importExcel = (formData: FormData, storeId: string | number) => {
  // 对于GET请求发送FormData，需要直接使用axios实例
  // axios的get方法可以在config中设置data属性（虽然不符合HTTP规范，但技术上可行）
  return httpApi.service.request({
    method: "POST",
    url: `/alienStorePlatform/menuPlatform/importMenu`,
    params: { storeId },
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

//获取人员配置列表（搜索用）
export const getStaffConfigList = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffConfig/getStaffConfigList`, params);
};
// 设置置顶状态
export const setTopStatus = (params: { id: string | number; topStatus: 0 | 1 }) => {
  return httpApi.get(`/alienStore/storeStaffConfig/setTopStatus`, params);
};
// 设置在线状态
export const setOnlineStatus = (params: { id: string | number; onlineStatus: 0 | 1 }) => {
  return httpApi.get(`/alienStore/storeStaffConfig/setOnlineStatus`, params);
};

//获取人员配置详情（查看详情用）
export const getStaffConfigDetail = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffConfig/getStaffConfigDeatail`, params);
};
//创建标题
export const createTitle = (params: any) => {
  return httpApi.post(`/alienStore/storeStaffTitle/createTitle`, params);
};
//修改标题
export const updateTitle = (params: any) => {
  return httpApi.post(`/alienStore/storeStaffTitle/updateTitle`, params);
};
//获取标题列表
export const getTitleList = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffTitle/getTitleList`, params, { loading: false }).catch((error: any) => {
    // 静默处理错误，避免显示错误横幅
    // 如果接口不存在（404），返回一个空数据格式，而不是抛出错误
    if (error?.response?.status === 404 || error?.message?.includes("不存在")) {
      return Promise.resolve({ code: 200, data: [], msg: "" });
    }
    // 其他错误也返回空数据，避免显示错误横幅
    return Promise.resolve({ code: 200, data: [], msg: "" });
  });
};
//获取标题详情（通过storeId查询）
export const getTitleDetail = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffTitle/detail`, params, { loading: false }).catch((error: any) => {
    // 静默处理错误，避免显示错误横幅
    if (error?.response?.status === 404 || error?.message?.includes("不存在")) {
      return Promise.resolve({ code: 200, data: null, msg: "" });
    }
    return Promise.resolve({ code: 200, data: null, msg: "" });
  });
};
//删除标题
export const deleteTitle = (params: any) => {
  return httpApi.get(`/alienStore/storeStaffTitle/deleteTitle`, params);
};
