<?php
// Ensure variables are set
$current_page = $current_page ?? 1;
$total_pages = $total_pages ?? 1;
$per_page = $per_page ?? 20;

// Generate base URL with existing query parameters
$query_params = $_GET;
unset($query_params['page']);
$base_url = strtok($_SERVER['REQUEST_URI'], '?') . '?' . http_build_query($query_params);
$base_url .= (strpos($base_url, '?') !== false ? '&' : '?');

// Determine range of page links to show
$range = 2; // Number of pages to show before/after current page
$start_page = max(1, $current_page - $range);
$end_page = min($total_pages, $current_page + $range);
?>

<?php if ($total_pages > 1): ?>
<nav aria-label="Navigacija stranica">
    <ul class="pagination justify-content-center">
        <?php 
        // Previous page link
        if ($current_page > 1): ?>
            <li class="page-item">
                <a class="page-link" href="<?php echo $base_url; ?>page=<?php echo $current_page - 1; ?>" aria-label="Prethodna">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        <?php endif; ?>

        <?php 
        // First page link
        if ($start_page > 1): ?>
            <li class="page-item <?php echo $current_page == 1 ? 'active' : ''; ?>">
                <a class="page-link" href="<?php echo $base_url; ?>page=1">1</a>
            </li>
            <?php if ($start_page > 2): ?>
                <li class="page-item disabled"><span class="page-link">...</span></li>
            <?php endif; ?>
        <?php endif; ?>

        <?php 
        // Page range links
        for ($page = $start_page; $page <= $end_page; $page++): ?>
            <li class="page-item <?php echo $current_page == $page ? 'active' : ''; ?>">
                <a class="page-link" href="<?php echo $base_url; ?>page=<?php echo $page; ?>"><?php echo $page; ?></a>
            </li>
        <?php endfor; ?>

        <?php 
        // Last page link
        if ($end_page < $total_pages): ?>
            <?php if ($end_page < $total_pages - 1): ?>
                <li class="page-item disabled"><span class="page-link">...</span></li>
            <?php endif; ?>
            <li class="page-item <?php echo $current_page == $total_pages ? 'active' : ''; ?>">
                <a class="page-link" href="<?php echo $base_url; ?>page=<?php echo $total_pages; ?>"><?php echo $total_pages; ?></a>
            </li>
        <?php endif; ?>

        <?php 
        // Next page link
        if ($current_page < $total_pages): ?>
            <li class="page-item">
                <a class="page-link" href="<?php echo $base_url; ?>page=<?php echo $current_page + 1; ?>" aria-label="Sledeća">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        <?php endif; ?>
    </ul>
</nav>

<div class="pagination-info text-center mt-2">
    Stranica <?php echo $current_page; ?> od <?php echo $total_pages; ?> 
    (Ukupno <?php echo $total_users; ?> korisnika)
</div>
<?php endif; ?>