import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { TowerRent, TowerRentParam } from './model';
// const merchantCode = String(localStorage.getItem('merchantCode'));

/**
 * 分页查询标准节租金
 */
export async function pageTowerRent(params: TowerRentParam) {
  const res = await request.get<ApiResult<PageResult<TowerRent>>>(
    '/tower/tower-rent/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询标准节租金列表
 */
export async function listTowerRent(params?: TowerRentParam) {
  const res = await request.get<ApiResult<TowerRent[]>>('/tower/tower-rent', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加标准节租金
 */
export async function addTowerRent(data: TowerRent) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-rent',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改标准节租金
 */
export async function updateTowerRent(data: TowerRent) {
  const res = await request.put<ApiResult<unknown>>('/tower/tower-rent', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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