<?php
ob_start();
header('Content-Type: application/json');
require_once '../config.php';

$user = currentUser();
if (!$user) {
    echo json_encode(['code' => 1, 'has_pending' => false]);
    exit;
}

// 查找该用户的待确认请求
$request = $db->fetchOne("
    SELECT * FROM pc_login_requests 
    WHERE user_id = {$user['id']}
    AND status = 0 
    AND expire_at > NOW()
    ORDER BY id DESC LIMIT 1
");

if ($request) {
    echo json_encode([
        'code' => 0,
        'has_pending' => true,
        'token' => $request['token'],
        'device_name' => $request['device_name']
    ]);
} else {
    echo json_encode(['code' => 0, 'has_pending' => false]);
}
exit;