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
You do not have permission to access billing records.
| Description | Cost |
|---|---|
| {proc.description} | ${proc.cost} |
| Description | Cost |
|---|---|
| {test.description} | ${test.cost} |
Total Amount:
${billing.totalCost}
No services itemized for this billing record.
)}