'use client'; import { useState, useEffect, useCallback } from 'react'; import { useRouter } from 'next/navigation'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Modal } from '@/components/ui/Modal'; import { Card } from '@/components/ui/Card'; import { getContact, deleteContact, addContactPerson, removeContactPerson, type ContactResponse, type ContactPersonResponse, } from '@/lib/contacts'; interface ContactDetailProps { contactId: string; } function formatDate(dateStr?: string): string { if (!dateStr) return '—'; return new Date(dateStr).toLocaleDateString('de-DE'); } export function ContactDetail({ contactId }: ContactDetailProps) { const router = useRouter(); const [contact, setContact] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [showPersonModal, setShowPersonModal] = useState(false); const [personForm, setPersonForm] = useState({ name: '', function: '', phone: '', email: '' }); const [personError, setPersonError] = useState(null); const [addingPerson, setAddingPerson] = useState(false); const fetchContact = useCallback(async () => { setLoading(true); setError(null); try { const data = await getContact(contactId); setContact(data); } catch (err: unknown) { const apiErr = err as { error?: { message?: string } }; setError(apiErr?.error?.message || 'Failed to load contact'); } finally { setLoading(false); } }, [contactId]); useEffect(() => { fetchContact(); }, [fetchContact]); const handleDelete = async () => { if (!contact) return; if (!confirm('Diesen Kontakt löschen?')) return; try { await deleteContact(contact.id); router.push('/de/kontakte'); } catch (err: unknown) { const apiErr = err as { error?: { message?: string } }; setError(apiErr?.error?.message || 'Failed to delete contact'); } }; const handleAddPerson = async (e: React.FormEvent) => { e.preventDefault(); if (!contact || !personForm.name.trim()) return; setAddingPerson(true); setPersonError(null); try { await addContactPerson(contact.id, { name: personForm.name, function: personForm.function || undefined, phone: personForm.phone || undefined, email: personForm.email || undefined, }); setShowPersonModal(false); setPersonForm({ name: '', function: '', phone: '', email: '' }); await fetchContact(); } catch (err: unknown) { const apiErr = err as { error?: { message?: string } }; setPersonError(apiErr?.error?.message || 'Failed to add person'); } finally { setAddingPerson(false); } }; const handleRemovePerson = async (personId: string) => { if (!contact) return; if (!confirm('Diese Kontaktperson entfernen?')) return; try { await removeContactPerson(contact.id, personId); await fetchContact(); } catch (err: unknown) { const apiErr = err as { error?: { message?: string } }; setError(apiErr?.error?.message || 'Failed to remove person'); } }; if (loading) { return (
Loading contact...
); } if (error) { return (
{error}
); } if (!contact) { return (
Contact not found
); } const fields: { label: string; value: string | number | undefined | null }[] = [ { label: 'Firmenname', value: contact.company_name }, { label: 'Rechtsform', value: contact.legal_form }, { label: 'Straße', value: contact.address_street }, { label: 'PLZ', value: contact.address_zip }, { label: 'Stadt', value: contact.address_city }, { label: 'Land', value: contact.address_country }, { label: 'USt-IdNr.', value: contact.vat_id }, { label: 'USt-Status', value: contact.vat_id_status }, { label: 'Telefon', value: contact.phone }, { label: 'E-Mail', value: contact.email }, { label: 'Website', value: contact.website }, { label: 'Rolle', value: contact.role }, { label: 'Privat', value: contact.is_private ? 'Ja' : 'Nein' }, { label: 'Erstellt am', value: formatDate(contact.created_at) }, { label: 'Aktualisiert am', value: formatDate(contact.updated_at) }, ]; return (

{contact.company_name}

{fields.map(field => (
{field.label}
{field.value !== null && field.value !== undefined && field.value !== '' ? field.value : '—'}
))}
{contact.contact_persons && contact.contact_persons.length > 0 ? ( contact.contact_persons.map((person: ContactPersonResponse) => (

{person.name}

{person.function &&

{person.function}

} {person.phone &&

Tel: {person.phone}

} {person.email &&

E-Mail: {person.email}

}
)) ) : (

Keine Kontaktpersonen vorhanden.

)}
setShowPersonModal(false)} title="Kontaktperson hinzufügen">
{personError && (
{personError}
)} setPersonForm(prev => ({ ...prev, name: e.target.value }))} /> setPersonForm(prev => ({ ...prev, function: e.target.value }))} /> setPersonForm(prev => ({ ...prev, phone: e.target.value }))} /> setPersonForm(prev => ({ ...prev, email: e.target.value }))} />
); }