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

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

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

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

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

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

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