<?php
ob_start();
require_once '../../config.php';
require_once '../../includes/functions.php';
$site_name = getSiteConfig('site_name') ?? '麟泓授权';
$site_logo = getSiteConfig('site_logo') ?? '/assets/uploads/logo.png';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录确认 - <?php echo htmlspecialchars($site_name); ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .confirm-card {
            background: white;
            border-radius: 8px;
            width: 360px;
            padding: 48px 32px;
            text-align: center;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
        }
        .app-icon {
            width: 80px;
            height: 80px;
            margin: 0 auto 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .app-icon img {
            width: 44px;
            height: 44px;
            filter: brightness(0) invert(1);
        }
        .title {
            font-size: 20px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 8px;
        }
        .subtitle {
            font-size: 14px;
            color: #999;
            margin-bottom: 32px;
        }
        .status-icon {
            width: 60px;
            height: 60px;
            margin: 0 auto 20px;
        }
        .status-text {
            font-size: 16px;
            color: #666;
            margin-bottom: 24px;
        }
        .loading-dots {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin: 20px 0;
        }
        .dot {
            width: 8px;
            height: 8px;
            background: #07C160;
            border-radius: 50%;
            animation: pulse 1.5s infinite;
        }
        .dot:nth-child(2) {
            animation-delay: 0.2s;
        }
        .dot:nth-child(3) {
            animation-delay: 0.4s;
        }
        @keyframes pulse {
            0%, 60%, 100% {
                opacity: 0.3;
                transform: scale(1);
            }
            30% {
                opacity: 1;
                transform: scale(1.2);
            }
        }
        .btn-cancel {
            margin-top: 24px;
            padding: 10px 32px;
            background: none;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            color: #666;
            font-size: 14px;
            cursor: pointer;
        }
        .btn-cancel:hover {
            background: #f5f5f5;
        }
    </style>
</head>
<body>
    <div class="confirm-card">
        <div class="app-icon">
            <img src="<?php echo htmlspecialchars($site_logo); ?>" alt="" onerror="this.src='/assets/uploads/logo.png'">
        </div>
        <div class="title">登录确认</div>
        <div class="subtitle">请在手机上确认登录</div>
        
        <div class="loading-dots">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>
        
        <div class="status-text" id="statusText">等待确认...</div>
        
        <button class="btn-cancel" onclick="cancelLogin()">取消</button>
    </div>
    
    <script>
        const token = '<?php echo $_GET["token"] ?? ""; ?>';
        let pollingTimer = null;
        let count = 0;
        
        function startPolling() {
            pollingTimer = setInterval(async () => {
                count++;
                
                try {
                    const res = await fetch('/api/pc_login_status.php?token=' + token);
                    const data = await res.json();
                    
                    if (data.status === 'confirmed') {
                        clearInterval(pollingTimer);
                        document.getElementById('statusText').textContent = '登录成功，正在跳转...';
                        setTimeout(() => {
                            window.location.href = data.redirect || '/admin.php';
                        }, 500);
                    } else if (data.status === 'rejected') {
                        clearInterval(pollingTimer);
                        document.getElementById('statusText').textContent = '登录已拒绝';
                        document.querySelector('.loading-dots').style.display = 'none';
                    } else if (data.status === 'expired') {
                        clearInterval(pollingTimer);
                        document.getElementById('statusText').textContent = '请求已过期，请重新登录';
                        document.querySelector('.loading-dots').style.display = 'none';
                    }
                    
                    if (count > 150) {
                        clearInterval(pollingTimer);
                        document.getElementById('statusText').textContent = '请求已过期，请重新登录';
                        document.querySelector('.loading-dots').style.display = 'none';
                    }
                } catch (e) {
                    console.error('轮询错误:', e);
                }
            }, 2000);
        }
        
        function cancelLogin() {
            if (pollingTimer) clearInterval(pollingTimer);
            
            fetch('/api/pc_login_cancel.php?token=' + token)
                .then(() => {
                    window.location.href = '/modules/home/login.php';
                });
        }
        
        startPolling();
    </script>
</body>
</html>