<?php
require '../includes/admin-check.php';

$stmt = $pdo->query("
    SELECT login_sessions.*, users.email 
    FROM login_sessions 
    JOIN users ON login_sessions.user_id = users.id
    ORDER BY login_time DESC
");
$sessions = $stmt->fetchAll();
?>

<h2>Login Sessions Monitor</h2>

<table border="1" cellpadding="8">
<tr>
    <th>User</th>
    <th>IP</th>
    <th>Device</th>
    <th>Login Time</th>
    <th>Logout Time</th>
    <th>Status</th>
    <th>Action</th>
</tr>

<?php foreach($sessions as $s): ?>
<tr>
    <td><?= htmlspecialchars($s['email']) ?></td>
    <td><?= $s['ip_address'] ?></td>
    <td style="max-width:250px;"><?= substr($s['user_agent'],0,80) ?>...</td>
    <td><?= $s['login_time'] ?></td>
    <td><?= $s['logout_time'] ?? '—' ?></td>
    <td>
        <?= $s['is_active'] ? "<span style='color:green'>Active</span>" : "Closed" ?>
    </td>
    <td>
        <?php if($s['is_active']): ?>
            <a href="force-logout.php?sid=<?= $s['session_id'] ?>">Force Logout</a>
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
</table>
