'use client' import { useState } from 'react' import { MonthlyBudget } from '@/lib/types' import { cn, getMonthName } from '@/lib/utils' interface BudgetFormProps { onSubmit: (budget: MonthlyBudget) => void onCancel: () => void initialData?: MonthlyBudget } const months = Array.from({ length: 12 }, (_, i) => ({ value: i + 1, label: getMonthName(i + 1), })) export function BudgetForm({ onSubmit, onCancel, initialData }: BudgetFormProps) { const now = new Date() const [formData, setFormData] = useState({ totalIncome: initialData?.totalIncome || 0, savingsGoal: initialData?.savingsGoal || 0, month: initialData?.month || now.getMonth() + 1, year: initialData?.year || now.getFullYear(), }) const [errors, setErrors] = useState>({}) const validate = (): boolean => { const newErrors: Record = {} if (formData.totalIncome <= 0) { newErrors.totalIncome = 'Los ingresos deben ser mayores a 0' } if (formData.savingsGoal >= formData.totalIncome) { newErrors.savingsGoal = 'La meta de ahorro debe ser menor que los ingresos' } if (formData.month < 1 || formData.month > 12) { newErrors.month = 'El mes debe estar entre 1 y 12' } if (formData.year < 2000 || formData.year > 2100) { newErrors.year = 'El año no es válido' } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (validate()) { onSubmit({ month: formData.month, year: formData.year, totalIncome: formData.totalIncome, savingsGoal: formData.savingsGoal, fixedExpenses: initialData?.fixedExpenses || 0, variableExpenses: initialData?.variableExpenses || 0, }) } } const updateField = ( field: K, value: typeof formData[K] ) => { setFormData((prev) => ({ ...prev, [field]: value })) if (errors[field]) { setErrors((prev) => { const newErrors = { ...prev } delete newErrors[field] return newErrors }) } } return (
{errors.month &&

{errors.month}

}
updateField('year', parseInt(e.target.value) || now.getFullYear())} className={cn( 'w-full px-3 py-2 bg-slate-800 border rounded-lg text-white', 'focus:outline-none focus:ring-2 focus:ring-blue-500/50 focus:border-blue-500', errors.year ? 'border-red-500' : 'border-slate-600' )} /> {errors.year &&

{errors.year}

}
updateField('totalIncome', parseFloat(e.target.value) || 0)} className={cn( 'w-full px-3 py-2 bg-slate-800 border rounded-lg text-white placeholder-slate-500', 'focus:outline-none focus:ring-2 focus:ring-blue-500/50 focus:border-blue-500', errors.totalIncome ? 'border-red-500' : 'border-slate-600' )} placeholder="0.00" /> {errors.totalIncome &&

{errors.totalIncome}

}
updateField('savingsGoal', parseFloat(e.target.value) || 0)} className={cn( 'w-full px-3 py-2 bg-slate-800 border rounded-lg text-white placeholder-slate-500', 'focus:outline-none focus:ring-2 focus:ring-blue-500/50 focus:border-blue-500', errors.savingsGoal ? 'border-red-500' : 'border-slate-600' )} placeholder="0.00" /> {errors.savingsGoal &&

{errors.savingsGoal}

} {formData.totalIncome > 0 && (

Disponible para gastos: {((formData.totalIncome - formData.savingsGoal) / formData.totalIncome * 100).toFixed(0)}%

)}
) }