"use client"; import { TicketIcon, CheckCircleIcon, XCircleIcon, DocumentIcon } from "@heroicons/react/24/outline"; import { Bill, BillingLocation } from "../lib/db-types"; import { FC, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { formatYearMonth } from "../lib/format"; import { useTranslations } from "next-intl"; import { uploadProofOfPayment } from "../lib/actions/billActions"; import { Pdf417Barcode } from "./Pdf417Barcode"; export interface ViewBillCardProps { location: BillingLocation; bill: Bill; shareId?: string; } export const ViewBillCard: FC = ({ location, bill, shareId }) => { const router = useRouter(); const t = useTranslations("bill-edit-form"); const { _id: billID, name, paid, attachment, notes, payedAmount, hub3aText, proofOfPayment } = bill ?? { _id: undefined, name: "", paid: false, notes: "" }; const { _id: locationID, proofOfPaymentType } = location; const [isUploading, setIsUploading] = useState(false); const [uploadError, setUploadError] = useState(null); const [proofOfPaymentUploadedAt, setProofOfPaymentUploadedAt] = useState(proofOfPayment?.uploadedAt ?? null); const [proofOfPaymentFilename, setProofOfPaymentFilename] = useState(proofOfPayment?.fileName); const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; // Validate file type client-side (quick feedback) if (file.type !== 'application/pdf') { setUploadError('Only PDF files are accepted'); e.target.value = ''; // Reset input return; } if (!shareId) { setUploadError('Invalid upload link'); return; } setIsUploading(true); setUploadError(null); try { const formData = new FormData(); formData.append('proofOfPayment', file); const result = await uploadProofOfPayment(shareId, billID as string, formData); if (result.success) { setProofOfPaymentFilename(file.name); setProofOfPaymentUploadedAt(new Date()); router.refresh(); } else { setUploadError(result.error || 'Upload failed'); } } catch (error: any) { setUploadError(error.message || 'Upload failed'); } finally { setIsUploading(false); e.target.value = ''; // Reset input } }; return (

{`${formatYearMonth(location.yearMonth)} ${location.name}`}

{name}

{t("paid-checkbox")} {paid ? : }

{t("payed-amount")} {payedAmount ? payedAmount / 100 : ""}

{ notes ?

{t("notes-placeholder")}

{notes}

: null } { attachment ?

{t("attachment")}

{decodeURIComponent(attachment.fileName)}
: null } { hub3aText ?

{t.rich('barcode-disclaimer', { br: () =>
})}

: null } { // IF proof of payment type is "per-bill", show upload fieldset proofOfPaymentType === "per-bill" &&
{t("upload-proof-of-payment-legend")} { // IF proof of payment was uploaded proofOfPaymentUploadedAt ? ( // IF file name is available, show link to download // ELSE it's not available that means that the uploaded file was purged by housekeeping // -> don't show anything proofOfPaymentFilename ? (
{ decodeURIComponent(proofOfPaymentFilename) }
) : null ) : /* ELSE show upload input */ (
{isUploading && ( )}
{uploadError && (

{uploadError}

)}
)}
}
{t("back-button")}
); }