<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** ADMIN USERS LIST — Expects: $users, $user (current). */
?>
<div class="page-head">
	<div><h2>Admin Users</h2><p>Manage back-office accounts and access roles.</p></div>
	<a href="<?= base_url('admin/users/create') ?>" class="btn btn-primary"><?= bloom_icon('plus', 16) ?> Add User</a>
</div>

<div class="card"><div class="card-body flush">
	<div class="table-wrap">
		<table class="data">
			<thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Last Login</th><th>Status</th><th class="text-right">Actions</th></tr></thead>
			<tbody>
			<?php foreach ($users as $u): $self = ((int) $u['id'] === (int) $user['id']); ?>
				<tr>
					<td>
						<div class="row-title"><?= html_escape($u['name']) ?> <?php if ($self): ?><span class="pill pill-blue">You</span><?php endif; ?></div>
					</td>
					<td class="muted"><?= html_escape($u['email']) ?></td>
					<td>
						<span class="pill <?= $u['role'] === 'super_admin' ? 'pill-amber' : 'pill-grey' ?>">
							<?= html_escape(ucwords(str_replace('_',' ',$u['role']))) ?>
						</span>
					</td>
					<td class="muted"><?= $u['last_login'] ? nice_date($u['last_login']) : 'Never' ?></td>
					<td>
						<?php if ($self): ?>
							<?= status_pill('published') ?>
						<?php else: ?>
							<a href="<?= base_url('admin/users/toggle/'.$u['id']) ?>"><?= status_pill($u['is_active'] ? 'published' : 'draft') ?></a>
						<?php endif; ?>
					</td>
					<td><div class="row-actions" style="justify-content:flex-end;">
						<a href="<?= base_url('admin/users/edit/'.$u['id']) ?>" class="icon-btn"><?= bloom_icon('edit', 16) ?></a>
						<?php if ( ! $self): ?>
							<a href="<?= base_url('admin/users/delete/'.$u['id']) ?>" class="icon-btn danger" data-confirm="Delete this admin user?"><?= bloom_icon('trash', 16) ?></a>
						<?php endif; ?>
					</div></td>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>
	</div>
</div></div>
