import React from "react"; import { Card, CardContent } from "@/components/ui/card"; import { useQuery } from "@tanstack/react-query"; import { apiRequest } from "@/lib/queryClient"; type SummaryResp = { totalPatients?: number; patientsWithBalance?: number; totalOutstanding?: number; totalCollected?: number; }; function fmtCurrency(v: number) { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", }).format(v); } export default function SummaryCards({ startDate, endDate, }: { startDate: string; endDate: string; }) { // Query the server summary for the given date range const { data, isLoading, isError } = useQuery({ queryKey: ["/api/payments-reports/summary", startDate, endDate], queryFn: async () => { const params = new URLSearchParams(); if (startDate) params.set("from", startDate); if (endDate) params.set("to", endDate); const endpoint = `/api/payments-reports/summary?${params.toString()}`; const res = await apiRequest("GET", endpoint); if (!res.ok) { const body = await res .json() .catch(() => ({ message: "Failed to load dashboard summary" })); throw new Error(body?.message ?? "Failed to load dashboard summary"); } return res.json(); }, enabled: Boolean(startDate && endDate), }); const totalPatients = data?.totalPatients ?? 0; const patientsWithBalance = data?.patientsWithBalance ?? 0; const patientsNoBalance = Math.max( 0, (data?.totalPatients ?? 0) - (data?.patientsWithBalance ?? 0) ); const totalOutstanding = data?.totalOutstanding ?? 0; const totalCollected = data?.totalCollected ?? 0; return ( {/* Heading */}

Report summary

Data covers the selected time frame

{/* Stats grid */}
{isLoading ? "—" : totalPatients}

Total Patients

{isLoading ? "—" : patientsWithBalance}

With Balance

{isLoading ? "—" : patientsNoBalance}

Zero Balance

{isLoading ? "—" : fmtCurrency(totalOutstanding)}

Outstanding

{isLoading ? "—" : fmtCurrency(totalCollected)}

Collected

{isError && (
Failed to load summary. Check server or network.
)}
); }