<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h5 mb-0">User Management</h1>
    <a href="<?= base_url('users/create') ?>" class="btn btn-primary btn-sm">
        <i class="fa-solid fa-plus me-1"></i>Add User
    </a>
</div>

<div class="stat-card mb-3">
    <form action="<?= base_url('users') ?>" method="get" class="row g-2 align-items-end">
        <div class="col-md-3">
            <label class="form-label small">Search</label>
            <input type="text" name="q" class="form-control form-control-sm" placeholder="Name, username or email"
                   value="<?= esc($filters['search']) ?>">
        </div>
        <div class="col-md-2">
            <label class="form-label small">Status</label>
            <select name="status" class="form-select form-select-sm">
                <option value="">Any</option>
                <?php foreach (['active', 'inactive', 'locked'] as $s): ?>
                    <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label small">Role</label>
            <select name="role_id" class="form-select form-select-sm">
                <option value="">Any</option>
                <?php foreach ($roles as $r): ?>
                    <option value="<?= $r['id'] ?>" <?= (string) $filters['roleId'] === (string) $r['id'] ? 'selected' : '' ?>><?= esc($r['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label small">Branch</label>
            <select name="branch_id" class="form-select form-select-sm">
                <option value="">Any</option>
                <?php foreach ($branches as $b): ?>
                    <option value="<?= $b['id'] ?>" <?= (string) $filters['branchId'] === (string) $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label small">Department</label>
            <select name="department_id" class="form-select form-select-sm">
                <option value="">Any</option>
                <?php foreach ($departments as $d): ?>
                    <option value="<?= $d['id'] ?>" <?= (string) $filters['departmentId'] === (string) $d['id'] ? 'selected' : '' ?>><?= esc($d['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-1 d-grid">
            <button type="submit" class="btn btn-sm btn-outline-primary">Filter</button>
        </div>

        <div class="col-12">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" id="trashed" name="trashed" value="1" <?= $filters['showTrashed'] ? 'checked' : '' ?> onchange="this.form.submit()">
                <label class="form-check-label small" for="trashed">Show deleted users</label>
            </div>
        </div>
    </form>
</div>

<div class="stat-card">
    <div class="table-responsive">
        <table class="table align-middle mb-0">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Contact</th>
                    <th>Role</th>
                    <th>Branch / Department</th>
                    <th>Status</th>
                    <th>Last Login</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $u): ?>
                    <?php $roleName = array_column($roles, 'name', 'id')[$u->role_id] ?? '—'; ?>
                    <?php $branchName = array_column($branches, 'name', 'id')[$u->branch_id] ?? '—'; ?>
                    <?php $deptName = array_column($departments, 'name', 'id')[$u->department_id] ?? '—'; ?>
                    <tr class="<?= $u->deleted_at ? 'opacity-50' : '' ?>">
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <span class="avatar-circle"><?= esc(strtoupper(substr($u->first_name, 0, 1))) ?></span>
                                <div>
                                    <a href="<?= base_url('users/' . $u->id) ?>" class="fw-semibold text-decoration-none">
                                        <?= esc($u->getFullName()) ?>
                                    </a>
                                    <?php if (in_array($u->id, $onlineIds, true)): ?>
                                        <span class="badge text-bg-success ms-1" title="Online now">●</span>
                                    <?php endif; ?>
                                    <div class="text-muted small">@<?= esc($u->username) ?></div>
                                </div>
                            </div>
                        </td>
                        <td><?= esc($u->email) ?></td>
                        <td><?= esc($roleName) ?></td>
                        <td><?= esc($branchName) ?> / <?= esc($deptName) ?></td>
                        <td>
                            <?php $badge = ['active' => 'success', 'inactive' => 'secondary', 'locked' => 'danger'][$u->status] ?? 'secondary'; ?>
                            <span class="badge text-bg-<?= $badge ?>"><?= esc(ucfirst($u->status)) ?></span>
                            <?php if ($u->deleted_at): ?><span class="badge text-bg-dark">Deleted</span><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= esc($u->last_login_at ?? 'Never') ?></td>
                        <td class="text-end">
                            <div class="dropdown">
                                <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown"><i class="fa-solid fa-ellipsis"></i></button>
                                <ul class="dropdown-menu dropdown-menu-end">
                                    <li><a class="dropdown-item" href="<?= base_url('users/' . $u->id) ?>"><i class="fa-solid fa-eye me-2"></i>View</a></li>
                                    <?php if (! $u->deleted_at): ?>
                                        <li><a class="dropdown-item" href="<?= base_url('users/' . $u->id . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit</a></li>
                                        <li>
                                            <form action="<?= base_url('users/' . $u->id . '/reset-password') ?>" method="post" onsubmit="return confirm('Reset this user\'s password?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="dropdown-item"><i class="fa-solid fa-key me-2"></i>Reset Password</button>
                                            </form>
                                        </li>
                                        <li>
                                            <?php if ($u->status === 'active'): ?>
                                                <form action="<?= base_url('users/' . $u->id . '/deactivate') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-ban me-2"></i>Deactivate</button>
                                                </form>
                                            <?php else: ?>
                                                <form action="<?= base_url('users/' . $u->id . '/activate') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-check me-2"></i>Activate</button>
                                                </form>
                                            <?php endif; ?>
                                        </li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li>
                                            <form action="<?= base_url('users/' . $u->id . '/delete') ?>" method="post" onsubmit="return confirm('Move this user to trash?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i>Delete</button>
                                            </form>
                                        </li>
                                    <?php else: ?>
                                        <li>
                                            <form action="<?= base_url('users/' . $u->id . '/restore') ?>" method="post">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="dropdown-item"><i class="fa-solid fa-rotate-left me-2"></i>Restore</button>
                                            </form>
                                        </li>
                                    <?php endif; ?>
                                </ul>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (count($users) === 0): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No users found.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <div class="mt-3">
        <?= $pager->links() ?>
    </div>
</div>

<?= $this->endSection() ?>
