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

/**
 * 分页查询设备型号
 */
export async function pageTowerProvider(params: TowerProviderParam) {
  const res = await request.get<ApiResult<PageResult<TowerProvider>>>(
    '/tower/tower-provider/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询设备型号列表
 */
export async function listTowerProvider(params?: TowerProviderParam) {
  const res = await request.get<ApiResult<TowerProvider[]>>(
    '/tower/tower-provider',
    {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加设备型号
 */
export async function addTowerProvider(data: TowerProvider) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-provider',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改设备型号
 */
export async function updateTowerProvider(data: TowerProvider) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-provider',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除设备型号
 */
export async function removeTowerProvider(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-provider/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除设备型号
 */
export async function removeBatchTowerProvider(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-model/provider',
    {
      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-model/provider',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
