<?php
$allowed = [
    'total_margin',
    'active_trading',
    'total_lots',
    'stop_outs',
    'profit_order',
    'profitability_score',
    'new_users',
    'spread_cost',
    'rollover_cost',
    'net_deposit',
    'net_withdrawal',
    'active_funds'
];
$defaultFilter = 'total_margin';
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Broker Ranking</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f9f9f9;
            padding: 30px;
        }

        .ranking {
            margin: 17px;
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.07);
            max-width: 100%;
            overflow-x: hidden;
        }

        .title-tab {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }

        .rank-ul {
            padding: 10px 18px;
            border-radius: 20px;
            background: #eee;
            cursor: pointer;
            font-weight: bold;
            transition: 0.2s;
        }

        .rank-ul.active {
            background: #007bff;
            color: white;
        }

        .list-title {
            font-size: 24px;
            font-weight: bold;
            margin: 0 auto 10px;
            color: #333;
            max-width: 1400px;
            width: min(1400px, calc(100% - 24px));
            box-sizing: border-box;
            padding: 0;
        }

        .list-head ul,
        .list-list ul {
            display: flex;
            list-style: none;
            padding: 10px;
            border-bottom: 1px solid #ddd;
        }

        #rankList {
            max-width: 1400px;
            width: min(1400px, calc(100% - 24px));
            margin: 0 auto;
            box-sizing: border-box;
        }

        .list-1,
        .list-2,
        .list-3,
        .list-4 {
            flex: 1;
        }

        .list-2 img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
            vertical-align: middle;
        }

        .rank-1 {
            color: gold;
            font-weight: bold;
        }

        .rank-2 {
            color: silver;
            font-weight: bold;
        }

        .rank-3 {
            color: #cd7f32;
            font-weight: bold;
        }

        /* Styles from the second style block merged here */
        .title-tab {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin-bottom: 20px;
            gap: 10px;
        }

        .rank-ul {
            padding: 10px 16px;
            border-radius: 20px;
            background-color: #f0f0f0;
            cursor: pointer;
            font-weight: 600;
            color: #333;
            transition: background 0.3s, color 0.3s;
            user-select: none;
        }

        .rank-ul:hover,
        .rank-ul.active {
            background-color: #007bff;
            color: white;
        }

        /* ===== TABLET BREAKPOINT (max-width: 1100px) ===== */
        @media (max-width: 1100px) {
            body {
                padding: 20px;
            }

            .ranking {
                margin: 12px;
                padding: 18px;
            }

            .list-title {
                font-size: 20px;
                width: min(1200px, calc(100% - 20px));
            }

            #rankList {
                width: min(1200px, calc(100% - 20px));
            }

            .title-tab {
                gap: 10px;
                margin-bottom: 16px;
            }

            .rank-ul {
                padding: 8px 14px;
                font-size: 13px;
            }

            .list-head ul,
            .list-list ul {
                padding: 8px;
            }

            .list-1,
            .list-2,
            .list-3,
            .list-4 {
                flex: 1;
                font-size: 13px;
            }

            .list-2 img {
                width: 28px;
                height: 28px;
                margin-right: 8px;
            }
        }

        /* ===== MOBILE BREAKPOINT (max-width: 768px) ===== */
        @media (max-width: 768px) {
            body {
                padding: 12px;
                background: #f5f5f5;
            }

            .ranking {
                margin: 8px;
                padding: 12px;
                border-radius: 6px;
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            }

            .list-title {
                font-size: 18px;
                margin-bottom: 8px;
                width: calc(100% - 16px);
                max-width: none;
            }

            #rankList {
                width: calc(100% - 16px);
                max-width: none;
            }

            .title-tab {
                display: flex;
                flex-wrap: wrap;
                gap: 8px;
                margin-bottom: 12px;
                justify-content: center;
            }

            .rank-ul {
                padding: 6px 12px;
                font-size: 12px;
                border-radius: 16px;
            }

            .list-head ul,
            .list-list ul {
                display: flex;
                flex-wrap: wrap;
                padding: 6px;
                gap: 4px;
            }

            .list-1,
            .list-2,
            .list-3,
            .list-4 {
                flex: 0 0 calc(50% - 2px);
                font-size: 12px;
                padding: 4px;
            }

            .list-2 img {
                width: 24px;
                height: 24px;
                margin-right: 6px;
            }

            .rank-1,
            .rank-2,
            .rank-3 {
                font-size: 12px;
            }
        }

        /* ===== SMALL MOBILE BREAKPOINT (max-width: 480px) ===== */
        @media (max-width: 480px) {
            body {
                padding: 8px;
            }

            .ranking {
                margin: 4px;
                padding: 8px;
                border-radius: 4px;
            }

            .list-title {
                font-size: 16px;
                margin-bottom: 6px;
                width: calc(100vw - 20px);
                max-width: none;
                padding: 0 4px;
            }

            #rankList {
                width: calc(100vw - 20px);
                max-width: none;
                margin: 0 auto;
                padding: 0 4px;
            }

            .title-tab {
                gap: 6px;
                margin-bottom: 10px;
            }

            .rank-ul {
                padding: 5px 10px;
                font-size: 11px;
                border-radius: 14px;
                flex: 0 1 auto;
            }

            .list-head ul,
            .list-list ul {
                display: flex;
                flex-direction: column;
                padding: 4px;
                gap: 2px;
            }

            .list-1,
            .list-2,
            .list-3,
            .list-4 {
                flex: 0 0 100%;
                font-size: 11px;
                padding: 3px;
                width: 100%;
            }

            .list-2 img {
                width: 20px;
                height: 20px;
                margin-right: 4px;
            }

            .rank-1,
            .rank-2,
            .rank-3 {
                font-size: 11px;
            }
        }
    </style>
</head>

<body>


    <section aria-live="polite" aria-atomic="true" id="rankList" class="list rankList-list">
        <div class="list-title">Loading Rankings...</div>
        <div class="list-head">
            <ul>
                <li></li>
            </ul>
        </div>

        <div class="list-list" id="brokerList">
            <div>Loading...</div>
        </div>
    </section>

    </div>

    <script>
        // First Script Block Logic
        document.addEventListener("DOMContentLoaded", () => {
            const tabs = document.querySelectorAll(".rank-ul");
            const brokerList = document.getElementById("brokerList");
            const listTitle = document.querySelector(".list-title");

            function getCurrentFilter() {
                const urlParams = new URLSearchParams(window.location.search);
                return urlParams.get('filter') || 'total_margin';
            }

            function updateActiveTab(selectedFilter) {
                tabs.forEach(tab => {
                    tab.classList.toggle("active", tab.dataset.filter === selectedFilter);
                });
            }

            function formatTitle(filterKey) {
                return filterKey.replace(/_/g, " ").replace(/\b\w/g, l => l.toUpperCase());
            }

            function loadBrokers(filter) {
                fetch("get_rights.php?filter=" + encodeURIComponent(filter))
                    .then(response => response.text())
                    .then(html => {
                        brokerList.innerHTML = html;
                        listTitle.textContent = formatTitle(filter) + " Rankings";
                        updateActiveTab(filter);
                    });
            }

            // Initial load
            const initialFilter = getCurrentFilter();
            loadBrokers(initialFilter);

            // Event Listeners for Filters
            tabs.forEach(tab => {
                tab.addEventListener("click", () => {
                    const selectedFilter = tab.dataset.filter;
                    history.replaceState(null, '', '?filter=' + selectedFilter);
                    loadBrokers(selectedFilter);
                });
            });
        });

        // Second Script Block Logic (Smooth Scrolling & Active State fallback)
        document.querySelectorAll('.rank-ul').forEach(button => {
            button.addEventListener('click', () => {
                const filterId = button.dataset.filter;
                const section = document.getElementById(filterId);
                if (section) {
                    section.scrollIntoView({
                        behavior: 'smooth',
                        block: 'start'
                    });

                    // Highlight the active tab
                    document.querySelectorAll('.rank-ul').forEach(el => el.classList.remove('active'));
                    button.classList.add('active');
                }
            });
        });
    </script>

</body>

</html>