'use client' import { useState } from 'react' import { FixedDebt } from '@/lib/types' import { cn } from '@/lib/utils' interface FixedDebtFormProps { initialData?: Partial onSubmit: (data: Omit) => void onCancel: () => void } const categories = [ { value: 'housing', label: 'Vivienda' }, { value: 'services', label: 'Servicios' }, { value: 'subscription', label: 'Suscripción' }, { value: 'other', label: 'Otro' }, ] as const export function FixedDebtForm({ initialData, onSubmit, onCancel }: FixedDebtFormProps) { const [formData, setFormData] = useState({ name: initialData?.name || '', amount: initialData?.amount || 0, dueDay: initialData?.dueDay || 1, category: initialData?.category || 'other', isAutoDebit: initialData?.isAutoDebit || false, notes: initialData?.notes || '', }) const [errors, setErrors] = useState>({}) const validate = (): boolean => { const newErrors: Record = {} if (!formData.name.trim()) { newErrors.name = 'El nombre es requerido' } if (formData.amount <= 0) { newErrors.amount = 'El monto debe ser mayor a 0' } if (formData.dueDay < 1 || formData.dueDay > 31) { newErrors.dueDay = 'El día debe estar entre 1 y 31' } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (validate()) { onSubmit(formData) } } 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 (
updateField('name', e.target.value)} 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.name ? 'border-red-500' : 'border-slate-600' )} placeholder="Ej: Alquiler, Internet, etc." /> {errors.name &&

{errors.name}

}
updateField('amount', 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.amount ? 'border-red-500' : 'border-slate-600' )} placeholder="0.00" /> {errors.amount &&

{errors.amount}

}
updateField('dueDay', parseInt(e.target.value) || 1)} 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.dueDay ? 'border-red-500' : 'border-slate-600' )} placeholder="1" /> {errors.dueDay &&

{errors.dueDay}

}
updateField('isAutoDebit', e.target.checked)} className="w-4 h-4 rounded border-slate-600 bg-slate-800 text-blue-500 focus:ring-blue-500/50" />