import React, { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { billingService } from '../../services/billingService'; import Loading from '../../components/Loading'; import ErrorAlert from '../../components/ErrorAlert'; import SuccessAlert from '../../components/SuccessAlert'; function BillingDetail() { const { id } = useParams(); const navigate = useNavigate(); const user = JSON.parse(localStorage.getItem('user') || '{}'); const isPatient = user.role === 'PATIENT'; const [billing, setBilling] = useState(null); const [billingDetail, setBillingDetail] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [updating, setUpdating] = useState(false); const [paymentStatus, setPaymentStatus] = useState(''); const [downloading, setDownloading] = useState(false); useEffect(() => { fetchBilling(); }, [id]); const fetchBilling = async () => { try { setLoading(true); const response = await billingService.getBillingById(id); setBilling(response.data); setPaymentStatus(response.data.paymentStatus); // Fetch detailed billing information try { const detailResponse = await billingService.getBillingDetail(id); setBillingDetail(detailResponse.data); } catch (err) { console.error('Could not fetch billing details:', err); } } catch (err) { setError('Failed to fetch billing record'); console.error(err); } finally { setLoading(false); } }; const handleUpdatePaymentStatus = async () => { try { setUpdating(true); const response = await billingService.updatePaymentStatus(id, { paymentStatus, paymentDate: new Date().toISOString().split('T')[0], }); setBilling(response.data); setSuccess('Payment status updated successfully!'); } catch (err) { setError('Failed to update payment status'); } finally { setUpdating(false); } }; const handleDownloadInvoice = async () => { try { setDownloading(true); await billingService.downloadInvoicePDF(id); setSuccess('Invoice downloaded successfully!'); } catch (err) { setError('Failed to download invoice'); console.error(err); } finally { setDownloading(false); } }; if (loading) return ; // Prevent unauthorized access to billing if (user.role === 'DOCTOR' || user.role === 'LAB_TECHNICIAN') { return (

Access Denied

You do not have permission to access billing records.

); } if (!billing) { return (
navigate('/billing')} />
); } return (

Billing Record #{billing.billId}

{error && setError(null)} />} {success && setSuccess(null)} />}

Billing Information

{!isPatient && (

Update Payment Status

)}

Patient Details

{billingDetail && ( <> )}

Itemized Services

{billingDetail && (billingDetail.procedures.length > 0 || billingDetail.labTests.length > 0) ? (
{billingDetail.procedures.length > 0 && (

Procedures

{billingDetail.procedures.map((proc, idx) => ( ))}
Description Cost
{proc.description} ${proc.cost}
)} {billingDetail.labTests.length > 0 && (

Lab Tests

{billingDetail.labTests.map((test, idx) => ( ))}
Description Cost
{test.description} ${test.cost}
)}

Total Amount:

${billing.totalCost}

) : (

No services itemized for this billing record.

)}
); } function InfoRow({ label, value }) { return (
{label}: {value || 'N/A'}
); } export default BillingDetail;