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

/**
 * 分页查询安全巡检部位
 */
export async function pageTowerSafety(params: TowerSafetyParam) {
  const res = await request.get<ApiResult<PageResult<TowerSafety>>>(
    '/tower/tower-safety/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询安全巡检部位列表
 */
export async function listTowerSafety(params?: TowerSafetyParam) {
  const res = await request.get<ApiResult<TowerSafety[]>>(
    '/tower/tower-safety',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加安全巡检部位
 */
export async function addTowerSafety(data: TowerSafety) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-safety',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改安全巡检部位
 */
export async function updateTowerSafety(data: TowerSafety) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-safety',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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