import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { formatDateToHumanReadable, formatLocalDate, parseLocalDate, } from "@/utils/dateUtils"; import React, { useState } from "react"; import { PaymentStatus, paymentStatusOptions, PaymentMethod, paymentMethodOptions, PaymentWithExtras, NewTransactionPayload, } from "@repo/db/types"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@radix-ui/react-select"; import { Input } from "@/components/ui/input"; import Decimal from "decimal.js"; import { toast } from "@/hooks/use-toast"; type PaymentEditModalProps = { isOpen: boolean; onOpenChange: (open: boolean) => void; onClose: () => void; onEditServiceLine: (payload: NewTransactionPayload) => void; payment: PaymentWithExtras | null; }; export default function PaymentEditModal({ isOpen, onOpenChange, onClose, payment, onEditServiceLine, }: PaymentEditModalProps) { if (!payment) return null; const [expandedLineId, setExpandedLineId] = useState(null); const [paymentStatus, setPaymentStatus] = React.useState( payment.status ); const [formState, setFormState] = useState(() => { return { serviceLineId: 0, transactionId: "", paidAmount: 0, adjustedAmount: 0, method: paymentMethodOptions[1] as PaymentMethod, receivedDate: formatLocalDate(new Date()), payerName: "", notes: "", }; }); const handleEditServiceLine = (lineId: number) => { if (expandedLineId === lineId) { // Closing current line setExpandedLineId(null); return; } // Find line data const line = payment.claim.serviceLines.find((sl) => sl.id === lineId); if (!line) return; // updating form to show its data, while expanding. setFormState({ serviceLineId: line.id, transactionId: "", paidAmount: Number(line.totalDue) > 0 ? Number(line.totalDue) : 0, adjustedAmount: 0, method: paymentMethodOptions[1] as PaymentMethod, receivedDate: formatLocalDate(new Date()), payerName: "", notes: "", }); setExpandedLineId(lineId); }; const updateField = (field: string, value: any) => { setFormState((prev) => ({ ...prev, [field]: value, })); }; const handleSavePayment = async () => { if (!formState.serviceLineId) { toast({ title: "Error", description: "No service line selected." }); return; } const payload: NewTransactionPayload = { paymentId: payment.id, status: paymentStatus, serviceLineTransactions: [ { serviceLineId: formState.serviceLineId, transactionId: formState.transactionId || undefined, paidAmount: Number(formState.paidAmount), adjustedAmount: Number(formState.adjustedAmount) || 0, method: formState.method, receivedDate: parseLocalDate(formState.receivedDate), payerName: formState.payerName || undefined, notes: formState.notes || undefined, }, ], }; try { await onEditServiceLine(payload); setExpandedLineId(null); onClose(); } catch (err) { console.error(err); toast({ title: "Error", description: "Failed to save payment." }); } }; return ( Edit Payment View and manage payments applied to service lines.
{/* Claim + Patient Info */}

{payment.claim.patientName}

Claim ID: {payment.claimId.toString().padStart(4, "0")}

Service Date:{" "} {formatDateToHumanReadable(payment.claim.serviceDate)}

Notes:{" "} {payment.notes || "N/A"}

{/* Payment Summary */}

Payment Info

Total Billed: $ {payment.totalBilled.toNumber().toFixed(2)}

Total Paid: $ {payment.totalPaid.toNumber().toFixed(2)}

Total Due: $ {payment.totalDue.toNumber().toFixed(2)}

Metadata

Created At:{" "} {payment.createdAt ? formatDateToHumanReadable(payment.createdAt) : "N/A"}

Last Upadated At:{" "} {payment.updatedAt ? formatDateToHumanReadable(payment.updatedAt) : "N/A"}

{/* Service Lines Payments */}

Service Lines

{payment.claim.serviceLines.length > 0 ? ( <> {payment.claim.serviceLines.map((line) => { return (

Procedure Code:{" "} {line.procedureCode}

Billed: $ {line.totalBilled.toFixed(2)}

Paid: $ {line.totalPaid.toFixed(2)}

Adjusted: $ {line.totalAdjusted.toFixed(2)}

Due: $ {line.totalDue.toFixed(2)}

{expandedLineId === line.id && (
updateField( "paidAmount", parseFloat(e.target.value) ) } />
updateField( "adjustedAmount", parseFloat(e.target.value) ) } />
updateField("receivedDate", e.target.value) } />
updateField("payerName", e.target.value) } />
updateField("notes", e.target.value) } />
)}
); })} ) : (

No service lines available.

)}
{/* Transactions Overview */}

All Transactions

{payment.serviceLineTransactions.length > 0 ? ( payment.serviceLineTransactions.map((tx) => (

Date:{" "} {formatDateToHumanReadable(tx.receivedDate)}

Paid Amount: $ {Number(tx.paidAmount).toFixed(2)}

Adjusted Amount: $ {Number(tx.adjustedAmount).toFixed(2)}

Method: {tx.method}

{tx.payerName && (

Payer Name:{" "} {tx.payerName}

)} {tx.notes && (

Notes: {tx.notes}

)}
)) ) : (

No transactions recorded.

)}
{/* Actions */}
); }