'use client' import React, { useState, useMemo } from 'react' import Link from 'next/link' import { Download, ExternalLink, ShoppingBag, ArrowLeft, Package, Search, ChevronLeft, ChevronRight, Building2, Monitor, RefreshCw, FileText } from 'lucide-react' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Input } from '@/components/ui/input' import type { Order } from '@/lib/types' const statusLabel: Record = { pending: 'Eingegangen', active: 'In Bearbeitung', completed: 'Abgeschlossen', cancelled: 'Storniert', rejected: 'Abgelehnt', } const statusClass: Record = { pending: 'bg-amber-500/20 text-amber-400 border-amber-500/30', active: 'bg-green-500/20 text-green-400 border-green-500/30', completed: 'bg-blue-500/20 text-blue-400 border-blue-500/30', cancelled: 'bg-red-500/20 text-red-400 border-red-500/30', rejected: 'bg-rose-500/20 text-rose-400 border-rose-500/30', } interface MyOrdersClientProps { orders: Order[] error: string | null } export function MyOrdersClient({ orders, error }: MyOrdersClientProps) { const [searchTerm, setSearchTerm] = useState('') const [currentPage, setCurrentPage] = useState(1) const pageSize = 10 const fmt = (val: number) => new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(val) // Volltextsuche filtern const filteredOrders = useMemo(() => { const term = searchTerm.toLowerCase().trim() if (!term) return orders return orders.filter(o => { // Anfragenummer if (o.order_number?.toString().toLowerCase().includes(term)) return true // Status const label = statusLabel[o.status] || o.status if (label.toLowerCase().includes(term) || o.status.toLowerCase().includes(term)) return true // Datum const dateStr = new Date(o.created_at).toLocaleDateString('de-DE') if (dateStr.includes(term)) return true // Endkunde (Firma / Name) const customer = (o as any).end_customer_data || o.order_data?.end_customer if (customer) { if (customer.company_name?.toLowerCase().includes(term)) return true if (customer.first_name?.toLowerCase().includes(term)) return true if (customer.last_name?.toLowerCase().includes(term)) return true if (customer.customer_number?.toLowerCase().includes(term)) return true } // Notizen if (o.notes?.toLowerCase().includes(term)) return true // Kassen & Module const items = o.order_data?.items ?? [] for (const item of items) { if (item.device_name?.toLowerCase().includes(term)) return true if (item.product_name?.toLowerCase().includes(term)) return true if (item.license_number?.toLowerCase().includes(term)) return true for (const mod of item.selected_modules ?? []) { if (mod.module_name?.toLowerCase().includes(term)) return true } } return false }) }, [orders, searchTerm]) // Paginierung berechnen const totalPages = Math.ceil(filteredOrders.length / pageSize) || 1 const paginatedOrders = useMemo(() => { const start = (currentPage - 1) * pageSize return filteredOrders.slice(start, start + pageSize) }, [filteredOrders, currentPage, pageSize]) const handleSearchChange = (e: React.ChangeEvent) => { setSearchTerm(e.target.value) setCurrentPage(1) } return (
{/* Header */}

Meine Anfragen

Alle Ihre Bestellungen und Konfigurationen auf einen Blick.

{/* Suche & Statistik */}
Anfragen: {filteredOrders.length} {searchTerm && (gefiltert aus {orders.length})}
{/* Fehler */} {error && (
Fehler beim Laden der Anfragen: {error}
)} {/* Keine Anfragen (aus DB) */} {!error && orders.length === 0 && (

Sie haben noch keine Anfragen aufgegeben.

)} {/* Keine Treffer nach Suche */} {!error && orders.length > 0 && filteredOrders.length === 0 && (

Keine Anfragen für "{searchTerm}" gefunden.

)} {/* Anfragenliste */} {paginatedOrders.length > 0 && (
{paginatedOrders.map((o) => { const items = o.order_data?.items ?? [] const customer = (o as any).end_customer_data || o.order_data?.end_customer return ( {/* Header-Zeile der Karte */}
Anfragenummer {statusLabel[o.status] ?? o.status}

#{o.order_number}

{/* Endkunde Details (falls vorhanden) */} {customer && (

{customer.company_name || `${customer.first_name} ${customer.last_name}`}

{(customer.first_name || customer.city) && (

{[customer.first_name && customer.last_name ? `${customer.first_name} ${customer.last_name}` : null, customer.city].filter(Boolean).join(' · ')}

)}
)}

Datum & Preis

{new Date(o.created_at).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric', })}

{fmt(o.total_price)}

{/* Kassen & Produkte Detail-Übersicht (Angelehnt an Kundenliste DeviceCard) */} {items.length > 0 && (

Kassenaufstellung ({items.length} {items.length === 1 ? 'Kasse' : 'Kassen'}):

{items.map((item, idx) => { const devName = item.device_name || `Kasse ${idx + 1}` const licNum = item.license_number const modules = item.selected_modules || [] // Chips für Hauptprodukt und Module const chips: string[] = [] if (item.product_name) chips.push(item.product_name) for (const mod of modules) { if (mod.module_name) chips.push(mod.module_name) } const itemPrice = (item as any).price || item.base_price || 0 const modulesPrice = modules.reduce((sum: number, m: any) => sum + ((m.price || 0) * (m.quantity || 1)), 0) const deviceTotal = itemPrice + modulesPrice return (
{/* Kassen-Info links */}
{/* Kassen-Name + Lizenznummer daneben + Intervall */}
{devName} {licNum && ( Lizenz: {licNum} )} {item.billing_interval === 'one_time' ? 'Kauf' : 'Abo'}
{/* Produkt + Modul Chips */} {chips.length > 0 && (
{chips.map((chip, cIdx) => ( {chip} ))}
)}
{/* Preis rechts */} {deviceTotal > 0 && (

Kassenwert

{fmt(deviceTotal)} {item.billing_interval === 'monthly' && / mtl.}

)}
) })}
)} {/* Notizen (falls vorhanden) */} {o.notes && (

Anmerkungen: {o.notes}

)} {/* Aktionsleiste */}
{o.status !== 'completed' && ( )}
{o.pdf_url && ( )}
) })}
)} {/* Paginierung (10 pro Seite) */} {totalPages > 1 && (
Seite {currentPage} von {totalPages}
)}
) }