<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vizz Gen</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            width: 100%;
            max-width: 400px;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h2, h3 {
            color: #333;
        }

        input {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border-radius: 5px;
            border: 1px solid #ccc;
            font-size: 14px;
        }

        button {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border-radius: 5px;
            border: none;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            opacity: 0.9;
        }

        .login-button {
            background-color: #007bff;
            color: white;
        }

        .discord-link {
            background-color: #7289da;
            color: white;
            font-size: 16px;
        }

        .copy-button {
            background-color: #28a745;
            color: white;
            font-size: 14px;
            border-radius: 5px;
            cursor: pointer;
            padding: 8px 15px;
            margin-top: 10px;
        }

        .copy-button:hover {
            background-color: #218838;
        }

        .generate-button {
            background-color: #ff6600;
            color: white;
            font-size: 16px;
            border-radius: 5px;
        }

        .generate-button:hover {
            background-color: #cc5200;
        }

        .output-container {
            background-color: #f7f7f7;
            padding: 15px;
            margin-top: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            text-align: left;
        }

        .error, .success {
            font-size: 14px;
            margin-top: 10px;
        }

        .error {
            color: red;
        }

        .success {
            color: green;
        }

        .discord-link {
            margin-top: 15px;
            background-color: #7289da;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Logowanie -->
        <div id="login-container">
            <h2>Logowanie</h2>
            <input type="text" id="username" placeholder="Nazwa użytkownika" required>
            <input type="password" id="password" placeholder="Hasło" required>
            <button class="login-button" onclick="checkLogin()">Zaloguj się</button>
            <button class="discord-link" onclick="window.location.href='https://discord.gg/GGASjCmD4r'">Kup dostęp</button>
            <div id="error-message" class="error"></div>
        </div>

        <!-- Powitanie po zalogowaniu -->
        <div id="welcome-container" style="display: none;">
            <h2 id="welcome-message"></h2>
            <p id="expiry-info"></p>
        </div>

        <!-- Generator Kodów PSC (widoczny po zalogowaniu jako admin) -->
        <div id="psc-generator-container" style="display: none;">
            <h3>Generator Kodów PSC</h3>
            <input type="number" id="num-codes" placeholder="Ilość kodów" min="1">
            <button class="generate-button" onclick="generatePscCodes()">Generuj kody</button>

            <div id="psc-output" class="output-container" style="display: none;">
                <p id="success-message"></p>
                <button id="copy-codes-button" class="copy-button" style="display: none;">Kopiuj kody</button>
                <button id="save-codes-button" class="copy-button" style="display: none;">Zapisz kody (.txt)</button>
            </div>
        </div>
    </div>

    <script>
        // Lista użytkowników
        const USERS = [
            { username: "1", password: "1", expiry: "2124-12-17 21:58" },
            { username: "bruhiek", password: "tajnehaslo", expiry: "2025-01-01 12:51" },
            { username: "ania", password: "kotek123", expiry: "2024-12-24 21:59" }
        ];

        // Funkcja obliczająca pozostały czas
        function getRemainingTime(expiryDate) {
            const now = new Date();
            const timeDifference = expiryDate - now; // Różnica czasu w milisekundach

            if (timeDifference <= 0) {
                return "Dostęp wygasł";
            }

            const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
            const hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));

            // Obliczanie lat z dni
            const years = Math.floor(days / 365);
            const remainingDays = days % 365;

            let timeString = "";

            // Jeśli pozostały czas to lata
            if (years > 0) {
                timeString += `${years}y`;
                if (remainingDays > 0) {
                    timeString += `,${remainingDays}d`;
                }
            } else {
                // Jeśli są dni i godziny
                if (remainingDays > 0) {
                    timeString += `${remainingDays}d`;
                    if (hours > 0) {
                        timeString += `,${hours}h`;
                    }
                } else if (hours > 0) {
                    timeString += `${hours}h`;
                }
            }

            // Jeśli są minuty i nie ma dni ani godzin, dodaj tylko minuty
            if (minutes > 0 && remainingDays === 0 && hours === 0) {
                if (timeString) {
                    timeString += `,`;
                }
                timeString += `${minutes}m`;
            }

            return timeString;
        }

        // Sprawdzanie logowania
        function checkLogin() {
            const enteredUsername = document.getElementById("username").value;
            const enteredPassword = document.getElementById("password").value;
            const errorMessage = document.getElementById("error-message");

            // Szukanie użytkownika
            const userFound = USERS.find(user => user.username === enteredUsername && user.password === enteredPassword);

            if (userFound) {
                // Sprawdzenie daty wygaśnięcia dostępu
                const now = new Date();
                const expiryDate = new Date(userFound.expiry);

                if (now > expiryDate) {
                    errorMessage.style.color = "red";
                    errorMessage.innerText = "Błąd: Twój dostęp wygasł!";
                } else {
                    // Ukrycie logowania, pokazanie powitania
                    document.getElementById("login-container").style.display = "none";
                    document.getElementById("welcome-container").style.display = "block";
                    
                    // Wyświetlenie wiadomości powitalnej
                    document.getElementById("welcome-message").innerText = `Witaj, ${userFound.username}!`;
                    document.getElementById("expiry-info").innerText = `Twój dostęp wygasa: ${userFound.expiry} (${getRemainingTime(expiryDate)})`;

                    // Jeśli zalogował się admin, pokaż generator kodów PSC
                    document.getElementById("psc-generator-container").style.display = "block";
                }
            } else {
                errorMessage.style.color = "red";
                errorMessage.innerText = "Błąd: Nieprawidłowa nazwa użytkownika lub hasło.";
            }
        }

        // Generowanie kodów PSC
        function generatePscCodes() {
            const numCodes = document.getElementById("num-codes").value;
            const successMessage = document.getElementById("success-message");

            if (numCodes < 1) {
                successMessage.innerText = "Proszę podać liczbę kodów!";
                return;
            }

            const pscCodes = [];

            for (let i = 0; i < numCodes; i++) {
                pscCodes.push(generatePscCode());
            }

            // Pokazywanie wyników
            document.getElementById("psc-output").style.display = "block";
            successMessage.innerText = `Wygenerowano ${numCodes} kodów PSC:`;

            // Zapisanie kodów do pliku
            document.getElementById("save-codes-button").onclick = function() {
                const blob = new Blob([pscCodes.join("\n")], { type: "text/plain" });
                const link = document.createElement("a");
                link.href = URL.createObjectURL(blob);
                link.download = `VizzGen-${numCodes}-PSC.txt`; // Poprawiona nazwa pliku
                link.click();
            };

            // Zmiana przycisku kopiowania
            document.getElementById("copy-codes-button").style.display = "inline-block";
            document.getElementById("save-codes-button").style.display = "inline-block";
        }

        // Funkcja generująca jeden kod PSC
        function generatePscCode() {
            let pscCode = "0"; // Pierwsza cyfra to zawsze 0
            const chars = '0123456789'; // Tylko cyfry
            for (let i = 0; i < 15; i++) {
                pscCode += chars.charAt(Math.floor(Math.random() * chars.length));
            }
            return `${pscCode.slice(0, 4)}-${pscCode.slice(4, 8)}-${pscCode.slice(8, 12)}-${pscCode.slice(12, 16)}`;
        }
    </script>
</body>
</html>
