import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { patientService } from '../services/patientService'; import { doctorService } from '../services/doctorService'; import { appointmentService } from '../services/appointmentService'; import { labService } from '../services/labService'; import { billingService } from '../services/billingService'; import Loading from '../components/Loading'; function Dashboard() { const [stats, setStats] = useState({ patients: 0, doctors: 0, appointments: 0, }); const [userAppointments, setUserAppointments] = useState([]); const [doctorInfo, setDoctorInfo] = useState(null); const [pendingLabTests, setPendingLabTests] = useState([]); const [billings, setBillings] = useState([]); const [patientBillings, setPatientBillings] = useState([]); const [loading, setLoading] = useState(true); const user = JSON.parse(localStorage.getItem('user') || '{}'); const isPatient = user.role === 'PATIENT'; const isDoctor = user.role === 'DOCTOR'; const isLabTechnician = user.role === 'LAB_TECHNICIAN'; const isBillingAdmin = user.role === 'BILLING_ADMIN'; useEffect(() => { const fetchStats = async () => { try { if (isPatient) { // For patients, fetch their own appointments, medical records, and billing const [appointmentsRes, billingRes] = await Promise.all([ appointmentService.getAppointmentsForPatient(user.patientId), billingService.getBillingHistoryForPatient(user.patientId) ]); setUserAppointments(appointmentsRes.data || []); setPatientBillings(billingRes.data || []); } else if (isDoctor) { // For doctors, fetch their own appointments and full doctor information const [appointmentsRes, doctorRes] = await Promise.all([ appointmentService.getAppointmentsForDoctor(user.doctorId), doctorService.getDoctorById(user.doctorId), ]); setUserAppointments(appointmentsRes.data || []); setDoctorInfo(doctorRes.data); } else if (isLabTechnician) { // For lab technicians, fetch pending lab tests const pendingRes = await labService.getPendingLabTests(); setPendingLabTests(pendingRes.data || []); } else if (isBillingAdmin) { // For billing admins, fetch all billing records const billingsRes = await billingService.getAllBillings(); setBillings(billingsRes.data || []); } else { // For admin/staff, fetch all stats const [patientsRes, doctorsRes, appointmentsRes] = await Promise.all([ patientService.getAllPatients(), doctorService.getAllDoctors(), appointmentService.getAllAppointments(), ]); setStats({ patients: patientsRes.data.length, doctors: doctorsRes.data.length, appointments: appointmentsRes.data.length, }); } } catch (error) { console.error('Error fetching stats:', error); } finally { setLoading(false); } }; fetchStats(); }, []); if (loading) return ; if (isPatient) { return (

Welcome, {user.firstName} {user.lastName}

Saturday · {new Date().toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}

Your Information

Name: {user.firstName} {user.lastName}
EMBG: {user.username}
Role: Patient

Quick Actions

e.currentTarget.style.background = '#d8b4fe'} onMouseLeave={(e) => e.currentTarget.style.background = '#e9d5ff'}> + Schedule Appointment e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> View My Appointments

Your Appointments

{userAppointments.length === 0 ? (

No appointments scheduled

) : (
{userAppointments.map(apt => ( ))}
Doctor Date Time Status
{apt.doctor?.firstName} {apt.doctor?.lastName} {apt.appointmentDate} {apt.appointmentTime} {apt.status}
)}

Medical Records

e.currentTarget.style.background = '#4c1d95'} onMouseLeave={(e) => e.currentTarget.style.background = '#5b21b6'}> View Medical Records

Billing Stats

{patientBillings.length === 0 ? (

No billing records

) : (
Total Bills: {patientBillings.length}
Paid: ${patientBillings.filter(b => b.paymentStatus === 'PAID').reduce((sum, b) => sum + (b.totalCost || 0), 0).toFixed(2)}
Pending: ${patientBillings.filter(b => b.paymentStatus === 'PENDING').reduce((sum, b) => sum + (b.totalCost || 0), 0).toFixed(2)}
e.currentTarget.style.background = '#4c1d95'} onMouseLeave={(e) => e.currentTarget.style.background = '#5b21b6'}> View Billing Details
)}
); } if (isDoctor) { return (

Welcome Dr. {user.firstName} {user.lastName}

Saturday · {new Date().toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}

Your Information

Name: {user.firstName} {user.lastName}
Email: {user.username}
Role: Doctor
{doctorInfo && ( <>
Department: {doctorInfo.department?.departmentName ? doctorInfo.department.departmentName.replace(/_DEPT$/, '').replace(/_/g, ' ').split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ') : 'N/A'}
Level: {doctorInfo.level?.level || 'N/A'}
)}

Quick Actions

e.currentTarget.style.background = '#d8b4fe'} onMouseLeave={(e) => e.currentTarget.style.background = '#e9d5ff'}> + Schedule Appointment e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> View My Appointments

Your Appointments

{userAppointments.length === 0 ? (

No appointments scheduled

) : (
{userAppointments.map(apt => ( ))}
Patient Date Time Status
{apt.patient?.firstName} {apt.patient?.lastName} {apt.appointmentDate} {apt.appointmentTime} {apt.status}
)}
); } if (isLabTechnician) { return (

Welcome, {user.firstName} {user.lastName}

Saturday · {new Date().toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}

Your Information

Name: {user.firstName} {user.lastName}
Username: {user.username}
Role: Lab Technician

Quick Actions

e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> View Lab Tests

Pending Lab Tests ({pendingLabTests.length})

{pendingLabTests.length === 0 ? (

No pending lab tests

) : (
{pendingLabTests.map(test => ( ))}
Test Name Patient Doctor Test Date Notes
{test.testName} {test.patientName} {test.doctorName} {test.testDate} {test.notes || 'N/A'}
)}
); } if (isBillingAdmin) { return (

Welcome, {user.firstName} {user.lastName}

Saturday · {new Date().toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}

Your Information

Name: {user.firstName} {user.lastName}
Username: {user.username}
Role: Billing Administrator

Quick Actions

e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> View Billing

Quick Links

e.currentTarget.style.background = '#d8b4fe'} onMouseLeave={(e) => e.currentTarget.style.background = '#e9d5ff'}> View Doctors e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> View Departments

Billing Records ({billings.length})

{billings.length === 0 ? (

No billing records

) : (
{billings.map(bill => ( ))}
Patient Amount Status Date
{bill.patientName} ${bill.totalCost} {bill.paymentStatus} {bill.paymentDate || 'N/A'}
)}
); } return (

Welcome, {user.firstName} {user.lastName}

Saturday · {new Date().toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}
); } function StatCard({ title, count, delta, statClass }) { return (

{title}

{count.toLocaleString()}

{delta}

); } function QuickActionsCard() { const user = JSON.parse(localStorage.getItem('user') || '{}'); // Lab technicians and billing admins should not see admin quick actions if (user.role === 'LAB_TECHNICIAN' || user.role === 'BILLING_ADMIN') { return null; } return (

Quick Actions

e.currentTarget.style.background = '#d8b4fe'} onMouseLeave={(e) => e.currentTarget.style.background = '#e9d5ff'}> + Add New Patient e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> + Add New Doctor e.currentTarget.style.background = '#d8b4fe'} onMouseLeave={(e) => e.currentTarget.style.background = '#e9d5ff'}> + Create Appointment
); } function RecentActivityCard() { const user = JSON.parse(localStorage.getItem('user') || '{}'); // Lab technicians and billing admins should not see admin dashboard cards if (user.role === 'LAB_TECHNICIAN' || user.role === 'BILLING_ADMIN') { return null; } return (

Recent Activity

Activity feed coming soon...

); } export default Dashboard;