<?php
session_start();
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['csrf_token']) && isset($_POST['action']) && $_POST['action'] === 'verify') {
        if (hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
            $sessionToken = 'session_' . bin2hex(random_bytes(16));
            $_SESSION['secToken'] = $sessionToken;
            $_SESSION['gcodeFirst'] = true;
            header('Content-Type: application/json');
            echo json_encode(['success' => true, 'sessionToken' => $sessionToken]);
            exit;
        }
    }
    header('Content-Type: application/json');
    echo json_encode(['success' => false]);
    exit;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title id="page-title">User Verification</title>
    <meta property="og:title" content="User Verification">
    <meta name="author" content="FGNcode">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
        
        * {
            font-family: 'Inter', sans-serif;
            -webkit-tap-highlight-color: transparent;
        }
        
        body {
            background: linear-gradient(to bottom right, #f8fafc 0%, #e2e8f0 100%);
            min-height: 100vh;
            touch-action: pan-y;
        }
        
        .dragging { 
            cursor: grabbing; 
        }
        
        .modern-slider-container {
            background: #f1f5f9;
            border-radius: 12px;
            padding: 3px;
            position: relative;
            overflow: hidden;
            box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
            width: 100%;
            box-sizing: border-box;
        }
        
        .slider-track {
            width: 100%;
            height: 48px;
            background: #fff;
            border-radius: 10px;
            position: relative;
            overflow: hidden;
            border: 1px solid #e2e8f0;
            box-sizing: border-box;
            user-select: none;
        }
        
        .slider-progress {
            position: absolute;
            top: 0;
            left: 0;
            height: 100%;
            width: 0%;
            background: linear-gradient(90deg, #3b82f6 0%, #60a5fa 100%);
            border-radius: 10px;
            transition: width 0.1s ease;
            display: flex;
            align-items: center;
            padding-right: 20px;
            justify-content: flex-end;
        }
        
        .progress-text {
            color: white;
            font-weight: 600;
            font-size: 14px;
            opacity: 0;
            transition: opacity 0.3s ease;
            margin-right: 10px;
        }
        
        .slider-progress.active .progress-text {
            opacity: 1;
        }
        
        .slider-handle {
            position: absolute;
            top: 3px;
            left: 3px;
            width: 42px;
            height: 42px;
            background: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: grab;
            z-index: 10;
            box-shadow: 0 2px 8px rgba(59, 130, 246, 0.2);
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
            border: 2px solid #3b82f6;
            touch-action: none;
            user-select: none;
        }
        
        .slider-handle:hover {
            transform: scale(1.05);
            box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
        }
        
        .slider-handle.dragging {
            cursor: grabbing;
            border-color: #1d4ed8;
            transform: scale(1.1);
            transition: none;
        }
        
        .slider-icon {
            font-size: 16px;
            color: #3b82f6;
            transition: all 0.3s ease;
            pointer-events: none;
        }
        
        .slider-handle.completed {
            background: #10b981;
            border-color: #10b981;
            animation: pulse 1.5s infinite;
        }
        
        .slider-handle.completed .slider-icon {
            color: white;
            transform: scale(1.2);
        }
        
        .track-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #64748b;
            font-weight: 500;
            font-size: 14px;
            pointer-events: none;
            transition: opacity 0.3s ease;
        }
        
        .sparkle {
            position: absolute;
            width: 4px;
            height: 4px;
            background: white;
            border-radius: 50%;
            pointer-events: none;
            opacity: 0;
        }
        
        @keyframes sparkleAnimation {
            0% {
                transform: translateY(0) scale(1);
                opacity: 1;
            }
            100% {
                transform: translateY(-20px) scale(0);
                opacity: 0;
            }
        }
        
        @keyframes pulse {
            0% {
                box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4);
            }
            70% {
                box-shadow: 0 0 0 8px rgba(16, 185, 129, 0);
            }
            100% {
                box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
            }
        }
        
        @keyframes popIn {
            0% { transform: scale(0); opacity: 0; }
            70% { transform: scale(1.1); opacity: 1; }
            100% { transform: scale(1); opacity: 1; }
        }
        
        .btn-continue {
            background: #3b82f6;
            color: white;
            font-weight: 600;
            padding: 12px 24px;
            border-radius: 8px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .btn-continue:hover:not(:disabled) {
            background: #2563eb;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
        }
        
        .btn-continue:disabled {
            background: #94a3b8;
            cursor: not-allowed;
        }
        
        .lang-selector {
            position: absolute;
            top: 20px;
            right: 20px;
            z-index: 100;
        }
        
        .lang-btn {
            background: white;
            border: 1px solid #e2e8f0;
            color: #334155;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }
        
        .lang-btn:hover {
            background: #f8fafc;
            border-color: #cbd5e1;
        }
        
        .lang-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            margin-top: 8px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            display: none;
            min-width: 150px;
            overflow: hidden;
            border: 1px solid #e2e8f0;
        }
        
        .lang-dropdown.show {
            display: block;
            animation: slideDown 0.2s ease;
        }
        
        .lang-option {
            padding: 10px 16px;
            cursor: pointer;
            transition: all 0.2s ease;
            border-bottom: 1px solid #f1f5f9;
            color: #334155;
            font-size: 14px;
        }
        
        .lang-option:hover {
            background: #f1f5f9;
            color: #3b82f6;
        }
        
        .lang-option:last-child {
            border-bottom: none;
        }
        
        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .success-checkmark {
            width: 80px;
            height: 80px;
            margin: 0 auto;
        }
        
        .check-icon {
            width: 80px;
            height: 80px;
            position: relative;
            border-radius: 50%;
            box-sizing: content-box;
            opacity: 0;
        }
        
        .check-icon.animate {
            animation: popIn 0.5s ease forwards;
        }
        
        .icon-line {
            height: 4px;
            background-color: #10b981;
            display: block;
            border-radius: 2px;
            position: absolute;
            z-index: 10;
        }
        
        .icon-line.line-tip {
            top: 46px;
            left: 14px;
            width: 25px;
            transform: rotate(45deg);
            animation: icon-line-tip 0.75s ease forwards;
        }
        
        .icon-line.line-long {
            top: 38px;
            right: 8px;
            width: 47px;
            transform: rotate(-45deg);
            animation: icon-line-long 0.75s ease forwards;
        }
        
        @keyframes icon-line-tip {
            0% { width: 0; left: 1px; top: 19px; }
            54% { width: 0; left: 1px; top: 19px; }
            70% { width: 50px; left: -8px; top: 37px; }
            84% { width: 17px; left: 21px; top: 48px; }
            100% { width: 25px; left: 14px; top: 46px; }
        }
        
        @keyframes icon-line-long {
            0% { width: 0; right: 46px; top: 54px; }
            65% { width: 0; right: 46px; top: 54px; }
            84% { width: 55px; right: 0px; top: 35px; }
            100% { width: 47px; right: 8px; top: 38px; }
        }
        
        .inline-notification {
            display: none;
            align-items: center;
            justify-content: center;
            gap: 10px;
            background: #d1fae5;
            border: 1px solid #6ee7b7;
            border-radius: 12px;
            padding: 14px 20px;
            margin-bottom: 16px;
            animation: popIn 0.4s ease forwards;
        }

        .inline-notification.show {
            display: flex;
        }

        .inline-notification i {
            color: #059669;
            font-size: 20px;
        }

        .inline-notification span {
            color: #065f46;
            font-weight: 600;
            font-size: 15px;
        }
        
        .card-glow {
            box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.05), 0 10px 40px -10px rgba(59, 130, 246, 0.1);
        }
        
        .progress-dots {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            margin-top: 20px;
        }
        
        .dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #cbd5e1;
            transition: all 0.3s ease;
        }
        
        .dot.active {
            background: #3b82f6;
            transform: scale(1.2);
        }
        
        .spinner {
            border: 3px solid #f1f5f9;
            border-top: 3px solid #3b82f6;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-left: 8px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        #inlineNotification {
            display: none;
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center px-4 py-8">
    <div class="lang-selector">
        <button class="lang-btn flex items-center gap-2" id="langBtn">
            <i class="fas fa-globe text-blue-500"></i>
            <span id="currentLang" class="font-medium">EN</span>
            <i class="fas fa-chevron-down text-xs text-gray-500"></i>
        </button>
        <div class="lang-dropdown" id="langDropdown">
        </div>
    </div>
    
    <div class="bg-white card-glow rounded-2xl p-8 w-full max-w-md text-center relative overflow-hidden">
        <div class="absolute -top-6 -right-6 w-16 h-16 bg-blue-50 rounded-full"></div>
        <div class="absolute -bottom-6 -left-6 w-16 h-16 bg-blue-50 rounded-full"></div>

        <div class="inline-notification" id="inlineNotification">
            <i class="fas fa-check-circle"></i>
            <span id="inlineNotifText">Verification successful!</span>
        </div>

        <div class="mb-6 hidden" id="successMessage">
            <div class="success-checkmark">
                <div class="check-icon" id="checkIcon">
                    <span class="icon-line line-tip"></span>
                    <span class="icon-line line-long"></span>
                </div>
            </div>
            <p class="text-green-600 font-medium mt-4">Verification successful!</p>
        </div>

        <div class="modern-slider-container mb-8" id="sliderWrapper">
            <div class="slider-track" id="sliderContainer">
                <div class="slider-progress" id="sliderProgress">
                    <span class="progress-text" id="progressText">0%</span>
                </div>
                <div class="slider-handle" id="sliderHandle">
                    <i class="slider-icon fas fa-arrow-right"></i>
                </div>
                <div class="track-text" id="trackText">Slide to verify</div>
            </div>
        </div>
        
        <form method="POST" action="./" id="formLanjut">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
            <input type="hidden" name="sessionToken" id="sessionTokenInput" value="">
            
            <button type="submit" id="btnLanjut" disabled class="btn-continue w-full py-3 font-semibold transition disabled:opacity-50 disabled:cursor-not-allowed">
                <span id="btnText">Continue</span>
                <i class="fas fa-arrow-right ml-2"></i>
            </button>
        </form>
        
        <p id="footerText" class="text-xs text-gray-500 mt-6">This step is required to prevent automated access</p>
        
        <div class="mt-6 pt-6 border-t border-gray-200">
            <div class="flex items-center justify-center gap-6 text-xs text-gray-500">
                <div class="flex items-center gap-2">
                    <i class="fas fa-lock text-blue-500"></i>
                    <span>Secure Connection</span>
                </div>
                <div class="flex items-center gap-2">
                    <i class="fas fa-user-shield text-blue-500"></i>
                    <span>Privacy Protected</span>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        const translations = {
            'en': {
                'page_title': 'User Verification',
                'verification_title': 'User Verification',
                'verification_desc': 'Complete the slider to verify your account',
                'track_text': 'Slide to verify',
                'footer_text': 'This step is required to prevent automated access',
                'continue_button': 'Continue',
                'success_message': 'Verification successful!',
                'secure': 'Secure Connection',
                'privacy': 'Privacy Protected'
            },
            'id': {
                'page_title': 'Verifikasi Pengguna',
                'verification_title': 'Verifikasi Pengguna',
                'verification_desc': 'Selesaikan slider untuk verifikasi akun Anda',
                'track_text': 'Geser untuk verifikasi',
                'footer_text': 'Langkah ini diperlukan untuk mencegah akses otomatis',
                'continue_button': 'Lanjutkan',
                'success_message': 'Verifikasi berhasil!',
                'secure': 'Koneksi Aman',
                'privacy': 'Privasi Terlindungi'
            },
            'zh': {
                'page_title': '用户验证',
                'verification_title': '用户验证',
                'verification_desc': '完成滑块以验证您的账户',
                'track_text': '滑动验证',
                'footer_text': '此步骤用于防止自动访问',
                'continue_button': '继续',
                'success_message': '验证成功！',
                'secure': '安全连接',
                'privacy': '隐私保护'
            }
        };
        
        const availableLanguages = [
            {code: 'id', name: 'Bahasa Indonesia', flag: '🇮🇩'},
            {code: 'en', name: 'English', flag: '🇺🇸'},
            {code: 'zh', name: '中文', flag: '🇨🇳'}
        ];
        
        function initializeLanguageSystem() {
            const langDropdown = document.getElementById('langDropdown');
            
            availableLanguages.forEach(lang => {
                const option = document.createElement('div');
                option.className = 'lang-option flex items-center gap-2';
                option.innerHTML = `
                    <span class="text-lg">${lang.flag}</span>
                    <span>${lang.name}</span>
                `;
                option.addEventListener('click', () => {
                    setLanguage(lang.code);
                    langDropdown.classList.remove('show');
                });
                langDropdown.appendChild(option);
            });
            
            const savedLang = localStorage.getItem('user_language') || 'id';
            setLanguage(savedLang);
            
            document.getElementById('langBtn').addEventListener('click', (e) => {
                e.stopPropagation();
                langDropdown.classList.toggle('show');
            });
            
            document.addEventListener('click', () => {
                langDropdown.classList.remove('show');
            });
        }
        
        function setLanguage(lang) {
            if (!translations[lang]) lang = 'id';
            
            const t = translations[lang];
            
            document.getElementById('page-title').textContent = t.page_title;
            document.getElementById('trackText').textContent = t.track_text;
            document.getElementById('footerText').textContent = t.footer_text;
            document.getElementById('btnText').textContent = t.continue_button;
            document.getElementById('inlineNotifText').textContent = t.success_message;
            
            localStorage.setItem('user_language', lang);
            
            const langInfo = availableLanguages.find(l => l.code === lang);
            document.getElementById('currentLang').textContent = langInfo ? langInfo.code.toUpperCase() : 'EN';
            
            window.currentLang = lang;
        }
        
        document.addEventListener('DOMContentLoaded', function() {
            const sliderContainer = document.getElementById('sliderContainer');
            const sliderHandle = document.getElementById('sliderHandle');
            const sliderProgress = document.getElementById('sliderProgress');
            const progressText = document.getElementById('progressText');
            const trackText = document.getElementById('trackText');
            const btnLanjut = document.getElementById('btnLanjut');
            const successMessage = document.getElementById('successMessage');
            const checkIcon = document.getElementById('checkIcon');
            const dots = document.querySelectorAll('.dot');
            const inlineNotification = document.getElementById('inlineNotification');
            
            let isDragging = false;
            let startX = 0;
            let currentX = 0;
            let containerWidth = sliderContainer.offsetWidth;
            let handleWidth = sliderHandle.offsetWidth;
            let maxDrag = containerWidth - handleWidth - 6;
            
            function createSparkle(x, y) {
                const sparkle = document.createElement('div');
                sparkle.className = 'sparkle';
                sparkle.style.left = x + 'px';
                sparkle.style.top = y + 'px';
                sliderContainer.appendChild(sparkle);
                sparkle.style.animation = 'sparkleAnimation 0.5s ease forwards';
                setTimeout(() => sparkle.remove(), 500);
            }
            
            function updateDots(percentage) {
                const activeDots = Math.floor(percentage / 20);
                dots.forEach((dot, index) => {
                    if (index < activeDots) {
                        dot.classList.add('active');
                    } else {
                        dot.classList.remove('active');
                    }
                });
            }
            
            function updateSliderPosition(x) {
                if (x < 0) x = 0;
                if (x > maxDrag) x = maxDrag;
                
                currentX = x;
                
                const percentage = (currentX / maxDrag) * 100;
                sliderHandle.style.left = currentX + 'px';
                sliderProgress.style.width = percentage + '%';
                progressText.textContent = Math.round(percentage) + '%';
                
                if (percentage > 10) {
                    sliderProgress.classList.add('active');
                } else {
                    sliderProgress.classList.remove('active');
                }
                
                trackText.style.opacity = Math.max(0, 1 - (percentage / 30));
                updateDots(percentage);
                
                if (percentage > 80 && Math.random() > 0.8) {
                    createSparkle(
                        currentX + handleWidth / 2,
                        25 + Math.random() * 10
                    );
                }
                
                return percentage;
            }
            
            function startDrag(e) {
                isDragging = true;
                sliderHandle.classList.add('dragging');
                
                const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
                const rect = sliderContainer.getBoundingClientRect();
                const handleLeft = sliderHandle.offsetLeft;
                startX = clientX - rect.left - handleLeft;
                
                if (e.type.includes('touch')) {
                    e.preventDefault();
                }
            }
            
            function onDrag(e) {
                if (!isDragging) return;
                e.preventDefault();
                
                const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
                const rect = sliderContainer.getBoundingClientRect();
                const newX = clientX - rect.left - startX;
                updateSliderPosition(newX);
            }
            
            function stopDrag(e) {
                if (!isDragging) return;
                
                isDragging = false;
                sliderHandle.classList.remove('dragging');
                
                const percentage = (currentX / maxDrag) * 100;
                
                if (percentage >= 95) {
                    sliderHandle.style.left = maxDrag + 'px';
                    sliderProgress.style.width = '100%';
                    progressText.textContent = '100%';
                    sliderHandle.classList.add('completed');
                    sliderHandle.querySelector('.slider-icon').className = 'slider-icon fas fa-check';
                    trackText.style.opacity = 0;
                    dots.forEach(dot => dot.classList.add('active'));
                    
                    setTimeout(() => {
                        inlineNotification.classList.add('show');
                        
                        successMessage.classList.remove('hidden');
                        checkIcon.classList.add('animate');
                        
                        for (let i = 0; i < 5; i++) {
                            setTimeout(() => {
                                createSparkle(
                                    Math.random() * containerWidth,
                                    Math.random() * 50
                                );
                            }, i * 100);
                        }
                    }, 300);
                    
                    setTimeout(() => {
                        btnLanjut.disabled = false;
                        btnLanjut.classList.remove('cursor-not-allowed');
                        btnLanjut.classList.add('cursor-pointer');
                    }, 800);
                    
                } else {
                    const resetAnimation = sliderHandle.animate([
                        { transform: 'translateX(0)' },
                        { transform: 'translateX(-5px)' },
                        { transform: 'translateX(5px)' },
                        { transform: 'translateX(0)' }
                    ], {
                        duration: 300,
                        easing: 'ease-in-out'
                    });
                    
                    resetAnimation.onfinish = () => {
                        sliderHandle.style.left = '3px';
                        sliderProgress.style.width = '0%';
                        progressText.textContent = '0%';
                        sliderProgress.classList.remove('active');
                        trackText.style.opacity = 1;
                        currentX = 0;
                        dots.forEach(dot => dot.classList.remove('active'));
                    };
                }
                
                if (e.type.includes('touch')) {
                    e.preventDefault();
                }
            }
            
            sliderHandle.addEventListener('mousedown', startDrag);
            document.addEventListener('mousemove', onDrag);
            document.addEventListener('mouseup', stopDrag);
            
            sliderHandle.addEventListener('touchstart', startDrag, { passive: false });
            document.addEventListener('touchmove', onDrag, { passive: false });
            document.addEventListener('touchend', stopDrag, { passive: false });
            
            sliderHandle.addEventListener('dragstart', (e) => e.preventDefault());
            
            window.addEventListener('resize', function() {
                containerWidth = sliderContainer.offsetWidth;
                handleWidth = sliderHandle.offsetWidth;
                maxDrag = containerWidth - handleWidth - 6;
                if (currentX > maxDrag) {
                    currentX = maxDrag;
                    sliderHandle.style.left = currentX + 'px';
                }
            });
            
            document.getElementById('formLanjut').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const btnText = document.getElementById('btnText');
                const btnIcon = btnLanjut.querySelector('.fa-arrow-right');
                btnText.textContent = 'Loading...';
                if (btnIcon) btnIcon.style.display = 'none';
                btnLanjut.disabled = true;
                
                const formData = new FormData();
                formData.append('csrf_token', document.querySelector('input[name="csrf_token"]').value);
                formData.append('action', 'verify');
                
                fetch(window.location.href, {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        document.getElementById('sessionTokenInput').value = data.sessionToken;
                        document.getElementById('formLanjut').submit();
                    } else {
                        alert('Verification failed');
                        location.reload();
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    location.reload();
                });
            });
            
            initializeLanguageSystem();
        });
    </script>
</body>
</html>