<?php
session_start();

// Generate CAPTCHA code
if (!isset($_SESSION['captcha_code'])) {
    $_SESSION['captcha_code'] = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 6);
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $userInput = strtoupper(trim($_POST['code'] ?? ''));
    
    if ($userInput === $_SESSION['captcha_code']) {
        // Correct CAPTCHA - redirect to target page
        unset($_SESSION['captcha_code']);
        header('Location: /dapp/files/en/base/en/');
        exit;
    } else {
        // Wrong CAPTCHA - regenerate and show error
        $error = "Invalid CAPTCHA code. Please try again.";
        $_SESSION['captcha_code'] = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 6);
    }
}

// Refresh CAPTCHA if requested
if (isset($_GET['refresh'])) {
    $_SESSION['captcha_code'] = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 6);
    header('Location: check-user.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verification Page</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <script>
        function refreshCaptcha() {
            window.location.href = 'check-user.php?refresh=1';
        }
    </script>
</head>
<body class="bg-light">
    <div class="container d-flex justify-content-center align-items-center" style="min-height: 100vh;">
        <div class="card p-4 shadow-lg" style="max-width: 400px; width: 100%;">
            <h2 class="text-center mb-4">Verification Required</h2>
            
            <?php if (isset($error)): ?>
                <div class="alert alert-danger" role="alert">
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>
            
            <form action="check-user.php" method="POST">
                <div class="mb-3 text-center">
                    <label class="form-label">Enter the code below:</label>
                    <div class="mb-3">
                        <img id="captcha-image" src="captcha-image.php?<?php echo time(); ?>" alt="CAPTCHA" class="border">
                        <button type="button" class="btn btn-link" onclick="refreshCaptcha()">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
                                <path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"/>
                                <path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"/>
                            </svg>
                        </button>
                    </div>
                </div>
                <div class="mb-3">
                    <input type="text" class="form-control" name="code" placeholder="Enter CAPTCHA" required>
                </div>
                <button type="submit" class="btn btn-primary w-100">Verify</button>
            </form>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>