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

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

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

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

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

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

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

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