<?php
include "config.php";

$image_base_url = "/uploads/";

// Pagination settings
$limit = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Count total brokers
$total_sql = "SELECT COUNT(*) FROM brokers";
$total_stmt = $dbh->prepare($total_sql);
$total_stmt->execute();
$total_brokers = $total_stmt->fetchColumn();
$total_pages = ceil($total_brokers / $limit);

// Fetch brokers for current page
$sql = "SELECT * FROM brokers ORDER BY score DESC LIMIT :limit OFFSET :offset";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$brokers = $stmt->fetchAll(PDO::FETCH_ASSOC);

$rank = $offset + 1;


if ($brokers && count($brokers) > 0) {
    

    echo '<div class="brokers-container" id="div1">';

    foreach ($brokers as $row) {
        $logo_url = $image_base_url . $row['logo'];
        $asset = $row['commodity'] ? 'Oil' : ($row['currency'] ? 'Currency' : 'Asset');
        $years_in_business = !empty($row['experience_years']) ? $row['experience_years'] . ' years' : 'N/A';
        $broker_page = "avatrade.php?name=" . urlencode(strtolower($row['name']));

        echo '
        <div class="broker-item">
            <a href="' . $broker_page . '" class="broker-item-link">
                <div class="broker-contents">
                    <div class="broker-logo-wrapper">
                        <span class="regulated-badge">' . htmlspecialchars($row['broker_type']) . '</span>
                        <img alt="' . htmlspecialchars($row['name']) . '" src="' . htmlspecialchars($logo_url) . '" loading="lazy" class="com-img" style="height:100%;">
                    </div>
                    <div class="broker-info">
                        <h2 class="broker-name">' . htmlspecialchars($row['name']) . '</h2>
                        <div class="score">' . number_format($row['score'], 2) . '</div>
                        <span class="broker-years">' . $asset . ' • ' . $years_in_business . '</span>
                    </div>
                    <span class="rank-number">' . ($row['rank'] ?? $rank++) . '</span>
                </div>
            </a>
        </div>';
        
         
    }

    echo '</div>'; // End .brokers-container

    // Pagination links
    if ($total_pages > 1) {
        echo '<div class="pagination">';

        if ($page > 1) {
            echo '<a href="?page=' . ($page - 1) . '">&laquo; Prev</a>';
        }

        for ($i = 1; $i <= $total_pages; $i++) {
            $active = ($i == $page) ? 'active' : '';
            echo '<a class="' . $active . '" href="?page=' . $i . '">' . $i . '</a>';
        }

        if ($page < $total_pages) {
            echo '<a href="?page=' . ($page + 1) . '">Next &raquo;</a>';
        }

        echo '</div>';
       
    }

} else {
    echo "<p>No brokers found. Please check your database.</p>";
}


  
?>


    

<style>
.pagination {
    margin-top: 20px;
    text-align: center;
}
.pagination a {
    padding: 8px 12px;
    margin: 0 3px;
    background: #eee;
    text-decoration: none;
    color: #333;
    border-radius: 5px;
}
.pagination a.active {
    background: #007bff;
    color: #fff;
    font-weight: bold;
}
.pagination a:hover {
    background: #0056b3;
    color: white;
}
#div1{
    width:300px;
}

#main{
    display: flex;
}

  @media (max-width: 768px) {
      #div1{
    width:100%;
}
  }
</style>
