
<?php $__env->startSection("content"); ?>
<?php $__env->startSection("title","Update Role Has Permissions"); ?>

<div class="row">
    <div class="col-xl-12 col-lg-12">
        <div class="alert alert-success my-3" style="display:none;" id="success"></div>
        
        <div class="card">
            <div class="card-header">
                <h4 class="card-title mb-0">Update Role has Permissions</h4>
            </div>
            
            <div class="card-body">
                <form accept-charset="utf-8" name="update-form" id="update-form">
                    <?php echo csrf_field(); ?>
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="mb-3">
                                <label for="role_id" class="form-label">Role</label>
                                <select name="role_id" id="role_id" class="form-control" readonly required>
                                    <option value="">-- Select Role --</option>
                                    <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($role->id); ?>" <?php echo e($role->id == $role_id ? 'selected' : ''); ?>>
                                        <?php echo e($role->name); ?>

                                    </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <span class='text-danger' id='update-role_idError'></span>
                            </div>
                        </div>
                    </div>

                    <div class="card-header mt-3">
                        <h4 class="card-title mb-0">Permissions</h4>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped" id="permissions-table">
                                <thead class="table-light">
                                    <tr>
                                        <th>Page</th>
                                        <th class="text-center">
                                            List<br><input type="checkbox" id="checkAllList">
                                        </th>
                                        <th class="text-center">
                                            Create<br><input type="checkbox" id="checkAllCreate">
                                        </th>
                                        <th class="text-center">
                                            Edit<br><input type="checkbox" id="checkAllEdit">
                                        </th>
                                        <th class="text-center">
                                            Delete<br><input type="checkbox" id="checkAllDelete">
                                        </th>
                                        <th class="text-center">
                                            All<br><input type="checkbox" id="checkAll">
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $grouped = collect($permissions)->groupBy('page'); ?>
                                    <?php $__currentLoopData = $grouped; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $page => $perms): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e(ucfirst(str_replace('-', ' ', $page))); ?></td>
                                        <?php $__currentLoopData = ['list', 'create', 'edit', 'delete']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $action): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php $perm = $perms->firstWhere('title', $action); ?>
                                        <td class="text-center">
                                            <?php if($perm): ?>
                                            <input type="checkbox" name="permission_ids[]" value="<?php echo e($perm->id); ?>"
                                                   class="perm-<?php echo e($action); ?> page-<?php echo e($page); ?>"
                                                   <?php if(isset($assignedPermissions) && in_array($perm->id, $assignedPermissions)): ?> checked <?php endif; ?>>
                                            <?php endif; ?>
                                        </td>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        <td class="text-center">
                                            <input type="checkbox" class="row-check-all" data-page="<?php echo e($page); ?>">
                                        </td>
                                    </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                            </table>
                        </div>
                        <span class='text-danger' id='update-permission_idError'></span>
                    </div>

                    <div class="p-3 mb-3 rounded">
                        <div class="row justify-content-end g-2">
                            <div class="col-lg-3">
                                <button type='submit' name='update-submit' id='update-submit'
                                        class='btn btn-outline-secondary w-100'
                                        data-args1='<?php echo e(url("/role-has-permissions/edit/". $role_id)); ?>'
                                        data-args2='admin/roles'
                                        onclick='addUpdate(event, "update")'>
                                    <i class="ri-save-line"></i> Save 
                                </button>
                            </div>
                            <div class="col-lg-2">
                                <a href="<?php echo e(url('/admin/role-has-permissions')); ?>" class="btn btn-primary w-100">
                                    <i class="ri-close-circle-line"></i> Cancel
                                </a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script>
    document.getElementById('checkAllList').addEventListener('change', function () {
        document.querySelectorAll('.perm-list').forEach(el => el.checked = this.checked);
    });
    document.getElementById('checkAllCreate').addEventListener('change', function () {
        document.querySelectorAll('.perm-create').forEach(el => el.checked = this.checked);
    });
    document.getElementById('checkAllEdit').addEventListener('change', function () {
        document.querySelectorAll('.perm-edit').forEach(el => el.checked = this.checked);
    });
    document.getElementById('checkAllDelete').addEventListener('change', function () {
        document.querySelectorAll('.perm-delete').forEach(el => el.checked = this.checked);
    });
    document.getElementById('checkAll').addEventListener('change', function () {
        document.querySelectorAll('input[name="permission_ids[]"]').forEach(el => el.checked = this.checked);
    });
    document.querySelectorAll('.row-check-all').forEach(rowCheckbox => {
        rowCheckbox.addEventListener('change', function () {
            let page = this.getAttribute('data-page');
            document.querySelectorAll(`.page-${page}`).forEach(el => el.checked = this.checked);
        });
    });
</script>

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

<?php echo $__env->make("layouts.metor.admin", \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /Applications/MAMP/htdocs/test1/resources/views/role-has-permissions/edit.blade.php ENDPATH**/ ?>