import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { TowerPlace, TowerPlaceParam } from './model';

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

/**
 * 查询检查/保养部位列表
 */
export async function listTowerPlace(params?: TowerPlaceParam) {
  const res = await request.get<ApiResult<TowerPlace[]>>('/tower/tower-place', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

/**
 * 批量删除检查/保养部位
 */
export async function removeBatchTowerPlace(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-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-place/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
