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

/**
 * 分页查询会员卡类型
 */
export async function pageBookingCardPlan(params: BookingCardPlanParam) {
  const res = await request.get<ApiResult<PageResult<BookingCardPlan>>>(
    MODULES_API_URL + '/booking/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 listBookingCardPlan(params?: BookingCardPlanParam) {
  const res = await request.get<ApiResult<BookingCardPlan[]>>(
    MODULES_API_URL + '/booking/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 addBookingCardPlan(data: BookingCardPlan) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/booking-card-plan',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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