import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { TowerFall, TowerFallParam } from '@/api/tower/fall/model';
/**
 * 分页查询防坠器
 */
export async function pageTowerFall(params: TowerFallParam) {
  const res = await request.get<ApiResult<PageResult<TowerFall>>>(
    '/tower/tower-fall/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询防坠器列表
 */
export async function listTowerFall(params?: TowerFallParam) {
  const res = await request.get<ApiResult<TowerFall[]>>('/tower/tower-fall', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加防坠器
 */
export async function addTowerFall(data: TowerFall) {
  const res = await request.post<ApiResult<unknown>>('/tower/tower-fall', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改防坠器
 */
export async function updateTowerFall(data: TowerFall) {
  const res = await request.put<ApiResult<unknown>>('/tower/tower-fall', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定防坠器
 */
export async function bindTowerFall(data: TowerFall) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-fall/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 批量添加防坠器
 */
export async function addBatchTowerFall(data: TowerFall[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-fall/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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