import React, { useState, useEffect } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { CheckCircle2, CreditCard, Circle } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { Switch } from '@/components/ui/switch'; interface Plan { id: number; name: string; price: string | number; duration: string; description?: string; features?: string[]; business?: number; max_users?: number; storage_limit?: string; is_active?: boolean; is_current?: boolean; is_default?: boolean; } interface UpgradePlanModalProps { isOpen: boolean; onClose: () => void; onConfirm: (planId: number, duration: string) => void; plans: Plan[]; currentPlanId?: number; companyName: string; } export function UpgradePlanModal({ isOpen, onClose, onConfirm, plans, currentPlanId, companyName }: UpgradePlanModalProps) { const { t } = useTranslation(); const [selectedPlanId, setSelectedPlanId] = useState(null); const [isYearly, setIsYearly] = useState(false); // Filter plans based on billing period const filteredPlans = plans.filter(plan => { const duration = plan.duration.toLowerCase(); return isYearly ? duration === 'yearly' : duration === 'monthly'; }); // Initialize with current plan ID when modal opens useEffect(() => { if (isOpen && filteredPlans && filteredPlans.length > 0) { const currentPlan = filteredPlans.find(plan => plan.is_current === true); if (currentPlan) { setSelectedPlanId(currentPlan.id); } else if (currentPlanId) { const planExists = filteredPlans.find(plan => plan.id === currentPlanId); setSelectedPlanId(planExists ? currentPlanId : filteredPlans[0].id); } else { setSelectedPlanId(filteredPlans[0].id); } } }, [isOpen, plans, isYearly]); // Reset selected plan when switching billing periods if current selection is not available useEffect(() => { if (filteredPlans.length > 0 && selectedPlanId) { const currentSelected = filteredPlans.find(plan => plan.id === selectedPlanId); if (!currentSelected) { setSelectedPlanId(filteredPlans[0].id); } } }, [isYearly]); const handleConfirm = () => { if (selectedPlanId) { onConfirm(selectedPlanId, isYearly ? 'yearly' : 'monthly'); } }; return ( {t("Upgrade Plan for Company")} {t("Select a new plan for this company")} {/* Billing Period Toggle */}
{t('Monthly')} {t('Yearly')} {isYearly && ( {t('Save up to 20%')} )}
setSelectedPlanId(parseInt(value))} className="space-y-2 pr-2" > {filteredPlans.length > 0 ? filteredPlans.map((plan) => (
setSelectedPlanId(plan.id)} >
{/* Radio Button */}
{/* Plan Content */}
{/* Plan Header */}

{plan.name}

{plan.is_current && ( {t("Current")} )}
{/* Price */}
{window.appSettings?.formatCurrency(plan.price) || `$${plan.price}`} / {plan.duration.toLowerCase()}
{/* Description */} {plan.description && (

{plan.description}

)} {/* Feature Tags */} {plan.features && plan.features.length > 0 && (
{plan.features.slice(0, 3).map((feature, index) => ( {feature} ))} {plan.features.length > 3 && ( +{plan.features.length - 3} more )}
)}
)) : (

{t('No plans available for')} {isYearly ? t('yearly') : t('monthly')} {t('billing')}

)}
); }