<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ColorTraderPro - Login</title>
    <style>
        body {
            background-color: black;
            color: lime;
            font-family: monospace;
            text-align: center;
            padding: 20px;
        }
        #loginPage {
            display: block;
        }
        #content {
            display: none;
        }
        input {
            padding: 10px;
            margin: 10px;
            background: black;
            color: lime;
            border: 1px solid lime;
        }
        button {
            padding: 10px;
            margin-top: 10px;
            background: darkgreen;
            color: white;
            border: none;
            cursor: pointer;
        }
        #output {
            font-size: 24px;
            font-weight: bold;
            color: red;
            margin-bottom: 20px;
        }
        table {
            width: 80%;
            margin: auto;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid lime;
            padding: 10px;
            text-align: center;
        }
        th {
            background-color: darkgreen;
        }
        .typing {
            font-weight: bold;
            color: cyan;
        }
    </style>
</head>
<body>

    <!-- Login Page -->
    <div id="loginPage">
        <h2>Login to Access ColorTraderPro</h2>
        <input type="text" id="username" placeholder="Username"><br>
        <input type="password" id="password" placeholder="Password"><br>
        <button onclick="checkLogin()">Login</button>
        <p id="error" style="color: red;"></p>
    </div>

    <!-- Main Content -->
    <div id="content">
        <div id="output">ColorTraderPro</div>
        <div id="typingEffect" class="typing"></div>
        <h2>Current Period & Result</h2>
        <table>
            <tr><th>PERIOD</th><th>RESULT</th></tr>
            <tr id="currentData"><td>Loading...</td><td>Loading...</td></tr>
        </table>
        <h2>Historical Data (Last 5 Periods)</h2>
        <table id="historyTable"><tr><th>PERIOD</th><th>RESULT</th></tr></table>
        <h2>[Timer] Countdown: <span id="countdown">  : 59</span></h2>
    </div>

    <script>
        function checkLogin() {
            let user = document.getElementById("username").value;
            let pass = document.getElementById("password").value;
            
            if (user === "VIP" && pass === "VIP69") {
                document.getElementById("loginPage").style.display = "none";
                document.getElementById("content").style.display = "block";
                updatePeriodAndTimer();
                startCountdown();
            } else {
                document.getElementById("error").innerText = "Invalid Credentials!";
            }
        }

        let history = [];
        let lastUpdatedMinute = -1; // पिछला अपडेटेड मिनट को स्टोर करने के लिए

        function typingEffect(text, elementId, delay = 50) {
            let i = 0;
            let element = document.getElementById(elementId);
            element.innerHTML = "";
            function type() {
                if (i < text.length) {
                    element.innerHTML += text[i];
                    i++;
                    setTimeout(type, delay);
                }
            }
            type();
        }

        function updatePeriodAndTimer(forceUpdate = false) {
            let now = new Date();
            let utcHours = now.getUTCHours();
            let utcMinutes = now.getUTCMinutes();

            // ✅ Check if period already updated in this minute
            if (!forceUpdate && lastUpdatedMinute === utcMinutes) {
                return;
            }

            lastUpdatedMinute = utcMinutes; // ✅ Update last updated minute

            let pattern = ["GREEN", "SMALL", "BIG", "RED", "RED", "SMALL", "GREEN", "BIG"];
            
let currentPattern = pattern[utcMinutes % pattern.length];
            let period1m = now.getUTCFullYear().toString() + 
                           String(now.getUTCMonth() + 1).padStart(2, '0') + 
                           String(now.getUTCDate()).padStart(2, '0') + 
                           "1000" + 
                           (10001 + utcMinutes);

            history.push({ period: period1m, result: currentPattern });
            if (history.length > 5) history.shift();

            document.getElementById("currentData").innerHTML = `<td>${period1m}</td><td>${currentPattern}</td>`;

            let historyTable = document.getElementById("historyTable");
            historyTable.innerHTML = "<tr><th>PERIOD</th><th>RESULT</th></tr>";
            history.forEach(entry => {
                historyTable.innerHTML += `<tr><td>${entry.period}</td><td>${entry.result}</td></tr>`;
            });

            typingEffect("[Processing Data...]", "typingEffect", 40);
            setTimeout(() => {
                typingEffect("[Data Retrieved Successfully]", "typingEffect", 40);
            }, 1000);
        }

        function startCountdown() {
            function updateTimer() {
                let now = new Date();
                let seconds = now.getSeconds();
                let remainingSeconds = 59 - seconds;

                document.getElementById("countdown").innerText = `  : ${remainingSeconds.toString().padStart(2, '0')}`;

                if (remainingSeconds === 0) {
                    updatePeriodAndTimer(true); // ✅ अब जैसे ही 60 सेकंड खत्म होगा, नया PERIOD लोड होगा
                }

                requestAnimationFrame(updateTimer);
            }

            updateTimer();
        }

        // ✅ जब Tab Minimize से वापस आए, तो Period को फिर से अपडेट करें
        document.addEventListener("visibilitychange", function () {
            if (!document.hidden) {
                updatePeriodAndTimer(true);
            }
        });

    </script>

</body>
</html>
