'use client'; import { useState, useEffect } from 'react'; import { Loader2 } from 'lucide-react'; import Modal from '@/src/components/modals/Modal'; import ModalHeader from '@/src/components/modals/ModalHeader'; import ModalBody from '@/src/components/modals/ModalBody'; import ModalFooter from '@/src/components/modals/ModalFooter'; import PlanEntitlementsEditor from './PlanEntitlementsEditor'; import { getEntitlements, updateEntitlements } from '@/src/lib/api/facility-admin'; import type { MembershipPlan, PlanEntitlements } from '@/src/types/facility-admin'; interface EntitlementsConfigModalProps { isOpen: boolean; onClose: () => void; facilityId: number; plan: MembershipPlan; onSuccess: () => void; } export default function EntitlementsConfigModal({ isOpen, onClose, facilityId, plan, onSuccess }: EntitlementsConfigModalProps) { const [entitlements, setEntitlements] = useState({}); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (isOpen) { fetchEntitlements(); } }, [isOpen, plan.facility_membership_plan_id]); async function fetchEntitlements() { setLoading(true); setError(null); const result = await getEntitlements(facilityId, plan.facility_membership_plan_id); if (result.success) { setEntitlements(result.data); } else { setError(result.error.detail || 'Failed to load entitlements'); } setLoading(false); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setSubmitting(true); setError(null); const result = await updateEntitlements( facilityId, plan.facility_membership_plan_id, entitlements ); setSubmitting(false); if (result.success) { onSuccess(); onClose(); } else { setError(result.error.detail || 'Failed to update entitlements'); } } return (
Configure Entitlements: {plan.name} {error && (
{error}
)} {loading ? (
Loading entitlements...
) : (
{/* Basic Entitlements */}

Booking Limits

setEntitlements({ ...entitlements, max_active_bookings: e.target.value ? parseInt(e.target.value, 10) : undefined })} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-500" placeholder="Unlimited" disabled={submitting} />
setEntitlements({ ...entitlements, advance_window_days: e.target.value ? parseInt(e.target.value, 10) : undefined })} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-500" placeholder="Default" disabled={submitting} />
setEntitlements({ ...entitlements, daily_free_bookings: e.target.value ? parseInt(e.target.value, 10) : undefined })} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-500" placeholder="None" disabled={submitting} />
{/* Pay-Per-Court Settings */}
)}
); }