<div class="row g-4">
    <div class="col-xl-4 col-lg-5">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Roles</h5>
            </div>
            <div class="card-body p-0">
                <div class="roles-list">
                    <?php $__empty_1 = true; $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <a href="<?php echo e(route('staff.index', ['tab' => 'roles', 'role' => $role->id])); ?>"
                           class="roles-item text-decoration-none <?php echo e(($selectedRole?->id) === $role->id ? 'active' : ''); ?>">
                            <div class="roles-item-icon" style="background: color-mix(in srgb, var(--accent-color) 15%, transparent); color: var(--accent-color);">
                                <i class="bi bi-shield-fill"></i>
                            </div>
                            <div class="roles-item-info">
                                <span class="roles-item-name"><?php echo e($role->name); ?></span>
                                <span class="roles-item-count"><?php echo e($role->users_count); ?> users</span>
                            </div>
                        </a>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <div class="p-4 text-center text-muted">No roles defined</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-8 col-lg-7">
        <?php if($selectedRole): ?>
            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('update', $selectedRole)): ?>
                <form id="rolePermissionsForm" action="<?php echo e(route('roles.update', $selectedRole)); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PUT'); ?>
            <?php endif; ?>

            <div class="card">
                <div class="card-header d-flex flex-column flex-sm-row align-items-sm-center justify-content-sm-between gap-2">
                    <div>
                        <h5 class="card-title mb-0">Permissions — <?php echo e($selectedRole->name); ?></h5>
                        <span class="text-muted small">Spatie Laravel Permission (cached, module-based)</span>
                    </div>
                    <div class="d-flex flex-wrap gap-2">
                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('delete', $selectedRole)): ?>
                            <?php if(! $selectedRole->isSuperAdmin()): ?>
                                <form action="<?php echo e(route('roles.destroy', $selectedRole)); ?>" method="POST"
                                      onsubmit="return confirm('Delete the role &quot;<?php echo e($selectedRole->name); ?>&quot;?');">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('DELETE'); ?>
                                    <button type="submit" class="btn btn-outline-danger btn-sm"
                                            <?php if($selectedRole->users_count > 0): echo 'disabled'; endif; ?>
                                            title="<?php echo e($selectedRole->users_count > 0 ? 'Reassign or remove staff before deleting this role' : 'Delete role'); ?>">
                                        <i class="bi bi-trash me-1"></i> Delete Role
                                    </button>
                                </form>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('update', $selectedRole)): ?>
                            <button type="submit" class="btn btn-primary btn-sm" form="rolePermissionsForm" <?php if($selectedRole->isSuperAdmin()): echo 'disabled'; endif; ?>>
                                <i class="bi bi-check-lg me-1"></i> Save Changes
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="card-body p-0">
                    <?php if($selectedRole->users_count > 0): ?>
                        <div class="px-3 py-2 border-bottom bg-light small text-muted">
                            <?php echo e($selectedRole->users_count); ?> staff member(s) use this role. Reassign them before deleting.
                        </div>
                    <?php endif; ?>
                    <div class="table-responsive">
                        <table class="table roles-perm-table mb-0">
                            <thead>
                                <tr>
                                    <th>Module</th>
                                    <th>Permission</th>
                                    <th class="text-center" style="width: 100px;">Granted</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $permissionGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group => $permissions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php $__currentLoopData = $permissions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $permission): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <tr>
                                            <?php if($index === 0): ?>
                                                <td rowspan="<?php echo e(count($permissions)); ?>" class="align-middle fw-semibold bg-light">
                                                    <?php echo e($group); ?>

                                                </td>
                                            <?php endif; ?>
                                            <td><?php echo e($permission['label']); ?></td>
                                            <td class="text-center">
                                                <input type="checkbox" class="form-check-input"
                                                       name="permissions[]"
                                                       value="<?php echo e($permission['name']); ?>"
                                                       <?php if($permission['granted']['all']): echo 'checked'; endif; ?>
                                                       <?php if($selectedRole->isSuperAdmin() || auth()->user()->cannot('update', $selectedRole)): echo 'disabled'; endif; ?>>
                                            </td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('update', $selectedRole)): ?>
                </form>
            <?php endif; ?>
        <?php else: ?>
            <div class="card">
                <div class="card-body text-center text-muted py-5">
                    Create a role to manage permissions.
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('create', \App\Models\Role::class)): ?>
    <div class="modal fade" id="addRoleModal" tabindex="-1" aria-labelledby="addRoleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="<?php echo e(route('roles.store')); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <div class="modal-header">
                        <h5 class="modal-title" id="addRoleModalLabel">Add Role</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="role_name" class="form-label">Role Name</label>
                            <input type="text" name="name" id="role_name" class="form-control <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                   value="<?php echo e(old('name')); ?>" required>
                            <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Create Role</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php endif; ?>
<?php /**PATH D:\RABI\rabi\module-laravel-platform\apps\admin\resources\views/roles/partials/permissions-panel.blade.php ENDPATH**/ ?>