import { useState } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { birthdayApi, customerApi } from '../services/api'; import Button from './ui/Button'; import { X, Cake, RotateCcw, Send, AlertTriangle, Loader2, Check } from 'lucide-react'; import type { Customer } from '../types'; interface Props { customer: Customer; onClose: () => void; } type Channel = 'email' | 'whatsapp' | 'telegram' | 'signal'; const channelLabels: Record = { email: { label: 'Per E-Mail', icon: '✉️' }, whatsapp: { label: 'Per WhatsApp', icon: '💬' }, telegram: { label: 'Per Telegram', icon: '📨' }, signal: { label: 'Per Signal', icon: '📱' }, }; type ConfirmState = | { type: 'none' } | { type: 'reset' } | { type: 'send'; channel: Channel }; export default function BirthdayManagementModal({ customer, onClose }: Props) { const queryClient = useQueryClient(); const [confirm, setConfirm] = useState({ type: 'none' }); const [autoEnabled, setAutoEnabled] = useState(customer.autoBirthdayGreeting ?? false); const [autoChannel, setAutoChannel] = useState( (customer.autoBirthdayChannel as Channel) || 'email', ); const [saved, setSaved] = useState(false); const [sentChannel, setSentChannel] = useState(null); const [reset, setReset] = useState(false); const resetMutation = useMutation({ mutationFn: () => birthdayApi.resetGreeting(customer.id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['customer', customer.id] }); setReset(true); setTimeout(() => setReset(false), 2500); }, }); const sendMutation = useMutation({ mutationFn: (channel: Channel) => birthdayApi.sendGreeting(customer.id, channel), onSuccess: (result, channel) => { const text = result.data?.messageText || ''; if (channel === 'whatsapp') { window.open(`https://wa.me/?text=${encodeURIComponent(text)}`, '_blank'); } else if (channel === 'telegram') { window.open( `https://t.me/share/url?url=&text=${encodeURIComponent(text)}`, '_blank', ); } else if (channel === 'signal') { window.open(`signal://send?text=${encodeURIComponent(text)}`, '_blank'); } setSentChannel(channel); setTimeout(() => setSentChannel(null), 2500); }, }); const saveAutoMutation = useMutation({ mutationFn: () => customerApi.update(customer.id, { autoBirthdayGreeting: autoEnabled, autoBirthdayChannel: autoEnabled ? autoChannel : null, } as any), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['customer', customer.id] }); setSaved(true); setTimeout(() => setSaved(false), 2000); }, }); const handleConfirmAction = () => { if (confirm.type === 'reset') { resetMutation.mutate(); } else if (confirm.type === 'send') { sendMutation.mutate(confirm.channel); } setConfirm({ type: 'none' }); }; const birthDateDisplay = customer.birthDate ? new Date(customer.birthDate).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric', }) : '-'; return (
{/* Header */}

Geburtstag verwalten

{customer.companyName || `${customer.firstName} ${customer.lastName}`}

{/* Body */}
{/* Info */}
Geburtsdatum: {birthDateDisplay}
{customer.birthPlace && (
Geburtsort: {customer.birthPlace}
)}
Anrede per: {customer.useInformalAddress ? 'Du (informell)' : 'Sie (formell)'}
{/* Gruß zurücksetzen */}

Gruß-Marker zurücksetzen

Setzt die Markierung zurück, dass dem Kunden dieses Jahr bereits der Geburtstagsgruß angezeigt wurde. Beim nächsten Portal-Login erscheint das Modal wieder.

{/* Gruß senden */}

Geburtstagsgruß jetzt senden

Sendet einen persönlichen Geburtstagsgruß über den gewählten Kanal.

{(Object.entries(channelLabels) as [Channel, typeof channelLabels[Channel]][]).map( ([ch, info]) => ( ), )}
{sendMutation.isError && (

{(sendMutation.error as any)?.message || 'Fehler beim Senden'}

)}
{/* Automatisch senden */}

Automatisch senden

{autoEnabled && (

Hinweis: WhatsApp/Telegram/Signal erfordern aktuell einen manuellen Klick im Browser. Aktuell wird nur automatischer E-Mail-Versand unterstützt.

)}
{/* Bestätigungs-Dialog */} {confirm.type !== 'none' && (

Bist du sicher?

{confirm.type === 'reset' ? 'Möchtest du den Geburtstagsgruß-Marker wirklich zurücksetzen? Beim nächsten Portal-Login sieht der Kunde das Geburtstagsmodal erneut.' : `Möchtest du den Geburtstagsgruß wirklich ${channelLabels[confirm.channel].label.toLowerCase()} senden?`}

)}
); }