"use client" import { useEffect, useState, useMemo } from "react" import { useRouter } from "next/navigation" import Image from "next/image" import Link from "next/link" import { Button } from "@/components/ui/button" import { SearchFilter } from "@/components/dashboard/search-filter" import { formatPrice } from "@/lib/calculations" import { type StoredQuote, type QuoteStatus } from "@/lib/db" import { LogOut, LayoutDashboard, Loader2, ExternalLink } from "lucide-react" const STATUS_LABELS: Record = { new: { label: "Ny", className: "bg-blue-100 text-blue-700" }, contacted: { label: "Kontaktet", className: "bg-amber-100 text-amber-700" }, accepted: { label: "Accepteret", className: "bg-green-100 text-green-700" }, rejected: { label: "Afvist", className: "bg-gray-100 text-gray-500" }, } function formatDate(dateString: string): string { return new Date(dateString).toLocaleDateString("da-DK", { day: "numeric", month: "short", year: "numeric", }) } export default function HistorikPage() { const router = useRouter() const [quotes, setQuotes] = useState([]) const [loading, setLoading] = useState(true) const [search, setSearch] = useState("") useEffect(() => { fetchQuotes() }, []) async function fetchQuotes() { try { const res = await fetch("/api/quotes") if (!res.ok) { if (res.status === 401) { router.push("/intern/login") return } throw new Error("Failed to fetch quotes") } const data = await res.json() setQuotes(data.quotes) } catch { // fetch failed } finally { setLoading(false) } } async function handleLogout() { try { await fetch("/api/auth/logout", { method: "POST" }) router.push("/intern/login") router.refresh() } catch { // logout failed } } const filteredQuotes = useMemo(() => { if (!search.trim()) return quotes const term = search.toLowerCase() return quotes.filter( (q) => q.customerName.toLowerCase().includes(term) || q.customerEmail.toLowerCase().includes(term) || q.postalCode.includes(term) || q.id.toString().includes(term) ) }, [quotes, search]) if (loading) { return (
) } return (
{/* Header */}
Foam King

Historik

{/* Main content */}
{/* Search and stats */}
{filteredQuotes.length} tilbud {search && ` (af ${quotes.length})`}
{/* Table */}
{filteredQuotes.map((quote) => { const slug = `${quote.postalCode}-${quote.id}` const status = STATUS_LABELS[quote.status] return ( ) })}
# Kunde Lokation Areal Pris Status Dato
{quote.id}
{quote.customerName}
{quote.customerEmail}
{quote.postalCode} {quote.address && ( , {quote.address} )} {quote.area} m² {quote.totalInclVat ? formatPrice(Math.round(quote.totalInclVat)) : "—"} {status.label} {formatDate(quote.createdAt)} Åbn
{filteredQuotes.length === 0 && (
{search ? "Ingen tilbud matcher søgningen" : "Ingen tilbud endnu"}
)}
) }