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

221 lines
10 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// If not logged in, redirect to login page
if (!isset($_SESSION['hospital_id'])) {
header("Location: hospital_login.php");
exit();
}
$hospital_id = $_SESSION['hospital_id'];
$pdo = db();
// Fetch hospital status
$stmt = $pdo->prepare("SELECT status FROM hospitals WHERE id = ?");
$stmt->execute([$hospital_id]);
$hospital = $stmt->fetch(PDO::FETCH_ASSOC);
$hospital_status = $hospital['status'] ?? 'pending_verification';
$success_message = '';
$error_message = '';
// Table creation and form processing only if hospital is approved
if ($hospital_status === 'approved') {
try {
// Idempotent table creation for recipients
$pdo->exec("CREATE TABLE IF NOT EXISTS recipients (
id INT AUTO_INCREMENT PRIMARY KEY,
hospital_id INT NOT NULL,
full_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(50),
blood_type VARCHAR(10) NOT NULL,
organ_needed VARCHAR(100) NOT NULL,
urgency_level VARCHAR(50) NOT NULL,
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(50) DEFAULT 'waiting', /* e.g., waiting, matched, transplanted */
FOREIGN KEY (hospital_id) REFERENCES hospitals(id)
)");
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
// Handle new recipient registration
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['register_recipient'])) {
$full_name = trim($_POST['full_name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$blood_type = $_POST['blood_type'];
$organ_needed = $_POST['organ_needed'];
$urgency_level = $_POST['urgency_level'];
if (empty($full_name) || empty($email) || empty($blood_type) || empty($organ_needed) || empty($urgency_level)) {
$error_message = "Please fill all required fields.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message = "Invalid email format.";
} else {
try {
$stmt = $pdo->prepare("INSERT INTO recipients (hospital_id, full_name, email, phone, blood_type, organ_needed, urgency_level) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$hospital_id, $full_name, $email, $phone, $blood_type, $organ_needed, $urgency_level]);
$success_message = "Recipient registered successfully!";
} catch (PDOException $e) {
$error_message = "Error registering recipient. Please try again.";
}
}
}
}
// Fetch this hospital's registered recipients
$recipients = [];
if ($hospital_status === 'approved') {
$stmt = $pdo->prepare("SELECT * FROM recipients WHERE hospital_id = ? ORDER BY registration_date DESC");
$stmt->execute([$hospital_id]);
$recipients = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Logout logic
if (isset($_GET['logout'])) {
session_destroy();
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hospital Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand" href="hospital_dashboard.php">Hospital Dashboard</a>
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<span class="navbar-text me-3">Welcome, <?php echo htmlspecialchars($_SESSION['hospital_name']); ?></span>
</li>
<li class="nav-item">
<a class="nav-link btn btn-light text-primary" href="?logout=true">Logout</a>
</li>
</ul>
</div>
</nav>
<div class="container mt-5">
<?php if ($hospital_status === 'pending_verification'): ?>
<div class="alert alert-warning text-center">
<h4 class="alert-heading">Account Pending Approval</h4>
<p>Your hospital registration is currently under review by our administrators. You will be able to register recipients once your account is approved.</p>
</div>
<?php elseif ($hospital_status === 'rejected'): ?>
<div class="alert alert-danger text-center">
<h4 class="alert-heading">Account Registration Rejected</h4>
<p>Your hospital registration was not approved. Please contact an administrator for more information.</p>
</div>
<?php else: // Approved ?>
<?php if ($success_message): ?><div class="alert alert-success"><?php echo $success_message; ?></div><?php endif; ?>
<?php if ($error_message): ?><div class="alert alert-danger"><?php echo $error_message; ?></div><?php endif; ?>
<!-- Recipient Registration Form -->
<div class="card mb-5">
<div class="card-header">
<h4>Register a New Recipient</h4>
</div>
<div class="card-body">
<form action="hospital_dashboard.php" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="full_name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="full_name" name="full_name" required>
</div>
<div class="col-md-6 mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="col-md-6 mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="phone" name="phone">
</div>
<div class="col-md-6 mb-3">
<label for="blood_type" class="form-label">Blood Type</label>
<select class="form-select" id="blood_type" name="blood_type" required>
<option value="" disabled selected>Select...</option>
<option value="A+">A+</option><option value="A-">A-</option><option value="B+">B+</option><option value="B-">B-</option>
<option value="AB+">AB+</option><option value="AB-">AB-</option><option value="O+">O+</option><option value="O-">O-</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="organ_needed" class="form-label">Organ Needed</label>
<select class="form-select" id="organ_needed" name="organ_needed" required>
<option value="" disabled selected>Select...</option>
<option value="Heart">Heart</option><option value="Lungs">Lungs</option><option value="Kidneys">Kidneys</option>
<option value="Liver">Liver</option><option value="Pancreas">Pancreas</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="urgency_level" class="form-label">Urgency Level</label>
<select class="form-select" id="urgency_level" name="urgency_level" required>
<option value="" disabled selected>Select...</option>
<option value="Critical">Critical</option><option value="High">High</option>
<option value="Medium">Medium</option><option value="Low">Low</option>
</select>
</div>
</div>
<button type="submit" name="register_recipient" class="btn btn-primary">Register Recipient</button>
</form>
</div>
</div>
<!-- Registered Recipients List -->
<div class="card">
<div class="card-header">
<h4>Your Registered Recipients</h4>
</div>
<div class="card-body">
<?php if (empty($recipients)): ?>
<p class="text-center">You have not registered any recipients yet.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th><th>Email</th><th>Blood Type</th><th>Organ Needed</th><th>Urgency</th><th>Status</th><th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($recipients as $recipient): ?>
<tr>
<td><?php echo htmlspecialchars($recipient['full_name']); ?></td>
<td><?php echo htmlspecialchars($recipient['email']); ?></td>
<td><?php echo htmlspecialchars($recipient['blood_type']); ?></td>
<td><?php echo htmlspecialchars($recipient['organ_needed']); ?></td>
<td><?php echo htmlspecialchars($recipient['urgency_level']); ?></td>
<td><span class="badge bg-secondary"><?php echo htmlspecialchars($recipient['status']); ?></span></td>
<td><?php echo date('Y-m-d', strtotime($recipient['registration_date'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<?php endif; // End approved status check ?>
</div>
<footer class="bg-dark text-white text-center p-3 mt-5">
<p>&copy; <?php echo date("Y"); ?> Organ Donation Management System. All Rights Reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>