34981-vm/hospital-treatments.php
Flatlogic Bot e59c8581a5 1.0.1
2025-10-15 18:48:09 +00:00

139 lines
5.4 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Ensure user is logged in and is a hospital admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'hospital') {
header("Location: login.php");
exit;
}
$hospitalId = $_SESSION['user_id'];
$message = '';
try {
$pdo = db();
// Create table if it doesn't exist
$pdo->exec("CREATE TABLE IF NOT EXISTS `treatment_categories` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`hospital_id` INT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`description` TEXT,
FOREIGN KEY (`hospital_id`) REFERENCES `hospitals`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['category_name'])) {
$categoryName = trim($_POST['category_name']);
$description = trim($_POST['description']);
if (!empty($categoryName)) {
$stmt = $pdo->prepare("INSERT INTO treatment_categories (hospital_id, name, description) VALUES (:hospital_id, :name, :description)");
$stmt->bindParam(':hospital_id', $hospitalId, PDO::PARAM_INT);
$stmt->bindParam(':name', $categoryName, PDO::PARAM_STR);
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
if ($stmt->execute()) {
$message = '<div class="alert alert-success">Treatment category added successfully!</div>';
} else {
$message = '<div class="alert alert-danger">Failed to add category.</div>';
}
} else {
$message = '<div class="alert alert-warning">Category name is required.</div>';
}
}
// Fetch existing categories for this hospital
$stmt = $pdo->prepare("SELECT * FROM treatment_categories WHERE hospital_id = :hospital_id ORDER BY name");
$stmt->bindParam(':hospital_id', $hospitalId, PDO::PARAM_INT);
$stmt->execute();
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
$categories = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Treatment Categories - Medicaltour</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>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">Medicaltour</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="dashboard.php">Dashboard</a></li>
<li class="nav-item"><a class="nav-link" href="logout.php">Logout</a></li>
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<main class="container mt-5 pt-5">
<section id="manage-treatments" class="py-5">
<h2 class="mb-4">Manage Treatment Categories</h2>
<?php echo $message; ?>
<!-- Add Category Form -->
<div class="card mb-4">
<div class="card-header">Add New Category</div>
<div class="card-body">
<form action="hospital-treatments.php" method="POST">
<div class="mb-3">
<label for="category_name" class="form-label">Category Name</label>
<input type="text" class="form-control" id="category_name" name="category_name" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description (Optional)</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add Category</button>
</form>
</div>
</div>
<!-- Existing Categories List -->
<div class="card">
<div class="card-header">Your Treatment Categories</div>
<div class="card-body">
<?php if (empty($categories)): ?>
<p>You have not added any treatment categories yet.</p>
<?php else: ?>
<ul class="list-group">
<?php foreach ($categories as $category): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<h6 class="my-0"><?php echo htmlspecialchars($category['name']); ?></h6>
<small class="text-muted"><?php echo htmlspecialchars($category['description']); ?></small>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</section>
</main>
<!-- Footer -->
<footer class="py-5 bg-dark text-white mt-auto">
<div class="container text-center">
<p>&copy; 2025 Medicaltour. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>