<?php
require_once __DIR__ . '/../includes/config.php';
$me = require_login('internal');

$pdo = db();
$q       = trim((string) input('q'));
$status  = trim((string) input('status'));
$from    = trim((string) input('from'));
$to      = trim((string) input('to'));

$where = '1=1'; $params = [];
if ($q !== '')     { $where .= ' AND (l.email_attempted LIKE ? OR l.ip LIKE ?)'; $params[]="%$q%"; $params[]="%$q%"; }
if ($status !== ''){ $where .= ' AND l.status = ?'; $params[] = $status; }
if ($from !== '')  { $where .= ' AND l.created_at >= ?'; $params[] = $from . ' 00:00:00'; }
if ($to   !== '')  { $where .= ' AND l.created_at <= ?'; $params[] = $to   . ' 23:59:59'; }

$rows = $pdo->prepare("SELECT l.*, u.name AS user_name FROM login_logs l LEFT JOIN users u ON u.id = l.user_id
                       WHERE $where ORDER BY l.created_at DESC LIMIT 500");
$rows->execute($params);
$rows = $rows->fetchAll();

$pageTitle = 'Login activity';
require __DIR__ . '/../includes/header.php';
?>
<div class="page-header"><div class="container-xl">
    <h2 class="page-title">Login activity</h2>
    <div class="text-secondary">Last 500 sign-in attempts.</div>
</div></div>

<div class="page-body"><div class="container-xl">
    <div class="card mb-3"><div class="card-body">
        <form method="GET" class="row g-2">
            <div class="col-md-4"><input type="text" name="q" class="form-control" placeholder="Search email or IP" value="<?= e($q) ?>"></div>
            <div class="col-md-2">
                <select name="status" class="form-select">
                    <option value="">All statuses</option>
                    <?php foreach (['success','failed','locked','2fa_required','2fa_failed'] as $s): ?>
                        <option value="<?= e($s) ?>" <?= $s===$status?'selected':'' ?>><?= e($s) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2"><input type="date" name="from" class="form-control" value="<?= e($from) ?>"></div>
            <div class="col-md-2"><input type="date" name="to"   class="form-control" value="<?= e($to) ?>"></div>
            <div class="col-md-2"><button class="btn btn-primary w-100"><i class="ti ti-search"></i></button></div>
        </form>
    </div></div>

    <div class="card">
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead><tr><th>When</th><th>Email</th><th>User</th><th>Status</th><th>Reason</th><th>IP</th></tr></thead>
                <tbody>
                <?php if (!$rows): ?><tr><td colspan="6" class="text-center text-secondary py-4">No login events.</td></tr>
                <?php else: foreach ($rows as $r): ?>
                    <tr>
                        <td class="text-secondary small"><?= e($r['created_at']) ?></td>
                        <td><?= e($r['email_attempted']) ?></td>
                        <td><?= e($r['user_name'] ?? '--') ?></td>
                        <td>
                            <?php $cls = ['success'=>'success','failed'=>'warning','locked'=>'danger','2fa_required'=>'info','2fa_failed'=>'warning'][$r['status']] ?? 'secondary'; ?>
                            <span class="badge bg-<?= $cls ?>-lt"><?= e($r['status']) ?></span>
                        </td>
                        <td class="text-secondary small"><?= e($r['failure_reason']) ?></td>
                        <td class="small text-secondary"><?= e($r['ip']) ?></td>
                    </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div></div>
<?php require __DIR__ . '/../includes/footer.php'; ?>
