<?php
ob_start();
require_once '../config.php';

$token = $_GET['token'] ?? '';
if (!$token) {
    die('无效的二维码');
}

$qrcode = $db->fetchOne("SELECT * FROM login_qrcodes WHERE token = '" . $db->escape($token) . "'");
if (!$qrcode) {
    die('二维码无效或已过期');
}

if (strtotime($qrcode['expire_at']) < time()) {
    die('二维码已过期');
}

if (!isLoggedIn()) {
    $_SESSION['redirect_after_login'] = '/m/qrcode_confirm.php?token=' . urlencode($token);
    header('Location: login.php');
    exit;
}

$user = currentUser();
$success = false;
$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'confirm') {
        $db->query("UPDATE login_qrcodes SET status = 2, confirm_user_id = {$user['id']} WHERE id = " . $qrcode['id']);
        $success = true;
        $message = '登录确认成功';
    } elseif ($action === 'cancel') {
        $db->query("UPDATE login_qrcodes SET status = 0, scan_user_id = 0 WHERE id = " . $qrcode['id']);
        $message = '已取消登录';
    }
} else {
    if ($qrcode['status'] == 0) {
        $db->query("UPDATE login_qrcodes SET status = 1, scan_user_id = {$user['id']} WHERE id = " . $qrcode['id']);
    }
}

$site_name = getSiteConfig('site_name') ?? '麟泓授权';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <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: #ffffff;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            text-align: center;
            max-width: 340px;
            width: 100%;
        }
        .avatar {
            width: 100px;
            height: 100px;
            margin: 0 auto 20px;
            border-radius: 50%;
            object-fit: cover;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        .username {
            font-size: 24px;
            font-weight: 600;
            color: #1f2937;
            margin-bottom: 8px;
        }
        .tip {
            color: #6b7280;
            font-size: 15px;
            margin-bottom: 30px;
        }
        .btn-group {
            display: flex;
            gap: 12px;
        }
        .btn {
            flex: 1;
            padding: 14px 0;
            border-radius: 30px;
            font-size: 16px;
            font-weight: 600;
            border: none;
            cursor: pointer;
            transition: all 0.2s;
        }
        .btn-confirm {
            background: #07C160;
            color: white;
        }
        .btn-cancel {
            background: #f5f5f5;
            color: #666;
        }
        .result-box {
            text-align: center;
        }
        .result-icon {
            width: 80px;
            height: 80px;
            margin: 0 auto 20px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .result-icon.success {
            background: #d1fae5;
        }
        .result-icon.error {
            background: #fee2e2;
        }
        .result-title {
            font-size: 22px;
            font-weight: 600;
            color: #1f2937;
            margin-bottom: 8px;
        }
        .result-desc {
            color: #6b7280;
            font-size: 14px;
            margin-bottom: 20px;
        }
        .countdown {
            color: #07C160;
            font-size: 14px;
            margin-top: 20px;
        }
        .home-link {
            display: inline-block;
            margin-top: 20px;
            color: #07C160;
            text-decoration: none;
            font-size: 15px;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <div class="container">
        <?php if ($success): ?>
            <div class="result-box">
                <div class="result-icon success">
                    <img src="https://api.iconify.design/mdi:check.svg?color=%2310b981" width="48" height="48" alt="">
                </div>
                <div class="result-title">登录成功</div>
                <div class="result-desc">电脑端将自动跳转</div>
                <div class="countdown" id="countdown">3 秒后返回首页</div>
                <a href="/m/" class="home-link">立即返回</a>
            </div>
            <script>
                let seconds = 3;
                const countdownEl = document.getElementById('countdown');
                const timer = setInterval(() => {
                    seconds--;
                    if (seconds <= 0) {
                        clearInterval(timer);
                        window.location.href = '/m/';
                    } else {
                        countdownEl.textContent = seconds + ' 秒后返回首页';
                    }
                }, 1000);
            </script>
        <?php elseif ($message): ?>
            <div class="result-box">
                <div class="result-icon error">
                    <img src="https://api.iconify.design/mdi:close.svg?color=%23ef4444" width="48" height="48" alt="">
                </div>
                <div class="result-title"><?php echo htmlspecialchars($message); ?></div>
                <a href="/m/" class="home-link">返回首页</a>
            </div>
        <?php else: ?>
            <img src="<?php echo htmlspecialchars($user['avatar'] ?? '/assets/uploads/default_avatar.png'); ?>" 
                 class="avatar" 
                 onerror="this.src='/assets/uploads/default_avatar.png'">
            <div class="username"><?php echo htmlspecialchars($user['username']); ?></div>
            <div class="tip">确认使用此账号登录？</div>
            
            <form method="POST">
                <div class="btn-group">
                    <button type="submit" name="action" value="confirm" class="btn btn-confirm">确认</button>
                    <button type="submit" name="action" value="cancel" class="btn btn-cancel">取消</button>
                </div>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>