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

/**
 * 分页查询门店
 */
export async function pageShopStore(params: ShopStoreParam) {
  const res = await request.get<ApiResult<PageResult<ShopStore>>>(
    '/shop/shop-store/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询门店列表
 */
export async function listShopStore(params?: ShopStoreParam) {
  const res = await request.get<ApiResult<ShopStore[]>>(
    '/shop/shop-store',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加门店
 */
export async function addShopStore(data: ShopStore) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改门店
 */
export async function updateShopStore(data: ShopStore) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除门店
 */
export async function removeShopStore(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-store/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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