'use client'; import { useFormContext } from 'react-hook-form'; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { CreditCard, Users, PlusCircle, DollarSign } from "lucide-react"; import { memo, useState, useCallback } from 'react'; import usePricingState from '../components/usePricingState'; import PricePackageField from '../components/PricePackageField'; import { PricePackage, CURRENCY_OPTIONS } from '@/schemas/plan'; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { PlanFormValues } from '@/schemas/plan'; export const PricingSection = memo(function PricingSection() { const { toast } = useToast(); const form = useFormContext(); const [newPersons, setNewPersons] = useState(2); const [newCurrency, setNewCurrency] = useState<'COP' | 'USD' | 'EUR'>('COP'); const { addPricePackage, updatePricePackage, removePricePackage, pricePackages } = usePricingState({ form }); const handleAddPriceOption = useCallback(() => { if (newCurrency === 'USD' && newPersons < 2) { toast({ title: "Mínimo 2 personas", description: "Para planes en dólares, el número mínimo de personas es 2.", variant: "destructive" }); return; } if (newPersons <= 0) { toast({ title: "Número inválido", description: "El número de personas debe ser mayor que cero.", variant: "destructive" }); return; } if (pricePackages.some(pkg => pkg.numPersons === newPersons && pkg.currency === newCurrency)) { toast({ title: "Opción duplicada", description: `Ya existe una opción de precio para ${newPersons} personas en ${newCurrency}.`, variant: "destructive" }); return; } if (addPricePackage(newPersons, newCurrency)) { setNewPersons(prev => Math.min(prev + 1, 20)); } }, [addPricePackage, newPersons, newCurrency, pricePackages, toast]); const handleUpdatePackage = useCallback((updatedPackage: PricePackage) => { const { id, ...updates } = updatedPackage; updatePricePackage(id, updates); }, [updatePricePackage]); const handleRemovePackage = useCallback((id: string) => removePricePackage(id), [removePricePackage]); const handlePersonsChange = useCallback((e: React.ChangeEvent) => setNewPersons(parseInt(e.target.value) || 1), []); const handleCurrencyChange = useCallback((value: string) => { if (value === 'COP' || value === 'USD' || value === 'EUR') { setNewCurrency(value); if (value === 'USD' && newPersons < 2) setNewPersons(2); } }, [newPersons]); const sortedPackages = [...pricePackages].sort((a, b) => a.currency === 'USD' ? -1 : (a.numPersons - b.numPersons)); return (
{sortedPackages.length > 0 ? ( sortedPackages.map((pkg) => ( handleRemovePackage(pkg.id)} isRemovable={true} /> )) ) : (

Aún no hay opciones de precio

Usa el formulario de abajo para empezar a añadir precios.

)}

Añadir nueva opción de precio

); });