36693-vm/admin_dashboard.php
Flatlogic Bot 28d92aa376 ODMS2
2025-12-05 19:54:09 +00:00

204 lines
11 KiB
PHP

<?php
session_start();
// Redirect to login if not logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
header("Location: admin_login.php");
exit;
}
require_once 'db/config.php';
$pdo = db();
// Handle hospital status updates
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['hospital_id'])) {
$hospital_id = $_POST['hospital_id'];
$new_status = $_POST['status']; // 'approved' or 'rejected'
if (in_array($new_status, ['approved', 'rejected'])) {
$stmt = $pdo->prepare("UPDATE hospitals SET status = ? WHERE id = ?");
$stmt->execute([$new_status, $hospital_id]);
}
header("Location: admin_dashboard.php");
exit;
}
// Handle donor status updates
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['donor_id'])) {
$donor_id = $_POST['donor_id'];
$new_status = $_POST['status']; // 'approved' or 'rejected'
if (in_array($new_status, ['approved', 'rejected'])) {
$stmt = $pdo->prepare("UPDATE donors SET status = ? WHERE id = ?");
$stmt->execute([$new_status, $donor_id]);
}
header("Location: admin_dashboard.php?tab=donors");
exit;
}
// Fetch pending hospitals
$stmt_hospitals = $pdo->prepare("SELECT * FROM hospitals WHERE status = 'pending_verification'");
$stmt_hospitals->execute();
$pending_hospitals = $stmt_hospitals->fetchAll(PDO::FETCH_ASSOC);
// Fetch all donors
$stmt_donors = $pdo->prepare("SELECT * FROM donors ORDER BY registration_date DESC");
$stmt_donors->execute();
$all_donors = $stmt_donors->fetchAll(PDO::FETCH_ASSOC);
// Fetch all recipients
$stmt_recipients = $pdo->prepare("SELECT r.*, h.hospital_name FROM recipients r JOIN hospitals h ON r.hospital_id = h.id ORDER BY r.registration_date DESC");
$stmt_recipients->execute();
$all_recipients = $stmt_recipients->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - Organ Donation</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="index.php">Organ Donation Admin</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="admin_logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<h2 class="mb-4">Admin Dashboard</h2>
<ul class="nav nav-tabs" id="adminTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="hospitals-tab" data-bs-toggle="tab" data-bs-target="#hospitals" type="button" role="tab">Pending Hospitals <span class="badge bg-danger"><?php echo count($pending_hospitals); ?></span></button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="donors-tab" data-bs-toggle="tab" data-bs-target="#donors" type="button" role="tab">Donors</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="recipients-tab" data-bs-toggle="tab" data-bs-target="#recipients" type="button" role="tab">Recipients</button>
</li>
</ul>
<div class="tab-content" id="adminTabContent">
<!-- Hospitals Tab -->
<div class="tab-pane fade show active" id="hospitals" role="tabpanel">
<div class="card mt-3">
<div class="card-header"><h4>Pending Hospital Approvals</h4></div>
<div class="card-body">
<?php if (empty($pending_hospitals)): ?>
<p class="text-center">No pending hospital registrations.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead><tr><th>ID</th><th>Hospital Name</th><th>Email</th><th>Phone</th><th>Registered On</th><th>Action</th></tr></thead>
<tbody>
<?php foreach ($pending_hospitals as $hospital): ?>
<tr>
<td><?php echo htmlspecialchars($hospital['id']); ?></td>
<td><?php echo htmlspecialchars($hospital['hospital_name']); ?></td>
<td><?php echo htmlspecialchars($hospital['email']); ?></td>
<td><?php echo htmlspecialchars($hospital['phone']); ?></td>
<td><?php echo htmlspecialchars($hospital['registration_date']); ?></td>
<td>
<form action="admin_dashboard.php" method="POST" class="d-inline-block"><input type="hidden" name="hospital_id" value="<?php echo $hospital['id']; ?>"><input type="hidden" name="status" value="approved"><button type="submit" class="btn btn-success btn-sm">Approve</button></form>
<form action="admin_dashboard.php" method="POST" class="d-inline-block"><input type="hidden" name="hospital_id" value="<?php echo $hospital['id']; ?>"><input type="hidden" name="status" value="rejected"><button type="submit" class="btn btn-danger btn-sm">Reject</button></form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Donors Tab -->
<div class="tab-pane fade" id="donors" role="tabpanel">
<div class="card mt-3">
<div class="card-header"><h4>Donor Management</h4></div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Blood Type</th><th>Organs</th><th>Status</th><th>Action</th></tr></thead>
<tbody>
<?php foreach ($all_donors as $donor): ?>
<tr>
<td><?php echo htmlspecialchars($donor['id']); ?></td>
<td><?php echo htmlspecialchars($donor['full_name']); ?></td>
<td><?php echo htmlspecialchars($donor['email']); ?></td>
<td><?php echo htmlspecialchars($donor['blood_type']); ?></td>
<td><?php echo htmlspecialchars($donor['organs_to_donate']); ?></td>
<td><span class="badge bg-<?php echo $donor['status'] == 'approved' ? 'success' : ($donor['status'] == 'pending_verification' ? 'warning' : 'danger'); ?>"><?php echo htmlspecialchars($donor['status']); ?></span></td>
<td>
<?php if ($donor['status'] == 'pending_verification'): ?>
<form action="admin_dashboard.php" method="POST" class="d-inline-block"><input type="hidden" name="donor_id" value="<?php echo $donor['id']; ?>"><input type="hidden" name="status" value="approved"><button type="submit" class="btn btn-success btn-sm">Approve</button></form>
<form action="admin_dashboard.php" method="POST" class="d-inline-block"><input type="hidden" name="donor_id" value="<?php echo $donor['id']; ?>"><input type="hidden" name="status" value="rejected"><button type="submit" class="btn btn-danger btn-sm">Reject</button></form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Recipients Tab -->
<div class="tab-pane fade" id="recipients" role="tabpanel">
<div class="card mt-3">
<div class="card-header"><h4>Registered Recipients</h4></div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead><tr><th>ID</th><th>Name</th><th>Blood Type</th><th>Organ Needed</th><th>Registered By</th><th>Date</th></tr></thead>
<tbody>
<?php foreach ($all_recipients as $recipient): ?>
<tr>
<td><?php echo htmlspecialchars($recipient['id']); ?></td>
<td><?php echo htmlspecialchars($recipient['full_name']); ?></td>
<td><?php echo htmlspecialchars($recipient['blood_type']); ?></td>
<td><?php echo htmlspecialchars($recipient['organ_needed']); ?></td>
<td><?php echo htmlspecialchars($recipient['hospital_name']); ?></td>
<td><?php echo htmlspecialchars($recipient['registration_date']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Preserve tab state across page reloads
document.addEventListener("DOMContentLoaded", function() {
var urlParams = new URLSearchParams(window.location.search);
var tab = urlParams.get('tab');
if (tab) {
var tabEl = document.querySelector('#' + tab + '-tab');
if(tabEl) {
var tabInstance = new bootstrap.Tab(tabEl);
tabInstance.show();
}
}
});
</script>
</body>
</html>