<link rel="icon" href="logo.jpg" type="image/jpg">
<?php
// File: residents.php
// Master directory showing all residents joined with household data.
require_once "db.php";
check_login();

$view = isset($_GET['view']) && $_GET['view'] == 'archived' ? 'archived' : 'active';
$is_archived = ($view == 'archived') ? 1 : 0;
$search = $_GET['search'] ?? '';

$page_title = "Resident Master List";
include 'header.php';

$search_query = "";
if (!empty($search)) {
    $esc = $conn->real_escape_string($search);
    $search_query = " AND (r.first_name LIKE '%$esc%' OR r.last_name LIKE '%$esc%' OR h.purok LIKE '%$esc%')";
}

$sql = "SELECT r.*, h.household_no, h.purok 
        FROM residents r 
        JOIN households h ON r.household_id = h.id 
        WHERE r.is_archived = ? $search_query 
        ORDER BY r.last_name, r.first_name";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $is_archived);
$stmt->execute();
$result = $stmt->get_result();
?>

<div class="main-content">
    <div class="page-header" style="display:flex; justify-content: space-between; align-items: center;">
        <h1><?php echo $is_archived ? "Archived" : "Active"; ?> Residents Master List</h1>
        <div style="display:flex; gap:10px;">
            <a href="residents.php?view=<?php echo $is_archived ? 'active' : 'archived'; ?>" class="btn btn-secondary">
                <i class="fas fa-archive"></i> View <?php echo $is_archived ? "Active" : "Archived"; ?>
            </a>
            <a href="households.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add via Household</a>
        </div>
    </div>

    <div class="card">
        <form method="get" class="search-form" style="margin-bottom: 25px; display:flex; gap:10px;">
            <input type="hidden" name="view" value="<?php echo $view; ?>">
            <input type="text" name="search" class="form-control" placeholder="Search by name or purok..." value="<?php echo htmlspecialchars($search); ?>" style="flex: 1;">
            <button type="submit" class="btn btn-primary">Search</button>
            <?php if($search): ?>
                <a href="residents.php?view=<?php echo $view; ?>" class="btn btn-light">Clear</a>
            <?php endif; ?>
        </form>

        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Full Name</th>
                        <th>Age</th>
                        <th>Gender</th>
                        <th>Household</th>
                        <th>Purok</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if($result->num_rows > 0): while($row = $result->fetch_assoc()): 
                        $age = date_diff(date_create($row['birthday']), date_create('now'))->y; ?>
                        <tr>
                            <td><strong><?php echo strtoupper($row['last_name'] . ', ' . $row['first_name'] . ' ' . $row['suffix']); ?></strong></td>
                            <td><?php echo $age; ?></td>
                            <td><?php echo $row['gender']; ?></td>
                            <td><?php echo htmlspecialchars($row['household_no']); ?></td>
                            <td><?php echo htmlspecialchars($row['purok']); ?></td>
                            <td>
                                <?php if($row['is_pwd']): ?><span class="badge" style="background:#17a2b8; color: #fff;">PWD</span><?php endif; ?>
                            </td>
                            <td>
                                <a href="manage_members.php?household_id=<?php echo $row['household_id']; ?>&edit_id=<?php echo $row['id']; ?>" class="btn btn-sm btn-primary">View/Edit</a>
                            </td>
                        </tr>
                    <?php endwhile; else: ?>
                        <tr><td colspan="7" class="text-center">No residents found in the current selection.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>