import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { HouseInfo, HouseInfoParam } from '@/api/house/info/model';
/**
 * 分页查询房源信息
 */
export async function pageHouseInfo(params: HouseInfoParam) {
  const res = await request.get<ApiResult<PageResult<HouseInfo>>>(
    '/house/info/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询房源信息列表
 */
export async function listHouseInfo(params?: HouseInfoParam) {
  const res = await request.get<ApiResult<HouseInfo[]>>('/house/info', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询房源信息
 */
export async function getHouseInfo(id: number) {
  const res = await request.get<ApiResult<HouseInfo>>('/house/info/' + id);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加房源信息
 */
export async function addHouseInfo(data: HouseInfo) {
  const res = await request.post<ApiResult<unknown>>('/house/info', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改房源信息
 */
export async function updateHouseInfo(data: HouseInfo) {
  const res = await request.put<ApiResult<unknown>>('/house/info', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定房源信息
 */
export async function bindHouseInfo(data: HouseInfo) {
  const res = await request.put<ApiResult<unknown>>('/house/info/bind', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量添加设备
 */
export async function addBatchHouseInfo(data: HouseInfo[]) {
  const res = await request.post<ApiResult<unknown>>('/house/info/batch', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除房源信息
 */
export async function removeHouseInfo(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/house/info/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除房源信息
 */
export async function removeBatchHouseInfo(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/house/info/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改房源信息
 */
export async function updateBatchHouseInfo(data: any) {
  const res = await request.put<ApiResult<unknown>>('/house/info/batch', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
