import { useEffect, useRef, useState } from 'react' import { Box, Button, CircularProgress, Dialog, LinearProgress, Typography } from '@mui/material' import { CheckCircle, Download, FileText } from 'lucide-react' import type { MarketReport } from '../../domain/marketReport' import { DS_TEXT, DS_BG, DS_BORDER, DS_SURFACE } from '../../lib/ds' const SECTION_COLORS: Record = { development: '#1e3a5f', demand: '#1a7a4a', supply: '#b45309', negotiation: '#7c3aed', } const SECTION_LABELS: Record = { development: 'Entwicklungsnews', demand: 'Nachfrage 5 km', supply: 'Angebot 5 km', negotiation: 'Verhandlungshinweise', } interface BerichtDialogProps { onClose: () => void report: MarketReport | null propertyId: string } export function BerichtDialog({ onClose, report, propertyId }: BerichtDialogProps) { const [ready, setReady] = useState(false) const [progress, setProgress] = useState(0) const timerRef = useRef | null>(null) useEffect(() => { let p = 0 timerRef.current = setInterval(() => { p += Math.random() * 18 + 8 if (p >= 100) { clearInterval(timerRef.current!) setProgress(100) setReady(true) } else { setProgress(Math.min(100, p)) } }, 200) return () => { if (timerRef.current) clearInterval(timerRef.current) } }, []) function handleDownload() { const signals = report?.signals ?? [] const lines: string[] = [ 'MARKTSIGNAL-BERICHT', '====================', `Objekt-ID: ${propertyId}`, `Erstellt: ${new Date().toLocaleString('de-CH')}`, '', ] for (const s of signals) { lines.push(`[${SECTION_LABELS[s.category] ?? s.category}]`) lines.push(s.title) lines.push(s.body) if (s.source) lines.push(`Quelle: ${s.source}`) lines.push('') } const blob = new Blob([lines.join('\n')], { type: 'application/pdf' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `Marktsignal-Bericht_${propertyId}.pdf` a.click() URL.revokeObjectURL(url) onClose() } const signals = report?.signals ?? [] const categories = ['development', 'demand', 'supply', 'negotiation'] as const return ( {/* Header */} Bericht erstellen Marktsignal-Bericht · {propertyId} {ready && ( Bereit )} {/* Generating state */} {!ready && ( PDF-Bericht wird generiert… {Math.round(progress)}% )} {/* PDF Preview */} {/* Document header */} Marktsignal-Bericht Wincasa AG · Zürich {new Date().toLocaleDateString('de-CH')} Standortintelligenz & Marktsignale Objekt-ID: {propertyId} · Generiert: {report?.generatedAt ? new Date(report.generatedAt).toLocaleDateString('de-CH') : new Date().toLocaleDateString('de-CH')} · {signals.length} Signale {/* Sections */} {categories.map(cat => { const catSignals = signals.filter(s => s.category === cat) if (catSignals.length === 0) return null const color = SECTION_COLORS[cat] const label = SECTION_LABELS[cat] return ( {label} ({catSignals.length}) {catSignals.map((s, i) => ( {s.title} {s.confidence != null && ( KI-Konfidenz: {Math.round(s.confidence * 100)}% )} {s.body} {s.date && ( {new Date(s.date).toLocaleDateString('de-CH')} )} {s.source && ( Quelle: {s.source} )} ))} ) })} {signals.length === 0 && ( Keine Marktsignale für dieses Objekt verfügbar. )} {/* Footer */} Dieser Bericht wurde automatisch auf Basis von KI-generierten Marktsignalen erstellt. · Wincasa AG {/* Footer actions */} ) }