import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CardPlan, CardPlanParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询会员卡类型
 */
export async function pageCardPlan(params: CardPlanParam) {
  const res = await request.get<ApiResult<PageResult<CardPlan>>>(
    MODULES_API_URL + '/booking/card-plan/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询会员卡类型列表
 */
export async function listCardPlan(params?: CardPlanParam) {
  const res = await request.get<ApiResult<CardPlan[]>>(
    MODULES_API_URL + '/booking/card-plan',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加会员卡类型
 */
export async function addCardPlan(data: CardPlan) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/card-plan',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改会员卡类型
 */
export async function updateCardPlan(data: CardPlan) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/card-plan',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除会员卡类型
 */
export async function removeCardPlan(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/card-plan/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除会员卡类型
 */
export async function removeBatchCardPlan(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/card-plan/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询会员卡类型
 */
export async function getCardPlan(id: number) {
  const res = await request.get<ApiResult<CardPlan>>(
    MODULES_API_URL + '/booking/card-plan/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
