"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Lead, Activity, Reminder } from "@/db/schema"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { OptionMultiSelect } from "@/components/ui/option-multi-select"; import { addLeadTag, removeLeadTag, renameLeadTag } from "@/app/admin/leads/actions"; import { formatDistanceToNow, format } from "date-fns"; import { it } from "date-fns/locale"; import { LogActivityModal } from "./LogActivityModal"; import { SendQuoteModal } from "./SendQuoteModal"; import { EditLeadModal } from "./LeadForm"; const ACTIVITY_ICON: Record = { call: "๐Ÿ“ž", email: "๐Ÿ“ง", meeting: "๐Ÿ“…", note: "๐Ÿ“", }; const STAGE_COLOR: Record = { contacted: "bg-blue-100 text-blue-800", qualified: "bg-purple-100 text-purple-800", proposal_sent: "bg-amber-100 text-amber-800", negotiating: "bg-orange-100 text-orange-800", won: "bg-green-100 text-green-800", lost: "bg-red-100 text-red-800", }; export function LeadDetail({ lead, activities, reminders, tags, tagOptions, }: { lead: Lead; activities: Activity[]; reminders: Reminder[]; tags: string[]; tagOptions: string[]; }) { const router = useRouter(); const [, startTransition] = useTransition(); const [tagError, setTagError] = useState(null); function run(fn: () => Promise) { setTagError(null); startTransition(async () => { try { await fn(); router.refresh(); } catch (e) { setTagError(e instanceof Error ? e.message : "Errore nel salvataggio"); } }); } return (
{/* Header + Actions */}

{lead.name}

{lead.company || "Azienda non specificata"}

{/* Lead Profile Card */} Profilo
{lead.status.replace(/_/g, " ")}
run(() => addLeadTag(lead.id, v))} onRemove={(v) => run(() => removeLeadTag(lead.id, v))} onRename={(o, n) => run(() => renameLeadTag(o, n))} /> {tagError &&

{tagError}

}

{lead.email || "โ€”"}

{lead.phone || "โ€”"}

{lead.last_contact_date ? formatDistanceToNow(new Date(lead.last_contact_date), { addSuffix: true, locale: it, }) : "โ€”"}

{lead.next_action || "โ€”"}

{/* Upcoming Reminders Card */} Prossimi Follow-up {reminders.length > 0 ? (
    {reminders.map((r) => (
  • {r.title}

    {format(new Date(r.due_date), "dd MMM yyyy", { locale: it })}

  • ))}
) : (

Nessun reminder

)}
{/* Notes Card */} Note

{lead.notes || "Nessuna nota"}

{/* Activity Log */} Storico Attivitร  {activities.length > 0 ? (
{activities.map((activity) => (
{ACTIVITY_ICON[activity.type as keyof typeof ACTIVITY_ICON] || "๐Ÿ“"} {activity.type} {formatDistanceToNow(new Date(activity.activity_date), { addSuffix: true, locale: it, })}

{activity.notes}

{activity.duration_minutes && (

Durata: {activity.duration_minutes} minuti

)}
))}
) : (

Nessuna attivitร  registrata

)}
); }