<?php
/** @var yii\web\View $this */
/** @var int $totalUsers */
/** @var int $totalGroups */
/** @var app\models\User[] $recentUsers */

use yii\helpers\Html;
use yii\helpers\Url;

$this->title = 'Dashboard';

$identity = Yii::$app->user->identity;
?>

<div class="page-title"><i class="fa fa-dashboard"></i> Dashboard</div>

<!-- Stats -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon bg-blue"><i class="fa fa-users"></i></div>
        <div>
            <div class="stat-value"><?= (int)$totalUsers ?></div>
            <div class="stat-label">Active Users</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon bg-green"><i class="fa fa-th-large"></i></div>
        <div>
            <div class="stat-value"><?= (int)$totalGroups ?></div>
            <div class="stat-label">User Groups</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon bg-orange"><i class="fa fa-clock-o"></i></div>
        <div>
            <div class="stat-value"><?= date('H:i') ?></div>
            <div class="stat-label">Current Time</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon bg-red"><i class="fa fa-calendar"></i></div>
        <div>
            <div class="stat-value"><?= date('d M Y') ?></div>
            <div class="stat-label">Today</div>
        </div>
    </div>
</div>

<!-- Welcome -->
<div class="lms-card">
    <div class="lms-card-header"><i class="fa fa-home"></i> Welcome</div>
    <div class="lms-card-body">
        <p>
            Hello, <strong><?= Html::encode($identity->getFullName()) ?></strong>! You are logged in as
            <span class="lms-badge <?= $identity->isAdmin() ? 'lms-badge-info' : 'lms-badge-success' ?>">
                <?= $identity->isAdmin() ? 'Administrator' : 'Staff' ?>
            </span>
        </p>
        <p style="margin-top:10px; color:#888; font-size:13px;">
            Use the navigation on the left to access system modules.
        </p>
    </div>
</div>

<!-- Users quick table (admin only) -->
<?php if ($identity->isAdmin()): ?>
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fa fa-users"></i> Recent Users
        <a href="<?= Url::to(['users/index']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
            <i class="fa fa-list"></i> View All
        </a>
    </div>
    <div class="table-responsive">
        <table class="lms-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Username</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Last Login</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($recentUsers)): ?>
                <tr><td colspan="6" style="text-align:center; color:#aaa; padding:20px;">No users found.</td></tr>
            <?php else: ?>
                <?php foreach ($recentUsers as $u): ?>
                <tr>
                    <td><?= (int)$u->id ?></td>
                    <td><?= Html::encode($u->getFullName()) ?></td>
                    <td><?= Html::encode($u->username) ?></td>
                    <td>
                        <span class="lms-badge <?= $u->isAdmin() ? 'lms-badge-info' : 'lms-badge-success' ?>">
                            <?= $u->isAdmin() ? 'Admin' : 'Staff' ?>
                        </span>
                    </td>
                    <td>
                        <span class="lms-badge <?= $u->is_active === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>">
                            <?= $u->is_active === 'Y' ? 'Active' : 'Inactive' ?>
                        </span>
                    </td>
                    <td><?= $u->last_login ? Html::encode($u->last_login) : '&mdash;' ?></td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
