<?php
/** @var yii\web\View $this */
/** @var app\models\User[] $users */

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

$this->title = 'Users';
?>

<div class="page-title">
    <i class="fa fa-users"></i> Users
    <a href="<?= Url::to(['users/create']) ?>" class="lms-btn lms-btn-primary lms-btn-sm">
        <i class="fa fa-plus"></i> Add User
    </a>
</div>

<div class="lms-card">
    <div class="table-responsive">
        <table class="lms-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Last Login</th>
                    <th style="text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($users)): ?>
                <tr><td colspan="8" style="text-align:center; color:#aaa; padding:24px;">No users found.</td></tr>
            <?php else: ?>
                <?php foreach ($users as $u): ?>
                <tr>
                    <td><?= (int)$u->id ?></td>
                    <td><?= Html::encode($u->getFullName()) ?></td>
                    <td><?= Html::encode($u->username) ?></td>
                    <td><?= Html::encode($u->email ?: '—') ?></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) : '—' ?></td>
                    <td style="text-align:center; white-space:nowrap;">
                        <a href="<?= Url::to(['users/update', 'id' => $u->id]) ?>"
                           class="lms-btn lms-btn-primary lms-btn-sm">
                            <i class="fa fa-edit"></i>
                        </a>
                        <?= Html::beginForm(['users/delete', 'id' => $u->id], 'post', ['style' => 'display:inline;']) ?>
                        <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                        <button type="submit" class="lms-btn lms-btn-danger lms-btn-sm"
                                onclick="return confirm('Delete this user?')">
                            <i class="fa fa-trash"></i>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
