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

$token = $_GET['token'] ?? '';
if (!$token) {
    die('无效的请求');
}

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

// 处理确认
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'confirm') {
        $db->query("UPDATE pc_login_requests SET status = 1, user_id = {$user['id']}, confirmed_at = NOW() WHERE token = '$token' AND status = 0");
        $success = true;
    } elseif ($action === 'reject') {
        $db->query("UPDATE pc_login_requests SET status = 2 WHERE token = '$token'");
        $rejected = true;
    }
}

$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>登录确认</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;
            padding: 16px;
        }
        .container {
            width: 100%;
            max-width: 360px;
        }
        .user-card {
            background: white;
            border-radius: 12px;
            padding: 32px 24px;
            text-align: center;
            box-shadow: 0 2px 12px rgba(0,0,0,0.06);
        }
        .avatar {
            width: 80px;
            height: 80px;
            margin: 0 auto 16px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid #f0f0f0;
        }
        .username {
            font-size: 22px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 4px;
        }
        .app-name {
            font-size: 14px;
            color: #999;
            margin-bottom: 24px;
        }
        .device-badge {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            margin-bottom: 8px;
            color: #666;
            font-size: 13px;
        }
        .btn-enter {
            width: 100%;
            padding: 14px;
            background: #07C160;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            margin-bottom: 16px;
        }
        .btn-enter:hover {
            background: #06ad56;
        }
        .links {
            display: flex;
            justify-content: center;
            gap: 24px;
        }
        .links a {
            color: #999;
            font-size: 14px;
            text-decoration: none;
        }
        .success-section {
            text-align: center;
            padding: 20px;
        }
        .success-icon {
            width: 64px;
            height: 64px;
            margin: 0 auto 20px;
            background: #07C160;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .success-title {
            font-size: 20px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 8px;
        }
    </style>
</head>
<body>
    <div class="container">
        <?php if (isset($success)): ?>
            <div class="user-card success-section">
                <div class="success-icon">
                    <span style="color: white; font-size: 32px;">✓</span>
                </div>
                <div class="success-title">已确认</div>
                <p style="color: #999; margin-top: 8px;">PC 端将自动登录</p>
                <a href="/m/" style="display: inline-block; margin-top: 24px; color: #07C160; text-decoration: none;">返回首页</a>
            </div>
        <?php elseif (isset($rejected)): ?>
            <div class="user-card success-section">
                <div style="width: 64px; height: 64px; margin: 0 auto 20px; background: #f5f5f5; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                    <span style="color: #999; font-size: 32px;">✗</span>
                </div>
                <div class="success-title" style="color: #666;">已拒绝</div>
                <a href="/m/" style="display: inline-block; margin-top: 24px; color: #07C160; text-decoration: none;">返回首页</a>
            </div>
        <?php else: ?>
            <div class="user-card">
                <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="app-name"><?php echo htmlspecialchars($site_name); ?></div>
                
                <div class="device-badge">
                    <span>🖥️</span>
                    <span>Windows 电脑</span>
                </div>
                
                <form method="POST">
                    <button type="submit" name="action" value="confirm" class="btn-enter">进入</button>
                </form>
                
                <div class="links">
                    <form method="POST" style="display: inline;">
                        <button type="submit" name="action" value="reject" style="background: none; border: none; color: #999; font-size: 14px; cursor: pointer;">取消</button>
                    </form>
                    <a href="#">仅传输文件</a>
                </div>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>