

<?php $__env->startSection('title', 'Roles & Permissions'); ?>

<?php $__env->startSection('content'); ?>

    <div class="card">

        <div class="card-header">

            <div class="d-flex justify-content-between align-items-center">

                <h4 class="mb-0">
                    Roles & Permissions
                </h4>

                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addRoleModal">

                    <i class="bi bi-plus-circle"></i>
                    Add Role

                </button>

            </div>

        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-striped" id="rolesTable">

                    <thead>
                        <tr>
                            <th>Role Name</th>
                            <th>Permissions</th>
                            <th>Users</th>
                            <th width="120">Action</th>
                        </tr>
                    </thead>

                    <tbody>

                        <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>

                                <td>
                                    <?php echo e($role->name); ?>

                                </td>

                                <td>
                                    <?php echo e($role->permissions->count()); ?>

                                </td>

                                <td>
                                    <?php echo e($role->users->count()); ?>

                                </td>

                                <td>

                                    <button class="btn btn-sm btn-primary edit-role" data-id="<?php echo e($role->id); ?>"
                                        data-name="<?php echo e($role->name); ?>" data-permissions='<?php echo json_encode($role->permissions->pluck('name'), 15, 512) ?>'>

                                        <i class="bi bi-pencil"></i>

                                    </button>

                                </td>

                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

    <?php echo $__env->make('roles.partials.add-role-modal', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
    <?php echo $__env->make('roles.partials.edit-role-modal', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script>
        $(document).ready(function() {

            $('#rolesTable').DataTable({
                pageLength: 25
            });

        });


        $(document).on('click', '.edit-role', function() {

            let id = $(this).data('id');
            let name = $(this).data('name');
            let permissions = $(this).data('permissions');

            $('#edit_role_name').val(name);

            $('.permission-checkbox').prop('checked', false);

            permissions.forEach(function(permission) {

                $('.permission-checkbox[value="' + permission + '"]')
                    .prop('checked', true);

            });

            $('#editRoleForm').attr(
                'action',
                "<?php echo e(url('roles')); ?>/" + id
            );

            const modal = new bootstrap.Modal(
                document.getElementById('editRoleModal')
            );

            modal.show();

        });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.master', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\Projects\lms-ects\resources\views/roles/index.blade.php ENDPATH**/ ?>