'use client'; import { useState } from 'react'; import { FiMail, FiCalendar, FiFileText, FiCheckCircle, FiAlertCircle, FiPlay, FiCheck, FiDownload, FiClock } from 'react-icons/fi'; interface BookingAutomationPanelProps { booking: { id: string; bookingNumber: string; status: string; contractSigned: boolean; contractSignedAt: string | null; contractGenerated: boolean; contractSentAt: string | null; calendarSynced: boolean; calendarSyncedAt: string | null; lexofficeContactId: string | null; lexofficeOfferId: string | null; lexofficeConfirmationId: string | null; lexofficeInvoiceId: string | null; }; invoiceType?: string; } export default function BookingAutomationPanel({ booking, invoiceType }: BookingAutomationPanelProps) { const [isRunning, setIsRunning] = useState(false); const [isConfirming, setIsConfirming] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(null); const runAutomation = async () => { setIsRunning(true); setError(null); setResult(null); try { const response = await fetch('/api/admin/test-automation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ bookingId: booking.id }), }); const data = await response.json(); if (response.ok) { setResult(data); setTimeout(() => { window.location.reload(); }, 2000); } else { setError(data.error || 'Fehler bei der Automation'); } } catch (err: any) { setError(err.message || 'Netzwerkfehler'); } finally { setIsRunning(false); } }; const confirmBooking = async () => { setIsConfirming(true); setError(null); try { const response = await fetch(`/api/bookings/${booking.id}/confirm`, { method: 'POST', }); const data = await response.json(); if (response.ok) { alert('✅ Buchung erfolgreich bestätigt!'); window.location.reload(); } else { setError(data.error || 'Fehler bei der Bestätigung'); } } catch (err: any) { setError(err.message || 'Netzwerkfehler'); } finally { setIsConfirming(false); } }; const hasRunAutomation = Boolean(booking.lexofficeOfferId); const canConfirm = booking.contractSigned && booking.status !== 'CONFIRMED'; const downloadPDF = async (type: 'quotation' | 'contract' | 'confirmation') => { try { const response = await fetch(`/api/bookings/${booking.id}/${type}-pdf`); if (!response.ok) { const data = await response.json(); alert(`Fehler: ${data.error}`); return; } const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; let filename = ''; if (type === 'quotation') filename = `Angebot_${booking.bookingNumber}.pdf`; if (type === 'contract') filename = `Mietvertrag_${booking.bookingNumber}.pdf`; if (type === 'confirmation') filename = `Auftragsbestaetigung_${booking.bookingNumber}.pdf`; a.download = filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } catch (err: any) { alert(`Fehler beim Download: ${err.message}`); } }; return (

Automation & Status

{/* Status Grid */}
{/* Contract Generated - nur bei Privatkunden */} {invoiceType !== 'BUSINESS' && (
Vertrag PDF
{booking.contractGenerated ? (
Generiert
) : (
Nicht generiert
)}
)} {/* LexOffice Created */}
LexOffice
{booking.lexofficeOfferId ? (
Angebot erstellt
⚠️ Angebot wurde als Entwurf erstellt.
Bitte in LexOffice suchen und freigeben, um PDF herunterzuladen.
Angebot in LexOffice suchen
) : (
Nicht erstellt
)}
{/* Email Sent */}
E-Mail
{booking.contractSentAt ? (
Versendet
) : (
Nicht versendet
)}
{/* Calendar Synced */}
Kalender
{booking.calendarSynced ? (
Synchronisiert
) : (
Nicht synchronisiert
)}
{/* LexOffice Workflow Timeline */}

LexOffice Workflow

{/* Step 1: Angebot */}
{booking.lexofficeOfferId ? ( ) : ( )}
Angebot {booking.lexofficeOfferId && (
Angebot in LexOffice suchen
)}
{/* Connector Line */}
{/* Step 2: Auftragsbestätigung */}
{booking.lexofficeConfirmationId ? ( ) : booking.lexofficeOfferId ? ( ) : ( )}
Auftragsbestätigung {booking.lexofficeConfirmationId && ( )}
{/* Connector Line */}
{/* Step 3: Rechnung */}
{booking.lexofficeInvoiceId ? ( ) : booking.lexofficeConfirmationId ? ( ) : ( )}
Rechnung {booking.lexofficeInvoiceId && ( )}
{/* Actions */}
{/* Run Automation Button */} {!hasRunAutomation && ( )} {/* Confirm Booking Button */} {canConfirm && (
Vertrag wurde unterschrieben am {new Date(booking.contractSignedAt!).toLocaleDateString('de-DE')}
)} {booking.status === 'CONFIRMED' && (
Buchung ist bestätigt {booking.lexofficeConfirmationId && ( LexOffice AB: {booking.lexofficeConfirmationId.slice(0, 8)}... )}
)}
{/* Result Display */} {result && (

✅ Automation erfolgreich!

Contract PDF: {result.contractGenerated ? '✅' : '❌'}
LexOffice: {result.lexofficeCreated ? '✅' : '❌'}
E-Mail: {result.emailSent ? '✅' : '❌'}
Kalender: {result.calendarSynced ? '✅' : '❌'}
{result.errors?.length > 0 && (
Fehler: {result.errors.join(', ')}
)}
)} {/* Error Display */} {error && (

Fehler

{error}

)}
); }