import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { TopAppBar } from "@/components/layout/top-app-bar"; import { Sidebar } from "@/components/layout/sidebar"; import { Card, CardHeader, CardTitle, CardContent, CardFooter } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { useAuth } from "@/hooks/use-auth"; // import { Patient, Appointment } from "@repo/db/shared/schemas"; import { Patient, Appointment } from "@repo/db/shared/schemas"; import { CreditCard, Clock, CheckCircle, AlertCircle, DollarSign, Receipt, Plus, ArrowDown, ReceiptText } from "lucide-react"; import { format } from "date-fns"; import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; export default function PaymentsPage() { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [paymentPeriod, setPaymentPeriod] = useState("all-time"); const { toast } = useToast(); const { user } = useAuth(); // Fetch patients const { data: patients = [], isLoading: isLoadingPatients } = useQuery({ queryKey: ["/api/patients"], enabled: !!user, }); // Fetch appointments const { data: appointments = [] as Appointment[], isLoading: isLoadingAppointments } = useQuery({ queryKey: ["/api/appointments"], enabled: !!user, }); const toggleMobileMenu = () => { setIsMobileMenuOpen(!isMobileMenuOpen); }; // Sample payment data const samplePayments = [ { id: "PMT-1001", patientId: patients[0]?.id || 1, amount: 75.00, date: new Date(new Date().setDate(new Date().getDate() - 2)), method: "Credit Card", status: "completed", description: "Co-pay for cleaning" }, { id: "PMT-1002", patientId: patients[0]?.id || 1, amount: 150.00, date: new Date(new Date().setDate(new Date().getDate() - 7)), method: "Insurance", status: "processing", description: "Insurance claim for x-rays" }, { id: "PMT-1003", patientId: patients[0]?.id || 1, amount: 350.00, date: new Date(new Date().setDate(new Date().getDate() - 14)), method: "Check", status: "completed", description: "Payment for root canal" }, { id: "PMT-1004", patientId: patients[0]?.id || 1, amount: 120.00, date: new Date(new Date().setDate(new Date().getDate() - 30)), method: "Credit Card", status: "completed", description: "Filling procedure" } ]; // Sample outstanding balances const sampleOutstanding = [ { id: "INV-5001", patientId: patients[0]?.id || 1, amount: 210.50, dueDate: new Date(new Date().setDate(new Date().getDate() + 7)), description: "Crown procedure", created: new Date(new Date().setDate(new Date().getDate() - 10)), status: "pending" }, { id: "INV-5002", patientId: patients[0]?.id || 1, amount: 85.00, dueDate: new Date(new Date().setDate(new Date().getDate() - 5)), description: "Diagnostic & preventive", created: new Date(new Date().setDate(new Date().getDate() - 20)), status: "overdue" } ]; // Calculate summary data const totalOutstanding = sampleOutstanding.reduce((sum, item) => sum + item.amount, 0); const totalCollected = samplePayments .filter(payment => payment.status === "completed") .reduce((sum, payment) => sum + payment.amount, 0); const pendingAmount = samplePayments .filter(payment => payment.status === "processing") .reduce((sum, payment) => sum + payment.amount, 0); const handleRecordPayment = (patientId: number, invoiceId?: string) => { const patient = patients.find(p => p.id === patientId); toast({ title: "Payment form opened", description: `Recording payment for ${patient?.firstName} ${patient?.lastName}${invoiceId ? ` (Invoice: ${invoiceId})` : ''}`, }); }; return (

Payments

Manage patient payments and outstanding balances

{/* Payment Summary Cards */}
Outstanding Balance
${totalOutstanding.toFixed(2)}

From {sampleOutstanding.length} outstanding invoices

Payments Collected
${totalCollected.toFixed(2)}

From {samplePayments.filter(p => p.status === "completed").length} completed payments

Pending Payments
${pendingAmount.toFixed(2)}

From {samplePayments.filter(p => p.status === "processing").length} pending transactions

{/* Outstanding Balances Section */}

Outstanding Balances

{sampleOutstanding.length > 0 ? ( Patient Invoice Description Amount Due Date Status Action {sampleOutstanding.map((invoice) => { const patient = patients.find(p => p.id === invoice.patientId) || { firstName: "Sample", lastName: "Patient" }; return ( {patient.firstName} {patient.lastName} {invoice.id} {invoice.description} ${invoice.amount.toFixed(2)} {format(invoice.dueDate, 'MMM dd, yyyy')} {invoice.status === 'overdue' ? ( <> Overdue ) : ( <> Pending )} ); })}
) : (

No outstanding balances

All patient accounts are current

)}
{sampleOutstanding.length > 0 && (
Download statement
Total: ${totalOutstanding.toFixed(2)}
)}
{/* Recent Payments Section */}

Recent Payments

{samplePayments.length > 0 ? ( Patient Payment ID Description Date Amount Method Status Action {samplePayments.map((payment) => { const patient = patients.find(p => p.id === payment.patientId) || { firstName: "Sample", lastName: "Patient" }; return ( {patient.firstName} {patient.lastName} {payment.id} {payment.description} {format(payment.date, 'MMM dd, yyyy')} ${payment.amount.toFixed(2)} {payment.method} {payment.status === 'completed' ? ( <> Completed ) : ( <> Processing )} ); })}
) : (

No payment history

Payments will appear here once processed

)}
); }