<?php
// File: solo_parents.php
// Dedicated module for managing Solo Parents in the Barangay.
require_once "db.php";
check_login();

$page_title = "Solo Parent Management";
include 'header.php';
?>

<div class="main-content">
    <div class="page-header">
        <h1>Solo Parent Management</h1>
        <p>Manage and view all registered solo parents in Barangay San Fabian.</p>
    </div>

    <div class="card">
        <h2 class="card-title">Solo Parent Masterlist</h2>
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Solo Parent ID</th>
                        <th>Name</th>
                        <th>Age</th>
                        <th>Gender</th>
                        <th>Purok</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql = "SELECT r.*, h.purok 
                            FROM residents r 
                            JOIN households h ON r.household_id = h.id 
                            WHERE r.is_solo_parent = 1 AND r.is_archived = 0 
                            ORDER BY r.last_name, r.first_name";
                    $result = $conn->query($sql);

                    if ($result->num_rows > 0) {
                        while($row = $result->fetch_assoc()) {
                            $name = htmlspecialchars(strtoupper($row['last_name'] . ', ' . $row['first_name']));
                            $age = date_diff(date_create($row['birthday']), date_create('now'))->y;
                            echo "<tr>";
                            echo "<td><strong>" . ($row['solo_parent_id'] ?: 'NOT SET') . "</strong></td>";
                            echo "<td>$name</td>";
                            echo "<td>$age</td>";
                            echo "<td>{$row['gender']}</td>";
                            echo "<td>{$row['purok']}</td>";
                            echo "<td><a href='manage_members.php?household_id={$row['household_id']}&edit_id={$row['id']}' class='btn btn-sm btn-primary'>Manage Status</a></td>";
                            echo "</tr>";
                        }
                    } else {
                        echo "<tr><td colspan='6' class='text-center'>No solo parents found.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>