35370-vm/index.php
Flatlogic Bot 275165284b 1.0
2025-10-31 00:13:36 +00:00

136 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
ini_set('display_errors', '1');
error_reporting(E_ALL);
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/db/database.php';
$pdo = get_db_connection();
// Search logic
$search = $_GET['search'] ?? '';
$sql = 'SELECT * FROM members';
$params = [];
if ($search) {
$sql .= ' WHERE full_name LIKE ? OR department LIKE ? OR city LIKE ?';
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
}
$sql .= ' ORDER BY created_at DESC';
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$members = $stmt->fetchAll();
// Fetch dependents for each member
$dependentsByMember = [];
if ($members) {
$memberIds = array_column($members, 'id');
if (!empty($memberIds)) {
$placeholders = implode(',', array_fill(0, count($memberIds), '?'));
$stmt = $pdo->prepare("SELECT * FROM dependents WHERE member_id IN ($placeholders) ORDER BY created_at DESC");
$stmt->execute($memberIds);
$dependents = $stmt->fetchAll();
foreach ($dependents as $dependent) {
$dependentsByMember[$dependent['member_id']][] = $dependent;
}
}
}
function calculate_age($dob) {
if (!$dob) return 'N/A';
$birthDate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthDate->diff($today)->y;
return $age;
}
include __DIR__ . '/layout/header.php';
?>
<h1>Members</h1>
<div class="actions">
<a href="add-member.php" class="btn">Add New Member</a>
<form action="index.php" method="get" class="search-form">
<input type="text" name="search" placeholder="Search by name, department, city..." value="<?php echo htmlspecialchars($search); ?>">
<button type="submit" class="btn">Search</button>
</form>
</div>
<?php if (isset($_GET['success_message'])): ?>
<div class="alert alert-success">
<?php echo htmlspecialchars($_GET['success_message']); ?>
</div>
<?php endif; ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Job Title</th>
<th>Department</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($members)): ?>
<tr>
<td colspan="5">No members found.</td>
</tr>
<?php else: ?>
<?php foreach ($members as $member): ?>
<tr>
<td><?php echo htmlspecialchars($member['full_name']); ?></td>
<td><?php echo htmlspecialchars($member['email']); ?></td>
<td><?php echo htmlspecialchars($member['job_title']); ?></td>
<td><?php echo htmlspecialchars($member['department']); ?></td>
<td>
<a href="add-dependent.php?member_id=<?php echo $member['id']; ?>" class="btn btn-sm">Add Dependent</a>
<a href="edit-member.php?id=<?php echo $member['id']; ?>" class="btn btn-sm">Edit</a>
<a href="delete-member.php?id=<?php echo $member['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this member and all their dependents?');">Delete</a>
</td>
</tr>
<?php if (!empty($dependentsByMember[$member['id']])): ?>
<tr>
<td colspan="5">
<table class="dependents-table">
<thead>
<tr>
<th>Dependent Name</th>
<th>Relationship</th>
<th>Date of Birth</th>
<th>Age</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($dependentsByMember[$member['id']] as $dependent): ?>
<tr>
<td><?php echo htmlspecialchars($dependent['full_name']); ?></td>
<td><?php echo htmlspecialchars($dependent['relationship']); ?></td>
<td><?php echo htmlspecialchars($dependent['date_of_birth']); ?></td>
<td><?php echo calculate_age($dependent['date_of_birth']); ?></td>
<td>
<a href="edit-dependent.php?id=<?php echo $dependent['id']; ?>" class="btn btn-sm">Edit</a>
<a href="delete-dependent.php?id=<?php echo $dependent['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this dependent?');">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<?php include __DIR__ . '/layout/footer.php'; ?>