"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Trash2, ShoppingCart } from "lucide-react"; import { getCart, saveCart, clearCart, type CartItem } from "@/lib/cart"; import { formatCurrency } from "@/lib/utils"; import Link from "next/link"; export function CartView() { const router = useRouter(); const [items, setItems] = useState([]); const [mounted, setMounted] = useState(false); useEffect(() => { setItems(getCart()); setMounted(true); const handler = () => setItems(getCart()); window.addEventListener("cart-updated", handler); return () => window.removeEventListener("cart-updated", handler); }, []); function updateQty(idx: number, qty: number) { const next = items.map((item, i) => i === idx ? { ...item, quantity: qty } : item); setItems(next); saveCart(next); } function remove(idx: number) { const next = items.filter((_, i) => i !== idx); setItems(next); saveCart(next); } function createPO() { const encoded = encodeURIComponent(JSON.stringify(items)); clearCart(); setItems([]); router.push(`/po/new?cart=${encoded}`); } if (!mounted) return null; if (items.length === 0) { return (

Your cart is empty

Browse Items or Vendors to add line items

Browse Items Browse Vendors
); } const total = items.reduce((s, i) => s + i.quantity * i.unitPrice, 0); return (
{items.map((item, i) => ( ))}
Item Vendor Unit Price Qty Total

{item.name}

{item.description &&

{item.description}

}
{item.vendorName ?? Not specified} {item.unitPrice > 0 ? formatCurrency(item.unitPrice) : TBD} updateQty(i, parseFloat(e.target.value) || 1)} className="w-20 rounded border border-neutral-200 px-2 py-1 text-sm text-right" /> {item.unitPrice > 0 ? formatCurrency(item.quantity * item.unitPrice) : "—"}
Estimated Total (excl. GST) {formatCurrency(total)}
+ Add more items

Prices shown are last known prices. Final amounts are set in the PO form.

); }