<?php
/**
 * View: admin/index (dashboard)
 *
 * Injected variables:
 *   @var int   $totalUsers
 *   @var int   $activeUsers
 *   @var array $recentLogins
 *
 * Available from bootstrap (via extract in View::display):
 *   $auth, $settings, $moduleRegistry, $currentUser
 */

$pageTitle = t('admin.dashboard');
include WEBROOT_PATH . '/includes/header.php';
?>
<div class="admin-layout">
<?php include WEBROOT_PATH . '/includes/admin_sidebar.php'; ?>
<div class="admin-content">

<h1><?= t('admin.title') ?></h1>
<p><?= t('admin.welcome') ?></p>

<div class="stat-grid">
    <div class="stat-card">
        <span class="stat-value"><?= $totalUsers ?></span>
        <span class="stat-label"><?= t('admin.total_users') ?></span>
    </div>
    <div class="stat-card">
        <span class="stat-value"><?= $activeUsers ?></span>
        <span class="stat-label"><?= t('admin.active_users') ?></span>
    </div>
</div>

<div class="card-grid" style="margin-top:2rem">
    <div class="card">
        <h2><?= t('nav.users') ?></h2>
        <p><a href="/admin/users.php" class="btn btn-primary"><?= t('user.list') ?></a>
           <a href="/admin/users.php?action=create" class="btn btn-secondary"><?= t('user.create') ?></a></p>
    </div>
    <div class="card">
        <h2><?= t('nav.settings') ?></h2>
        <p><a href="/admin/settings.php" class="btn btn-primary"><?= t('settings.title') ?></a></p>
    </div>
    <div class="card">
        <h2><?= t('module.title') ?></h2>
        <p><?= count(array_filter($moduleRegistry->getAll(), fn($m) => $m['active'])) ?>
           / <?= count($moduleRegistry->getAll()) ?> <?= t('module.active_count') ?></p>
        <p><a href="/admin/modules.php" class="btn btn-primary"><?= t('module.manage') ?></a></p>
    </div>
    <div class="card">
        <h2><?= t('batch.title') ?></h2>
        <p><?= t('batch.card_desc') ?></p>
        <p><a href="/admin/batch.php" class="btn btn-primary"><?= t('batch.manage') ?></a></p>
    </div>
</div>

<?php if ($recentLogins): ?>
<div class="card" style="margin-top:2rem">
    <h2><?= t('user.last_login') ?></h2>
    <table class="data-table">
        <thead>
            <tr>
                <th><?= t('auth.username') ?></th>
                <th><?= t('user.roles') ?></th>
                <th><?= t('user.last_login') ?></th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($recentLogins as $row): ?>
            <tr>
                <td><?= htmlspecialchars($row['username']) ?></td>
                <td>
                    <?php foreach ($row['roles'] as $r): ?>
                        <span class="badge <?= $r === ROLE_APP_ADMIN ? 'badge-warning' : 'badge-info' ?>"
                              style="margin-right:.2rem;font-size:.7rem">
                            <?= htmlspecialchars(t('role.' . $r)) ?>
                        </span>
                    <?php endforeach; ?>
                </td>
                <td><?= htmlspecialchars($row['last_login']) ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php endif; ?>

</div><!-- .admin-content -->
</div><!-- .admin-layout -->

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
