<?php
function ou_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function ou_t($k, $f) {
    return function_exists('ec_t') ? ec_t($k, $f) : $f;
}
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;">
        <div class="card-body p-4 d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="fw-bold mb-1">
                    <i class="fa fa-users-viewfinder"></i>
                    <?= ou_h(ou_t('online.title','Online Users')) ?>
                </h3>
                <div class="opacity-75"><?= ou_h(ou_t('online.subtitle','Users active recently in the system.')) ?></div>
            </div>
            <div class="rounded-4 px-4 py-3" style="background:rgba(255,255,255,.14);">
                <div class="small opacity-75"><?= ou_h(ou_t('online.currently_online','Currently Online')) ?></div>
                <div class="fs-2 fw-bold"><?= (int)($totalOnline ?? 0) ?></div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form class="row g-3" method="get" action="/easychair/admin/online-users">
                <div class="col-md-6">
                    <input name="q" class="form-control" value="<?= ou_h($_GET['q'] ?? '') ?>" placeholder="<?= ou_h(ou_t('common.search','Search')) ?>">
                </div>
                <div class="col-md-3">
                    <select name="minutes" class="form-select">
                        <?php foreach ([5,15,30,60,120,240] as $m): ?>
                            <option value="<?= $m ?>" <?= (int)($_GET['minutes'] ?? 15) === $m ? 'selected' : '' ?>>
                                <?= $m ?> <?= ou_h(ou_t('online.minutes','Minutes')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <button class="btn btn-primary rounded-pill w-100">
                        <i class="fa fa-search"></i>
                        <?= ou_h(ou_t('common.filter','Filter')) ?>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>User</th>
                        <th>Role</th>
                        <th><?= ou_h(ou_t('online.last_seen','Last Seen')) ?></th>
                        <th><?= ou_h(ou_t('online.ip','IP Address')) ?></th>
                        <th><?= ou_h(ou_t('online.url','Last URL')) ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach (($users ?? []) as $u): ?>
                        <tr>
                            <td>
                                <strong><?= ou_h($u['email'] ?? $u['username'] ?? '') ?></strong>
                                <div class="text-muted small">
                                    <?= ou_h(trim(($u['first_name'] ?? '') . ' ' . ($u['last_name'] ?? ''))) ?>
                                </div>
                            </td>
                            <td>
                                <span class="badge bg-primary">
                                    <?= ou_h(function_exists('ec_role_t') ? ec_role_t((string)($u['role'] ?? $u['role_code'] ?? '')) : ($u['role'] ?? '')) ?>
                                </span>
                            </td>
                            <td><?= ou_h($u['last_seen_at'] ?? '') ?></td>
                            <td><code><?= ou_h($u['last_seen_ip'] ?? '') ?></code></td>
                            <td><code><?= ou_h($u['last_seen_url'] ?? '') ?></code></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (empty($users)): ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted py-4">
                                <?= ou_h(ou_t('common.no_data','No data available.')) ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
