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

/**
 * 分页查询检查标准
 */
export async function pageTowerPlaceStandard(params: TowerPlaceStandardParam) {
  const res = await request.get<ApiResult<PageResult<TowerPlaceStandard>>>(
    '/tower/tower-place-standard/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询检查标准列表
 */
export async function listTowerPlaceStandard(params?: TowerPlaceStandardParam) {
  const res = await request.get<ApiResult<TowerPlaceStandard[]>>(
    '/tower/tower-place-standard',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加检查标准
 */
export async function addTowerPlaceStandard(data: TowerPlaceStandard) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-place-standard',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改检查标准
 */
export async function updateTowerPlaceStandard(data: TowerPlaceStandard) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-place-standard',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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