import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  ProjectPlace,
  ProjectPlaceParam
} from '@/api/tower/project/place/model/index';

/**
 * 分页查询检查/保养部位配置
 */
export async function pageProjectPlace(params: ProjectPlaceParam) {
  const res = await request.get<ApiResult<PageResult<ProjectPlace>>>(
    '/tower/tower-project-place/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询检查/保养部位配置列表
 */
export async function listProjectPlace(params?: ProjectPlaceParam) {
  const res = await request.get<ApiResult<ProjectPlace[]>>(
    '/tower/tower-project-place',
    {
      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 getProjectPlace(id: number) {
  const res = await request.get<ApiResult<ProjectPlace>>(
    '/tower/tower-project-place/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加检查/保养部位配置
 */
export async function addProjectPlace(data: ProjectPlace) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-project-place',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改检查/保养部位配置
 */
export async function updateProjectPlace(data: ProjectPlace) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-project-place',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除检查/保养部位配置
 */
export async function removeProjectPlace(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-project-place/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除检查/保养部位配置
 */
export async function removeBatchProjectPlace(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-project-place/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/tower/tower-project-place/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
