'use client'; import { FC, useState } from "react"; import { BilledTo, BillingLocation, UserSettings } from "../lib/db-types"; import { formatYearMonth } from "../lib/format"; import { formatCurrency } from "../lib/formatStrings"; import { useTranslations } from "next-intl"; import { ViewBillBadge } from "./ViewBillBadge"; import { Pdf417Barcode } from "./Pdf417Barcode"; import { PaymentParams } from "hub-3a-payment-encoder"; import Link from "next/link"; import { DocumentIcon } from "@heroicons/react/24/outline"; import { uploadUtilBillsProofOfPayment } from "../lib/actions/locationActions"; export interface ViewLocationCardProps { location: BillingLocation; userSettings: UserSettings | null; } export const ViewLocationCard:FC = ({location, userSettings}) => { const { _id, name: locationName, yearMonth, bills, tenantName, tenantStreet, tenantTown, generateTenantCode, utilBillsProofOfPaymentAttachment } = location; const t = useTranslations("home-page.location-card"); const [isUploading, setIsUploading] = useState(false); const [uploadError, setUploadError] = useState(null); const [attachment, setAttachment] = useState(utilBillsProofOfPaymentAttachment); const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; // Validate file type if (file.type !== 'application/pdf') { setUploadError('Only PDF files are accepted'); e.target.value = ''; // Reset input return; } setIsUploading(true); setUploadError(null); try { const formData = new FormData(); formData.append('utilBillsProofOfPaymentAttachment', file); const result = await uploadUtilBillsProofOfPayment(_id, formData); if (result.success) { // Update local state with the uploaded attachment setAttachment({ fileName: file.name, fileSize: file.size, fileType: file.type, fileLastModified: file.lastModified, fileContentsBase64: '', // We don't need the contents in the UI }); } else { setUploadError(result.error || 'Upload failed'); } } catch (error: any) { setUploadError(error.message || 'Upload failed'); } finally { setIsUploading(false); e.target.value = ''; // Reset input } }; // sum all the billAmounts (only for bills billed to tenant) const monthlyExpense = bills.reduce((acc, bill) => (bill.paid && (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant) ? acc + (bill.payedAmount ?? 0) : acc, 0); const locationNameTrimmed_max20 = locationName.trimEnd().trimEnd().substring(0,19); const paymentParams:PaymentParams = { Iznos: (monthlyExpense/100).toFixed(2).replace(".",","), ImePlatitelja: tenantName ?? "", AdresaPlatitelja: tenantStreet ?? "", SjedistePlatitelja: tenantTown ?? "", Primatelj: userSettings?.ownerName ?? "", AdresaPrimatelja: userSettings?.ownerStreet ?? "", SjedistePrimatelja: userSettings?.ownerTown ?? "", IBAN: userSettings?.ownerIBAN ?? "", ModelPlacanja: "HR00", PozivNaBroj: formatYearMonth(yearMonth), SifraNamjene: "", OpisPlacanja: `Režije-${locationNameTrimmed_max20}-${formatYearMonth(yearMonth)}`, // max length 35 = "Režije-" (7) + locationName (20) + "-" (1) + "YYYY-MM" (7) }; return(

{formatYearMonth(yearMonth)} {locationName}

{ bills.filter(bill => (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant).map(bill => ) }
{ monthlyExpense > 0 ?

{ t("payed-total-label") } {formatCurrency(monthlyExpense, userSettings?.currency)}

: null } { userSettings?.show2dCodeInMonthlyStatement && generateTenantCode ? <>

{t("payment-info-header")}

  • {t("payment-iban-label")}
    {paymentParams.IBAN}
  • {t("payment-recipient-label")}
    {paymentParams.Primatelj}
  • {t("payment-recipient-address-label")}
    {paymentParams.AdresaPrimatelja}
  • {t("payment-recipient-city-label")}
    {paymentParams.SjedistePrimatelja}
  • {t("payment-amount-label")}
    {paymentParams.Iznos}
  • {t("payment-description-label")}
    {paymentParams.OpisPlacanja}
  • {t("payment-model-label")}
    {paymentParams.ModelPlacanja}
  • {t("payment-reference-label")}
    {paymentParams.PozivNaBroj}
  • {t("payment-purpose-code-label")}
    {paymentParams.SifraNamjene}
: null }
{t("upload-proof-of-payment-legend")} {attachment ? (
{decodeURIComponent(attachment.fileName)}
) : (
{isUploading && ( )}
{uploadError && (

{uploadError}

)}
)}
); };