35311-vm/resident_edit.php
2025-10-30 00:12:08 +00:00

151 lines
7.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Check if user is logged in and has the 'staff' role
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'staff') {
header("Location: index.php");
exit;
}
$resident_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($resident_id === 0) {
header("Location: staff_dashboard.php");
exit;
}
$pdo = db();
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$sql = "UPDATE residents SET first_name = ?, last_name = ?, email = ?, phone_number = ?, date_of_birth = ?, program = ?, status = ?, health_progress = ?, housing_progress = ?, employment_progress = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([
$_POST['first_name'],
$_POST['last_name'],
$_POST['email'],
$_POST['phone_number'],
$_POST['date_of_birth'],
$_POST['program'],
$_POST['status'],
$_POST['health_progress'],
$_POST['housing_progress'],
$_POST['employment_progress'],
$resident_id
]);
header("Location: resident_view.php?id=" . $resident_id . "&success=2"); // Success code for update
exit;
} catch (PDOException $e) {
$error_message = "Database error: Could not update resident. " . $e->getMessage();
}
}
// Fetch resident details to pre-fill the form
$stmt = $pdo->prepare("SELECT * FROM residents WHERE id = ?");
$stmt->execute([$resident_id]);
$resident = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$resident) {
header("Location: staff_dashboard.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Resident - Continuum of Healing</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/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-dark">
<div class="container-fluid">
<a class="navbar-brand" href="staff_dashboard.php">Continuum of Healing</a>
</div>
</nav>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">Edit Resident Information</h1>
<a href="resident_view.php?id=<?php echo $resident_id; ?>" class="btn btn-secondary">← Back to View</a>
</div>
<div class="card">
<div class="card-body">
<form method="POST" action="resident_edit.php?id=<?php echo $resident_id; ?>">
<div class="row">
<div class="col-md-6 mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo htmlspecialchars($resident['first_name']); ?>" required>
</div>
<div class="col-md-6 mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo htmlspecialchars($resident['last_name']); ?>" required>
</div>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($resident['email']); ?>" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="phone_number" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone_number" name="phone_number" value="<?php echo htmlspecialchars($resident['phone_number']); ?>">
</div>
<div class="col-md-6 mb-3">
<label for="date_of_birth" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="date_of_birth" name="date_of_birth" value="<?php echo htmlspecialchars($resident['date_of_birth']); ?>">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="program" class="form-label">Program</label>
<select class="form-select" id="program" name="program">
<option <?php echo ($resident['program'] == 'General Support') ? 'selected' : ''; ?>>General Support</option>
<option <?php echo ($resident['program'] == 'Transitional Housing') ? 'selected' : ''; ?>>Transitional Housing</option>
<option <?php echo ($resident['program'] == 'Outreach') ? 'selected' : ''; ?>>Outreach</option>
<option <?php echo ($resident['program'] == 'Aftercare') ? 'selected' : ''; ?>>Aftercare</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status">
<option <?php echo ($resident['status'] == 'Active') ? 'selected' : ''; ?>>Active</option>
<option <?php echo ($resident['status'] == 'Inactive') ? 'selected' : ''; ?>>Inactive</option>
<option <?php echo ($resident['status'] == 'Pending') ? 'selected' : ''; ?>>Pending</option>
</select>
</div>
</div>
<hr class="my-4">
<h5 class="mb-3">Continuum Progress</h5>
<div class="row">
<div class="col-md-4 mb-3">
<label for="health_progress" class="form-label">Health Progress</label>
<input type="range" class="form-range" id="health_progress" name="health_progress" min="0" max="100" step="10" value="<?php echo htmlspecialchars($resident['health_progress']); ?>">
</div>
<div class="col-md-4 mb-3">
<label for="housing_progress" class="form-label">Housing Progress</label>
<input type="range" class="form-range" id="housing_progress" name="housing_progress" min="0" max="100" step="10" value="<?php echo htmlspecialchars($resident['housing_progress']); ?>">
</div>
<div class="col-md-4 mb-3">
<label for="employment_progress" class="form-label">Employment Progress</label>
<input type="range" class="form-range" id="employment_progress" name="employment_progress" min="0" max="100" step="10" value="<?php echo htmlspecialchars($resident['employment_progress']); ?>">
</div>
</div>
<button type="submit" class="btn btn-primary-custom">Save Changes</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>