<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ativar Key | CoffeeHub</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.css">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            margin-top: 100px;
            padding: 30px;
        }
        .modal-content {
            border-radius: 10px;
        }
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: none;
        }
    </style>
</head>
<body>
<div class="container text-center">
    <h1>Key de <%= expire %></h1>
    <p>Ative sua key CoffeeHub <%= expire %>, e aproveite todos os recursos do CoffeeHub por completo.</p>
    <button id="activateKeyBtn" class="btn btn-success"><b>ATIVAR KEY AGORA</b></button>
</div>

<div class="overlay" id="overlay"></div>

<div class="modal" id="resultModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle"></h5>
            </div>
            <div class="modal-body">
                <p id="modalMessage"></p>
                <input type="text" id="generatedKey" class="form-control" readonly style="display:none;">
                <button id="copyKeyBtn" class="btn btn-primary mt-2" style="display:none;">
                    <i class="fas fa-copy"></i> Copiar
                </button>
                <a href="https://coffeehub.moonhorizon.xyz" class="btn btn-secondary mt-2" id="supportLink" style="display:none;">Ir para CoffeeHub</a>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/js/all.min.js"></script>

<script>
    $(document).ready(function () {
        const expireValue = "<%= expire %>";
        $("h1, p").find("span").text(expireValue);

        const urlPath = window.location.pathname;
        const key = urlPath.split("/key/")[1];

        $("#activateKeyBtn").click(function () {
            const token = generateToken();

            $.ajax({
                url: "https://ativar.moonhorizon.xyz/active/key",
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify({ key: key, token: token }),
                success: function (response) {
                    showResult(response.generatedKey);
                },
                error: function () {
                    showError();
                }
            });
        });

        function generateToken() {
            const timestamp = Date.now();
            let token = timestamp.toString();
            token = token.replace(/1/g, 'a').replace(/3/g, 'e').replace(/4/g, 'c')
                .replace(/5/g, 'd').replace(/8/g, 'f').replace(/9/g, 'g');
            return token.split('').reverse().join('');
        }

        function showResult(generatedKey) {
            $("#modalTitle").text("Key Ativada");
            $("#modalMessage").html("Sua Key foi ativada, copie e a guarde. Por segurança, este link de ativação será revogado e sua chave só poderá ser copiada agora, guarde-a com segurança em algum lugar, não será possível recuperar depois.<br><br><a href='https://coffeehub.moonhorizon.xyz/download'>Baixar CoffeeHub</a>");
            $("#generatedKey").val(generatedKey).show();
            $("#copyKeyBtn").show();
            $("#supportLink").hide();
            $("#overlay").show();
            $("#resultModal").modal('show');
        }

        function showError() {
            $("#modalTitle").text("Erro ao gerar");
            $("#modalMessage").text("Não foi possível ativar a chave, pois ocorreu um erro. Entre em contato com o suporte do CoffeeHub.");
            $("#supportLink").show();
            $("#generatedKey").val("").hide();
            $("#copyKeyBtn").hide();
            $("#overlay").show();
            $("#resultModal").modal('show');
        }

        $("#copyKeyBtn").click(function () {
            const keyField = $("#generatedKey");
            keyField.select();
            document.execCommand("copy");
        });
    });
</script>
</body>
</html>