<?php ob_start(); ?>

<?php admin_content_header(__('admin.nav.users'), 'users', [
    ['url' => admin_url('users/create'), 'label' => __('admin.users.new'), 'class' => 'btn-primary'],
]); ?>

<?php
$bulkUrl = admin_url('users/bulk');
$bulkActions = [
    ['action' => 'activate',   'label' => __('admin.common.bulk_activate'),   'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'deactivate', 'label' => __('admin.common.bulk_deactivate'), 'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'delete',     'label' => __('admin.common.bulk_delete'),     'class' => 'btn-bulk-danger', 'confirm' => __('admin.common.bulk_confirm_delete')],
];
include dirname(__DIR__) . '/layouts/_bulk_bar.php';
?>

<div class="table-responsive">
<table class="data-table" data-bulk-table>
    <thead>
        <tr>
            <th class="bulk-check-cell"><input type="checkbox" data-bulk-select-all aria-label="Select all"></th>
            <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)): ?>
        <?php admin_empty_state(__('admin.users.no_users'), 'users', admin_url('users/create'), __('admin.users.new'), true, 7); ?>
        <?php else: ?>
        <?php foreach ($users as $user): ?>
        <?php $isSelf = ((int) $user['id'] === $auth->userId()); ?>
        <tr <?= $isSelf ? 'class="row-highlight"' : '' ?>>
            <td class="bulk-check-cell">
                <?php if (!$isSelf): ?>
                <input type="checkbox" data-bulk-item value="<?= (int) $user['id'] ?>" aria-label="Select <?= e($user['username']) ?>">
                <?php endif; ?>
            </td>
            <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_url('users/edit/' . (int) $user['id']) ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <?php if (!$isSelf): ?>
                <form method="POST" action="<?= admin_url('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 btn-disabled" 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'; ?>
