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

/**
 * 分页查询会员
 */
export async function pageManager(params: ManagerParam) {
  const res = await request.get<ApiResult<PageResult<Manager>>>(
    '/shop/manager/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据ID查询会员
 */
export async function getManager(id: number) {
  const res = await request.get<ApiResult<Manager>>('/shop/manager/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询会员列表
 */
export async function listManager(params?: ManagerParam) {
  const res = await request.get<ApiResult<Manager[]>>('/shop/manager', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加会员
 */
export async function addManager(data: Manager) {
  const res = await request.post<ApiResult<unknown>>('/shop/manager', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改会员
 */
export async function updateManager(data: Manager) {
  const res = await request.put<ApiResult<unknown>>('/shop/manager', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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