36247-vm/doctor_dashboard.php
Flatlogic Bot f0947adeb2 version1
2025-11-25 08:03:40 +00:00

248 lines
11 KiB
PHP

<?php
require_once 'auth.php';
require_role('doctor');
require_once 'db/config.php';
// Get doctor ID from session
$doctor_id = $_SESSION['user_id'] ?? 0; // Default to 0 if not set
// Fetch patients assigned to the doctor
try {
$pdo = db();
// Corrected to fetch patients for the logged-in doctor based on the user_id which corresponds to the doctor's ID in the `doctors` table
$stmt = $pdo->prepare("SELECT p.* FROM patients p JOIN users u ON p.doctor_id = u.id WHERE u.id = ? AND DATE(p.created_at) = CURDATE() ORDER BY p.created_at DESC");
$stmt->execute([$doctor_id]);
$patients = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$patients = [];
// In a real app, you'd want to log this error
}
// Fetch doctor's details from users table
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$doctor_id]);
$doctor = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$doctor = null;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Doctor's Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="page-wrapper">
<!-- Sidebar -->
<aside class="sidebar">
<div class="sidebar-header">
<a href="index.php" class="sidebar-brand">
<i class="bi bi-heart-pulse-fill"></i>
<span>ClinicFlow</span>
</a>
</div>
<nav class="sidebar-nav">
<a href="reception.php" class="nav-link">
<i class="bi bi-people-fill"></i>
<span>Reception</span>
</a>
<a href="doctor_dashboard.php" class="nav-link active">
<i class="bi bi-person-fill"></i>
<span>Doctor</span>
</a>
</nav>
<div class="sidebar-footer">
<div class="dropdown">
<a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle me-2"></i>
<strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong>
</a>
<ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
<li><a class="dropdown-item" href="logout.php">Sign out</a></li>
</ul>
</div>
</div>
</aside>
<!-- Main Content -->
<main class="main-content">
<div class="container-fluid">
<div class="header">
<h1 class="header-title">Doctor's Dashboard</h1>
<p class="header-subtitle">Welcome, Dr. <?php echo htmlspecialchars($doctor['username'] ?? 'Doctor'); ?>!</p>
</div>
<!-- Today's Patients -->
<div class="card content-card">
<div class="card-header">
<h5 class="card-title-text"><i class="bi bi-list-ul me-2"></i>Today's Appointments</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Patient ID</th>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (!empty($patients)):
foreach ($patients as $patient):
?>
<tr id="patient-row-<?php echo $patient['id']; ?>">
<td><?php echo htmlspecialchars($patient['patient_id']); ?></td>
<td>
<a href="patient_profile.php?id=<?php echo $patient['id']; ?>"><?php echo htmlspecialchars($patient['patient_name']); ?></a>
</td>
<td><span class="badge bg-<?php echo get_status_badge_class($patient['status']); ?>-soft" id="status-<?php echo $patient['id']; ?>"><?php echo htmlspecialchars($patient['status']); ?></span></td>
<td id="action-cell-<?php echo $patient['id']; ?>">
<?php echo get_action_buttons($patient); ?>
</td>
</tr>
<tr class="notes-row" id="notes-row-<?php echo $patient['id']; ?>" style="<?php echo $patient['status'] !== 'In Progress' ? 'display: none;' : ''; ?>">
<td colspan="4">
<form class="notes-form" data-patient-id="<?php echo $patient['id']; ?>">
<div class="mb-3">
<label for="notes-<?php echo $patient['id']; ?>" class="form-label">Consultation Notes</label>
<textarea class="form-control" id="notes-<?php echo $patient['id']; ?>" rows="3"><?php echo htmlspecialchars($patient['notes'] ?? ''); ?></textarea>
</div>
<div class="row">
<div class="col-md-8 mb-3">
<label for="service-<?php echo $patient['id']; ?>" class="form-label">Service Rendered</label>
<input type="text" class="form-control" id="service-<?php echo $patient['id']; ?>" placeholder="e.g., General Consultation">
</div>
<div class="col-md-4 mb-3">
<label for="cost-<?php echo $patient['id']; ?>" class="form-label">Cost ($)</label>
<input type="number" class="form-control" id="cost-<?php echo $patient['id']; ?>" placeholder="e.g., 75.00" step="0.01">
</div>
</div>
<button type="submit" class="btn btn-sm btn-success">
<i class="bi bi-check-circle me-1"></i>Complete Consultation & Bill
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php else:
?>
<tr>
<td colspan="4" class="text-center text-muted">No patients assigned for today.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('click', function(e) {
if (e.target.matches('.start-consultation-btn')) {
const patientId = e.target.dataset.patientId;
updatePatientStatus(patientId, 'In Progress');
}
});
document.addEventListener('submit', function(e) {
if (e.target.matches('.notes-form')) {
e.preventDefault();
const patientId = e.target.dataset.patientId;
const notes = document.getElementById('notes-' + patientId).value;
const service = document.getElementById('service-' + patientId).value;
const cost = document.getElementById('cost-' + patientId).value;
updatePatientStatus(patientId, 'Completed', notes, service, cost);
}
});
function updatePatientStatus(patientId, status, notes = null, service = null, cost = null) {
const formData = new FormData();
formData.append('patient_id', patientId);
formData.append('status', status);
if (notes !== null) {
formData.append('notes', notes);
}
if (service !== null) {
formData.append('service_rendered', service);
}
if (cost !== null) {
formData.append('cost', cost);
}
fetch('update_patient_status.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update status badge
const statusBadge = document.getElementById('status-' + patientId);
statusBadge.textContent = status;
statusBadge.className = 'badge bg-' + data.status_class + '-soft';
// Update action buttons/form
const actionCell = document.getElementById('action-cell-' + patientId);
const notesRow = document.getElementById('notes-row-' + patientId);
if (status === 'In Progress') {
actionCell.innerHTML = '';
notesRow.style.display = 'table-row';
} else if (status === 'Completed') {
actionCell.innerHTML = '<span class="text-success"><i class="bi bi-check-circle-fill me-1"></i>Completed</span>';
notesRow.style.display = 'none';
}
} else {
alert('Error: ' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('An unexpected error occurred.');
});
}
</script>
</body>
</html>
<?php
function get_status_badge_class($status) {
switch ($status) {
case 'Pending':
return 'warning';
case 'In Progress':
return 'info';
case 'Completed':
return 'success';
default:
return 'secondary';
}
}
function get_action_buttons($patient) {
switch ($patient['status']) {
case 'Pending':
return '<button class="btn btn-sm btn-primary start-consultation-btn" data-patient-id="' . $patient['id'] . '"><i class="bi bi-play-circle me-1"></i>Start Consultation</button>';
case 'In Progress':
return ''; // Form is shown in a separate row
case 'Completed':
return '<span class="text-success"><i class="bi bi-check-circle-fill me-1"></i>Completed</span>';
default:
return '';
}
}
?>