import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  TowerEquipment,
  TowerEquipmentParam
} from '@/api/tower/equipment/model';
/**
 * 分页查询设备
 */
export async function pageTowerEquipment(params: TowerEquipmentParam) {
  const res = await request.get<ApiResult<PageResult<TowerEquipment>>>(
    '/tower/tower-equipment/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

/**
 * 绑定设备
 */
export async function bindTowerEquipment(data: TowerEquipment) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-equipment/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 批量添加设备
 */
export async function addBatchTowerEquipment(data: TowerEquipment[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-equipment/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改设备状态
 */
export async function updateBatchTowerEquipment(data: TowerEquipment[]) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-equipment/status',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除设备
 */
export async function removeBatchTowerEquipment(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-equipment/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
