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

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

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

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

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

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

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