<?php $__env->startSection('content'); ?>
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <h4 class="page-title">Edit Role: <?php echo e(ucfirst($role->name)); ?></h4>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12">
        <form id="role-form">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>
            <div class="card">
                <div class="card-body">
                    <div class="mb-3">
                        <label for="name" class="form-label">Role Name <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="name" name="name" maxlength="255" required value="<?php echo e($role->name); ?>">
                    </div>

                    <hr>
                    <h5 class="mb-4">Permissions <span class="text-danger">*</span></h5>

                    <?php $__currentLoopData = $permissionsByModule; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $moduleName => $submodules): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="module-group mb-4 p-3 border rounded">
                            <h6 class="text-primary border-bottom pb-2"><?php echo e($moduleName); ?> MODULE</h6>
                            
                            <?php $__currentLoopData = $submodules; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $submoduleName => $permissions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="submodule-group mt-3 mb-3 ms-3">
                                    <div class="d-flex align-items-center mb-2">
                                        <h7 class="fw-bold me-3"><?php echo e($submoduleName); ?></h7>
                                        <div class="form-check">
                                            <input class="form-check-input select-all-submodule" type="checkbox" id="all_<?php echo e(Str::slug($moduleName . $submoduleName)); ?>">
                                            <label class="form-check-label text-muted small" for="all_<?php echo e(Str::slug($moduleName . $submoduleName)); ?>">Select All</label>
                                        </div>
                                    </div>
                                    
                                    <div class="row ms-2">
                                        <?php $__currentLoopData = $permissions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $permission): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <div class="col-md-3 mb-2">
                                                <div class="form-check">
                                                    <input class="form-check-input permission-checkbox" type="checkbox" name="permissions[]" value="<?php echo e($permission->id); ?>" id="perm_<?php echo e($permission->id); ?>" 
                                                        <?php echo e(in_array($permission->id, $rolePermissions) ? 'checked' : ''); ?>>
                                                    <label class="form-check-label" for="perm_<?php echo e($permission->id); ?>">
                                                        <?php echo e(ucwords(str_replace('-', ' ', $permission->name))); ?>

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

                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary">Update Role</button>
                        <a href="<?php echo e(route('settings.roles.index')); ?>" class="btn btn-secondary">Cancel</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
$(document).ready(function() {
    // Select all permissions in a submodule
    $('.select-all-submodule').change(function() {
        $(this).closest('.submodule-group').find('.permission-checkbox').prop('checked', $(this).prop('checked'));
    });

    // Handle form submission
    $('#role-form').submit(function(e) {
        e.preventDefault();

        let btn = $(this).find('button[type=submit]');
        btn.prop('disabled', true);

        let url = "<?php echo e(route('settings.roles.update', $role->id)); ?>";

        $.ajax({
            url: url,
            type: 'POST', // Use POST with _method PUT for Laravel
            data: $(this).serialize(),
            success: function(response) {
                if (response.status) {
                    Swal.fire('Success', response.message, 'success')
                        .then(() => window.location.href = "<?php echo e(route('settings.roles.index')); ?>");
                }
            },
            error: function(xhr) {
                btn.prop('disabled', false);

                if (xhr.status === 422) {
                    let errors = xhr.responseJSON.errors;
                    let errorList = '<ul style="text-align: left;">';

                    $.each(errors, function(field, messages) {
                        $.each(messages, function(index, message) {
                            errorList += '<li>' + message + '</li>';
                        });
                    });

                    errorList += '</ul>';

                    Swal.fire({
                        title: 'Validation Error',
                        html: errorList,
                        icon: 'error'
                    });
                } else {
                    Swal.fire('Error', 'An error occurred. Please try again.', 'error');
                }
            }
        });
    });
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/codeproh/hr.vadistilleries.lk/app/Modules/PLATFORM/UserManagement/Providers/../Views/roles/edit.blade.php ENDPATH**/ ?>