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

/**
 * 分页查询小区
 */
export async function pageShopCommunity(params: ShopCommunityParam) {
  const res = await request.get<ApiResult<PageResult<ShopCommunity>>>(
    '/shop/shop-community/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询小区列表
 */
export async function listShopCommunity(params?: ShopCommunityParam) {
  const res = await request.get<ApiResult<ShopCommunity[]>>(
    '/shop/shop-community',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加小区
 */
export async function addShopCommunity(data: ShopCommunity) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-community',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改小区
 */
export async function updateShopCommunity(data: ShopCommunity) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-community',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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