"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useTranslations, useFormatter } from "next-intl"; import { Card } from "@/components/ui/card"; import type { InvoiceDraftRecord } from "@/types"; interface Props { drafts: InvoiceDraftRecord[]; /** Map ZITADEL org id → company name for friendlier display. */ orgNameMap: Record; } /** * Renders the drafts table with per-row Edit / Delete actions. * * The total preview is the algebraic sum of line amounts (the same * formula billing.computeCustomInvoiceTotals uses for the subtotal, * minus VAT — which we don't know without the org's billing * snapshot). It's a hint, not authoritative; the real total * appears when the draft is issued. * * Empty state shows a clear CTA so a fresh admin knows where to * start. */ export function DraftList({ drafts, orgNameMap }: Props) { const t = useTranslations("adminBilling"); const fmt = useFormatter(); const router = useRouter(); const [busyId, setBusyId] = useState(null); const onDelete = async (id: string) => { if (!confirm(t("draftDeleteConfirm"))) return; setBusyId(id); try { const res = await fetch(`/api/admin/billing/invoice-drafts/${id}`, { method: "DELETE", }); if (!res.ok) { const j = await res.json().catch(() => ({})); throw new Error(j.error || `HTTP ${res.status}`); } router.refresh(); } catch (e: any) { alert(e.message); } finally { setBusyId(null); } }; if (drafts.length === 0) { return (

{t("draftsEmpty")}

{t("newInvoiceBtn")}
); } return (
{t("newInvoiceBtn")}
{drafts.map((d) => { const subtotal = d.payload.lines.reduce( (s, ln) => s + Math.round(ln.quantity * ln.unitPriceChf * 100) / 100, 0 ); return ( ); })}
{t("draftOrgCol")} {t("draftIssueDateCol")} {t("draftLinesCol")} {t("draftSubtotalCol")} {t("draftUpdatedCol")} {t("draftActionsCol")}
{orgNameMap[d.zitadelOrgId] ?? d.zitadelOrgId} {d.payload.issueDate} {d.payload.lines.length} CHF {subtotal.toFixed(2)} {fmt.dateTime(new Date(d.updatedAt), { dateStyle: "medium", timeStyle: "short", })} {t("editBtn")}
); }