<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<h3 class="text-orange fw-bold">House Numbering Management</h3>
<p class="text-muted">Manage all houses linked to their registered streets.</p>

<!-- Add New -->
<div class="d-flex justify-content-end mt-4">
    <a href="add_house.php" class="btn btn-orange px-4 py-2 shadow">+ Add New House</a>
</div>

<div class="card mt-4 shadow">
    <div class="card-header bg-orange text-white d-flex justify-content-between align-items-center">
        <h5 class="m-0" style="color:black;">All Registered Streets</h5>

        <!-- Realtime Search Bar -->
        <input type="text" 
               id="streetSearch" 
               class="form-control form-control-sm" 
               placeholder="Search street..."
               style="max-width:300px;">
    </div>

    <div class="card-body">
        <div class="table-responsive">

            <table class="table table-bordered table-hover">
                <thead class="table-light">
                    <tr>
                        <th>S/N</th>
                        <th>House Number</th>
                        <th>Street</th>
                        <th>Occupant</th>
                        <th>Status</th>
                        <th>Coordinates</th>
                        <th>Actions</th>
                    </tr>
                </thead>

                <tbody>
                    <?php
                  $sql =  "SELECT houses.*, registered_streets.street_name
        FROM houses
        JOIN registered_streets 
            ON houses.street_id = registered_streets.street_id
        WHERE houses.house_number IS NOT NULL
        ORDER BY houses.id DESC";

                    $result = $conn->query($sql);
                    $count = 1;

                    $result = $conn->query($sql);

if (!$result) {
    die("SQL Error: " . $conn->error);
}

if ($result->num_rows > 0) {

                        while ($row = $result->fetch_assoc()) {

                            echo "<tr>
                                <td>{$count}</td>
                                <td>{$row['house_number']}</td>
                                <td>{$row['street_name']}</td>
                                <td>{$row['occupant_name']}</td>
                                <td><span class='badge bg-" . ($row['status']=='verified'?'success':'warning') . "'>{$row['status']}</span></td>
                                <td>{$row['latitude']}, {$row['longitude']}</td>
                                <td>
                                    <a href='view_house.php?id={$row['id']}' class='btn btn-sm btn-primary'>View</a>
                                    <a href='edit_house.php?id={$row['id']}' class='btn btn-sm btn-warning text-white'>Edit</a>
                                    <a href='delete_house.php?id={$row['id']}' class='btn btn-sm btn-danger' onclick='return confirm(\"Delete this house?\")'>Delete</a>
                                </td>
                            </tr>";

                            $count++;
                        }
                    } else {
                        echo "<tr><td colspan='7' class='text-center text-muted'>No houses recorded yet.</td></tr>";
                    }
                    ?>
                </tbody>

            </table>

        </div>
    </div>
</div>

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