'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { createContact, updateContact, validateVatIdFormat, type ContactResponse, type ContactCreateData, } from '@/lib/contacts'; const ROLE_OPTIONS = ['kaeufer', 'verkaeufer', 'beide']; const LEGAL_FORMS = ['GmbH', 'AG', 'KG', 'OHG', 'GbR', 'e.K.', 'UG', 'SE', 'Einzelunternehmen']; const COUNTRY_OPTIONS = [ 'DE', 'AT', 'BE', 'BG', 'CY', 'CZ', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK', ]; interface ContactFormProps { contact?: ContactResponse; mode?: 'create' | 'edit'; } interface FormErrors { [key: string]: string | undefined; } export function ContactForm({ contact, mode = 'create' }: ContactFormProps) { const router = useRouter(); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState({}); const [submitError, setSubmitError] = useState(null); const [formData, setFormData] = useState({ company_name: contact?.company_name || '', legal_form: contact?.legal_form, address_street: contact?.address_street, address_zip: contact?.address_zip, address_city: contact?.address_city, address_country: contact?.address_country || 'DE', vat_id: contact?.vat_id, phone: contact?.phone, email: contact?.email, website: contact?.website, role: contact?.role || 'kaeufer', is_private: contact?.is_private || false, }); const isEu = formData.address_country !== 'DE'; const validate = (): boolean => { const newErrors: FormErrors = {}; if (!formData.company_name || formData.company_name.trim().length === 0) { newErrors.company_name = 'Company name is required'; } if (!formData.role) { newErrors.role = 'Role is required'; } if (!formData.address_country || formData.address_country.length !== 2) { newErrors.address_country = 'Country code is required (2 letters)'; } // VAT ID validation if (formData.vat_id) { const vatError = validateVatIdFormat(formData.vat_id, formData.address_country); if (vatError) { newErrors.vat_id = vatError; } } // EU contacts should have a VAT ID (warning, not error) if (isEu && !formData.vat_id) { // Soft warning - don't block submission } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleChange = (field: keyof ContactCreateData, value: string | boolean | undefined) => { setFormData(prev => ({ ...prev, [field]: value })); if (errors[field]) { setErrors(prev => { const next = { ...prev }; delete next[field]; return next; }); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validate()) return; setLoading(true); setSubmitError(null); try { const submitData = { ...formData }; // Remove undefined empty strings Object.keys(submitData).forEach(key => { if (submitData[key as keyof ContactCreateData] === '') { (submitData as Record)[key] = undefined; } }); if (mode === 'edit' && contact) { await updateContact(contact.id, submitData); router.push(`/de/kontakte/${contact.id}`); } else { const created = await createContact(submitData); router.push(`/de/kontakte/${created.id}`); } } catch (err: unknown) { const apiErr = err as { error?: { message?: string } }; setSubmitError(apiErr?.error?.message || 'Failed to save contact'); } finally { setLoading(false); } }; const inputClass = 'w-full px-3 py-2 border rounded-lg bg-surface text-text border-border focus:outline-none focus:ring-2 focus:ring-primary'; return (
{submitError && (
{submitError}
)} {/* EU / Inland Toggle */}
handleChange('company_name', e.target.value)} error={errors.company_name} />
{errors.role &&

{errors.role}

}
{errors.address_country &&

{errors.address_country}

}
handleChange('address_street', e.target.value || undefined)} /> handleChange('address_zip', e.target.value || undefined)} /> handleChange('address_city', e.target.value || undefined)} />
handleChange('vat_id', e.target.value || undefined)} error={errors.vat_id} placeholder={isEu ? 'z.B. ATU12345678' : 'z.B. DE123456789'} /> {isEu && !formData.vat_id && (

Für EU-Kontakte wird eine USt-IdNr. empfohlen.

)}
handleChange('phone', e.target.value || undefined)} />
handleChange('email', e.target.value || undefined)} /> handleChange('website', e.target.value || undefined)} />
); }