<?php
require 'includes/db_connect.php';

$limit = 10;
$search = isset($_GET['q']) ? trim($_GET['q']) : "";
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? intval($_GET['page']) : 1;
if ($page < 1) { $page = 1; }

$offset = ($page - 1) * $limit;

$where = "";
if ($search !== "") {
    $safe = $conn->real_escape_string($search);
    $where = "WHERE street_name LIKE '%$safe%' OR town LIKE '%$safe%' OR reg_no LIKE '%$safe%'";
}

$countQuery = "SELECT COUNT(*) AS total FROM registered_streets $where";
$countResult = $conn->query($countQuery);
$totalRows = $countResult->fetch_assoc()['total'] ?? 0;
$totalPages = ($totalRows > 0) ? ceil($totalRows / $limit) : 1;

$sql = "SELECT * FROM registered_streets $where ORDER BY street_id DESC LIMIT $limit OFFSET $offset";
$result = $conn->query($sql);
?>

<style>
/* Table Styling */
.table thead th {
    background: #ffbc5a !important;
    color: #000;
    font-weight: bold;
}

.table-hover tbody tr:hover {
    background: #fff3e0;
    transition: 0.2s ease-in-out;
}

td, th {
    vertical-align: middle !important;
}

/* Action Buttons */
.btn-sm {
    border-radius: 8px;
    font-weight: 600;
}

.btn-primary { background: #0066ff; border: none; }
.btn-warning { background: #f7931e; border: none; }
.btn-danger { background: #e63946; border: none; }

.btn-primary:hover { opacity: 0.85; }
.btn-warning:hover { opacity: 0.85; }
.btn-danger:hover { opacity: 0.85; }

/* Pagination */
.pagination .page-item .page-link {
    color: #ff7b00;
    border-radius: 6px;
    margin: 0 3px;
    font-weight: 600;
}

.pagination .active .page-link {
    background: #ff7b00 !important;
    color: #fff !important;
    border: none;
}

.pagination .page-link:hover {
    background: #ffe0c2;
}

/* Highlight Search Text */
.highlight {
    background: yellow;
    padding: 0px 3px;
    border-radius: 3px;
}
</style>

<script>
// Function to highlight search matches
function highlight(text, search) {
    if (!search.trim()) return text;
    const regex = new RegExp(search, "gi");
    return text.replace(regex, match => `<span class="highlight">${match}</span>`);
}
</script>
<!-- Export Buttons -->
<div class="d-flex justify-content-end mb-3 gap-2">
    <a href="export_streets_pdf.php" class="btn btn-danger btn-sm rounded-pill px-3">
        Export PDF
    </a>

    <a href="export_streets_excel.php" class="btn btn-success btn-sm rounded-pill px-3">
        Export Excel
    </a>
</div>

<div class="table-responsive">
    <table class="table table-bordered table-hover shadow-sm rounded">
        <thead>
            <tr>
                <th>#</th>
                <th>Street Name</th>
                <th>Location</th>
                <th>Reg Number</th>
                <th>Numbered?</th>
                <th>Approval Year</th>
                <th width="150">Actions</th>
            </tr>
        </thead>

        <tbody>
        <?php
        if ($result && $result->num_rows > 0) {
            $sn = $offset + 1;

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

                // Highlight values for search
                $street = $search ? preg_replace("/($search)/i", "<span class='highlight'>$1</span>", $row['street_name']) : $row['street_name'];
                $town = $search ? preg_replace("/($search)/i", "<span class='highlight'>$1</span>", $row['town']) : $row['town'];
                $regno = $search ? preg_replace("/($search)/i", "<span class='highlight'>$1</span>", $row['reg_no']) : $row['reg_no'];
                $street_numbered = $search ? preg_replace("/($search)/i", "<span class='highlight'>$1</span>", $row['street_numbered']) : $row['street_numbered'];

                echo "
                <tr>
                    <td>{$sn}</td>
                    <td>{$street}</td>
                    <td>{$town}</td>
                    <td>{$regno}</td>
                    <td>{$street_numbered}</td>
                    <td>{$row['approval_year']}</td>
                    <td>
                        <a href='view_street.php?id={$row['street_id']}' class='btn btn-sm btn-primary mb-1'>View</a>
                       
                    </td>
                </tr>";
                $sn++;
            }
        } else {
            echo "<tr><td colspan='6' class='text-center text-muted py-3'>No results found.</td></tr>";
        }
        ?>
        </tbody>
    </table>
</div>

<!-- PAGINATION -->
<?php if ($totalPages > 1): ?>
<nav>
    <ul class="pagination justify-content-center mt-3">

        <!-- Previous -->
        <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
            <a class="page-link pagination-link" data-page="<?= $page - 1 ?>" href="#">
                &laquo;
            </a>
        </li>

        <?php
        for ($i = 1; $i <= $totalPages; $i++) {
            $active = ($i == $page) ? "active" : "";
            echo "
            <li class='page-item $active'>
                <a class='page-link pagination-link' data-page='$i' href='#'>$i</a>
            </li>";
        }
        ?>

        <!-- Next -->
        <li class="page-item <?= ($page >= $totalPages) ? 'disabled' : '' ?>">
            <a class="page-link pagination-link" data-page="<?= $page + 1 ?>" href="#">
                &raquo;
            </a>
        </li>

    </ul>
</nav>
<?php endif; ?>
