<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.nav.users') ?></h2>
    <div class="header-actions">
        <a href="/admin/users/create" class="btn btn-primary"><?= __('admin.users.new') ?></a>
    </div>
</div>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <th><?= __('admin.users.username') ?></th>
            <th><?= __('admin.common.email') ?? 'Email' ?></th>
            <th><?= __('admin.users.role') ?></th>
            <th><?= __('admin.common.status') ?></th>
            <th><?= __('admin.users.created') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($users)): ?>
        <tr>
            <td colspan="6" class="text-center text-muted"><?= __('admin.users.no_users') ?></td>
        </tr>
        <?php else: ?>
        <?php foreach ($users as $user): ?>
        <?php $isSelf = ((int) $user['id'] === $auth->userId()); ?>
        <tr <?= $isSelf ? 'class="row-highlight"' : '' ?>>
            <td>
                <strong><?= e($user['username']) ?></strong>
                <?php if ($isSelf): ?>
                <span class="badge badge-primary" title="<?= __('admin.users.you_badge') ?? 'You' ?>"><?= __('admin.users.you_badge') ?></span>
                <?php endif; ?>
            </td>
            <td><?= e($user['email']) ?></td>
            <td>
                <span class="badge <?= $user['role'] === 'admin' ? 'badge-warning' : 'badge-secondary' ?>">
                    <?= e($user['role']) ?>
                </span>
            </td>
            <td>
                <span class="badge <?= $user['is_active'] ? 'badge-success' : 'badge-draft' ?>">
                    <?= $user['is_active'] ? __('admin.common.active') : __('admin.common.inactive') ?>
                </span>
            </td>
            <td><?= !empty($user['created_at']) ? e(format_date($user['created_at'])) : '—' ?></td>
            <td class="actions">
                <a href="/admin/users/edit/<?= (int) $user['id'] ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <?php if (!$isSelf): ?>
                <form method="POST" action="/admin/users/delete/<?= (int) $user['id'] ?>"
                      class="inline-form"
                      onsubmit="return confirm('<?= __('admin.users.confirm_delete', [':name' => e(addslashes($user['username']))]) ?>')">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-sm btn-danger"><?= __('admin.common.delete') ?></button>
                </form>
                <?php else: ?>
                <span class="btn btn-sm btn-danger" style="opacity:.4;cursor:not-allowed;" title="<?= __('admin.users.cannot_delete_self') ?>"><?= __('admin.common.delete') ?></span>
                <?php endif; ?>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
