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

/**
 * 查询仓库列表
 */
export async function listAccessory(params?: AccessoryParam) {
  const res = await request.get<ApiResult<Accessory[]>>(
    '/tower/tower-accessory',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加仓库
 */
export async function addAccessory(data: Accessory) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-accessory',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改仓库
 */
export async function updateAccessory(data: Accessory) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-accessory',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定仓库
 */
export async function bindAccessory(data: Accessory) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-accessory/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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