import { useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherCreditCard, FeatherDownload, FeatherEye, FeatherFileText, FeatherReceipt, FeatherX } from "@subframe/core"; import type { BillingPaymentInvoiceVM, BillingPaymentVM } from "@hivegrid/view-models"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageIntro, PageShell } from "../shared/PageShell"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { Loader } from "~/ui/components/Loader"; import { StatsCard } from "~/ui/components/StatsCard"; import { Table } from "~/ui/components/Table"; export type BillingPaymentActions = { readonly onDownloadInvoice: (href: string) => void; readonly onRetry: () => void; }; function invoiceTone(tone: BillingPaymentInvoiceVM["tone"]): React.ComponentProps["farbe"] { if (tone === "positive") return "subtle-positive"; if (tone === "negative") return "subtle-negative"; if (tone === "warning") return "subtle-warning"; return "subtle-neutral"; } /** D-P20 S03 interim: Lago invoices and their Mollie payment result. */ export function BillingPaymentPage({ vm, actions, embedded = false, }: { vm: BillingPaymentVM; actions: BillingPaymentActions; /** Inside the Abrechnung hub (AK-SET-01) the hub owns the header — see PageIntro. */ embedded?: boolean; }): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); // Which invoice is open in the preview frame below the table. Local view // state: the frame streams the same route the download button opens, so the // route has nothing to prepare for it. const [previewInvoiceId, setPreviewInvoiceId] = useState(null); const previewInvoice = vm.status === "ready" ? (vm.data.invoices.find((invoice) => invoice.id === previewInvoiceId) ?? null) : null; return ( {embedded ? null : ( } title={t("billing:billingPayment.title")} subtitle={t("billing:billingPayment.description")} /> )} {embedded ? {t("billing:billingPayment.description")} : null} {vm.status === "idle" || vm.status === "loading" ? (
{t("billing:billingPayment.loading.title")} {t("billing:billingPayment.loading.description")}
) : vm.status === "error" ? ( {t("billing:states.retry")} } /> ) : vm.status === "empty" ? ( {t("billing:states.refresh")} } /> ) : vm.status === "ready" ? ( <>
} label={t("billing:billingPayment.summary.invoices")} value={formatters.number(vm.data.invoices.length)} percentage={t("billing:billingPayment.summary.lago")} /> } label={t("billing:billingPayment.summary.latestPayment")} value={t(vm.data.latestPaymentStatusLabelKey)} percentage={t( vm.data.hasMollieReference ? "billing:billingPayment.summary.mollieConnected" : "billing:billingPayment.summary.noPaymentReference" )} />
{t("billing:billingPayment.invoices.title")} {t("billing:billingPayment.invoices.description")}
{t("billing:billingPayment.invoices.columns.invoice")}{t("billing:billingPayment.invoices.columns.date")}{t("billing:billingPayment.invoices.columns.amount")}{t("billing:billingPayment.invoices.columns.invoiceStatus")}{t("billing:billingPayment.invoices.columns.paymentStatus")}{t("billing:billingPayment.invoices.columns.download")} } > {vm.data.invoices.map((invoice) => ( {invoice.number ?? invoice.id} {invoice.issuingDate ? formatters.date(invoice.issuingDate, { dateStyle: "medium" }) : t("shared.unknown")} {invoice.amountCents !== null && invoice.currency ? formatters.currencyFromCents(invoice.amountCents, invoice.currency) : t("shared.unknown")} {t(invoice.statusLabelKey)} {t(invoice.paymentStatusLabelKey)} {invoice.downloadHref ? (
{/* Preview toggles: the same button closes the frame again. */}
) : ( {t("billing:billingPayment.invoices.unavailable")} )}
))}
{/* Preview opens below the list and closes from its own header or from the row button that opened it. The frame streams the invoice route with ?disposition=inline — same permission check and same bytes as the download. */} {previewInvoice?.previewHref ? (
{t("billing:billingPayment.invoices.previewTitle", { invoice: previewInvoice.number ?? previewInvoice.id })} {t("billing:billingPayment.invoices.previewHint")}