import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopWarehouse, ShopWarehouseParam } from './model';

/**
 * 分页查询仓库
 */
export async function pageShopWarehouse(params: ShopWarehouseParam) {
  const res = await request.get<ApiResult<PageResult<ShopWarehouse>>>(
    '/shop/shop-warehouse/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询仓库列表
 */
export async function listShopWarehouse(params?: ShopWarehouseParam) {
  const res = await request.get<ApiResult<ShopWarehouse[]>>(
    '/shop/shop-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 addShopWarehouse(data: ShopWarehouse) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-warehouse',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

/**
 * 根据id查询仓库
 */
export async function getShopWarehouse(id: number) {
  const res = await request.get<ApiResult<ShopWarehouse>>(
    '/shop/shop-warehouse/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
