"use client"; import { useEffect, useState } from "react"; import { X, Loader2, Sparkles, FileDown, Download, ClipboardCopy, Building2, AlertTriangle } from "lucide-react"; import { Button } from "@/shared/components/ui/button"; import type { ClickedFeature, FeatureDetail, FeatureEnrichmentData } from "../types"; type CfStatus = { available: boolean; expired?: boolean; downloadUrl?: string; documentName?: string }; type FeatureInfoPanelProps = { feature: ClickedFeature | null; onClose: () => void; }; export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) { const [detail, setDetail] = useState(null); const [loading, setLoading] = useState(false); const [enriching, setEnriching] = useState(false); const [enrichMsg, setEnrichMsg] = useState(""); const [cfStatus, setCfStatus] = useState(null); useEffect(() => { if (!feature) { setDetail(null); setCfStatus(null); return; } const objectId = feature.properties.object_id ?? feature.properties.objectId; const siruta = feature.properties.siruta; if (!objectId || !siruta) { setDetail(null); return; } let cancelled = false; setLoading(true); setEnrichMsg(""); setCfStatus(null); fetch(`/api/geoportal/feature?objectId=${objectId}&siruta=${siruta}&sourceLayer=${feature.sourceLayer}`) .then((r) => r.ok ? r.json() : Promise.reject()) .then((data: { feature: FeatureDetail }) => { if (cancelled) return; setDetail(data.feature); const e = data.feature.enrichment as FeatureEnrichmentData | null; const nrCad = e?.NR_CAD ?? data.feature.cadastralRef; if (nrCad) { fetch(`/api/geoportal/cf-status?nrCad=${encodeURIComponent(nrCad)}`) .then((r) => r.ok ? r.json() : null) .then((cf: CfStatus | null) => { if (!cancelled && cf) setCfStatus(cf); }) .catch(() => {}); } }) .catch(() => { if (!cancelled) setDetail(null); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [feature]); if (!feature) return null; const e = detail?.enrichment as FeatureEnrichmentData | null | undefined; const isUat = feature.sourceLayer?.includes("uat"); const cadRef = e?.NR_CAD ?? String(feature.properties.cadastral_ref ?? ""); const title = isUat ? String(feature.properties.name ?? "UAT") : cadRef ? `Parcela ${cadRef}` : `#${feature.properties.object_id ?? "?"}`; // Has REAL enrichment (not just NR_CAD/SUPRAFATA which come from basic sync) const hasRealEnrichment = !!e && !!(e.NR_CF || e.PROPRIETARI || e.CATEGORIE_FOLOSINTA || e.INTRAVILAN); const siruta = String(feature.properties.siruta ?? detail?.siruta ?? ""); const handleEnrich = async () => { if (!detail?.id && !siruta) return; setEnriching(true); setEnrichMsg(""); try { const objectId = feature.properties.object_id ?? feature.properties.objectId; const resp = await fetch("/api/geoportal/enrich", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(detail?.id ? { featureId: detail.id } : { siruta, objectId: Number(objectId) }), }); const d = await resp.json(); if (resp.ok) { // Update panel with enrichment data immediately if (d.enrichment && detail) { setDetail({ ...detail, enrichment: d.enrichment, enrichedAt: new Date().toISOString() }); setEnrichMsg(""); } else { setEnrichMsg(d.message ?? "Enrichment finalizat"); } } else { setEnrichMsg(d.error ?? "Eroare la enrichment"); } } catch { setEnrichMsg("Eroare retea"); } finally { setEnriching(false); } }; return (
{/* Header */}

{title}

{loading && (
Se incarca...
)} {!loading && isUat && ( <> )} {!loading && !isUat && ( <> {/* Basic info */} {/* Enrichment data */} {hasRealEnrichment && ( <>
{/* Building info */} {(e?.HAS_BUILDING === 1) && (
Constructie: {e?.BUILD_LEGAL === 1 ? "Cu acte" : ( Fara acte )}
)} )} {/* Action buttons */}
{!hasRealEnrichment && ( )} {cfStatus?.available && cfStatus.downloadUrl ? ( ) : cadRef ? ( ) : null}
{enrichMsg && (

{enrichMsg}

)} )}
); } /** Single-line row (value truncated only if very long) */ function Row({ label, value }: { label: string; value: unknown }) { if (!value || value === "-" || value === "" || value === 0) return null; return (
{label} {String(value)}
); } /** Multi-line row (long text wraps) */ function WrapRow({ label, value }: { label: string; value: unknown }) { if (!value || value === "-" || value === "") return null; return (
{label}

{String(value)}

); } function formatArea(v: unknown): string { if (!v || v === "") return ""; const n = typeof v === "number" ? v : parseFloat(String(v)); if (isNaN(n)) return String(v); return `${n.toLocaleString("ro-RO")} mp`; }