<?php $__env->startSection('content'); ?>
<div class="container-fluid">

    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-3">
        <div>
            <h2 class="fw-bold text-dark mb-0"><i class="bi bi-shield-lock-fill me-2"></i>Roles y Permisos</h2>
            <p class="text-muted mb-0">Configura los accesos a los módulos por cada tipo de usuario</p>
        </div>
        <form action="<?php echo e(route('roles.permissions.seed')); ?>" method="POST" onsubmit="return confirm('¿Deseas regenerar los permisos base? Esto no afecta los permisos asignados.');">
            <?php echo csrf_field(); ?>
            <button class="btn btn-outline-secondary fw-bold shadow-sm">
                <i class="bi bi-arrow-clockwise me-2"></i> Regenerar Permisos Base
            </button>
        </form>
    </div>

    <div class="row mb-4">
        <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $roleKey => $roleData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="col-md-4 col-xl-2 mb-3">
            <div class="card border-0 shadow-sm h-100 border-start border-4 border-<?php echo e($roleData['color']); ?>">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-2">
                        <span class="badge bg-<?php echo e($roleData['color']); ?> me-2 px-3 py-2">
                            <?php if($roleKey === 'superadmin'): ?><i class="bi bi-shield-lock-fill me-1"></i><?php endif; ?>
                            <?php echo e($roleData['label']); ?>

                        </span>
                    </div>
                    <p class="small text-muted mb-0"><?php echo e($roleData['description']); ?></p>
                    <?php if($roleKey === 'superadmin'): ?>
                        <div class="mt-2 small text-danger">
                            <i class="bi bi-exclamation-triangle-fill me-1"></i>
                            Permisos inmutables
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0 fw-bold"><i class="bi bi-list-check me-2"></i>Configuración actual</h5>
                <div>
                    <label class="form-label small mb-0 me-2 fw-bold">Rol a editar:</label>
                    <select id="roleSelector" class="form-select d-inline-block" style="width: auto;">
                        <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $roleKey => $roleData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php if($roleKey !== 'superadmin'): ?>
                            <option value="<?php echo e($roleKey); ?>" data-color="<?php echo e($roleData['color']); ?>">
                                <?php echo e($roleData['label']); ?>

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

            <div class="alert alert-info border-0 border-start border-info border-4 mb-3">
                <i class="bi bi-info-circle-fill me-2"></i>
                <strong>Tip:</strong> Selecciona un rol del menú superior, marca los módulos a los que debe tener acceso y guarda los cambios.
            </div>
        </div>
    </div>

    <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $roleKey => $roleData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php
        $isSuperAdmin = $roleKey === 'superadmin';
        $rolePermIds = $rolePermissions[$roleKey] ?? [];
    ?>
    <div class="role-config-card mb-4" data-role="<?php echo e($roleKey); ?>" style="<?php echo e($roleKey === 'admin' ? '' : 'display:none;'); ?>">

        <div class="card border-0 shadow-sm border-top border-4 border-<?php echo e($roleData['color']); ?>">
            <div class="card-header bg-<?php echo e($roleData['color']); ?> bg-opacity-10 border-0 py-3 px-4">
                <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
                    <h5 class="mb-0 text-<?php echo e($roleData['color']); ?> fw-bold">
                        <i class="bi bi-person-badge-fill me-2"></i>
                        <?php echo e($roleData['label']); ?>

                        <?php if($isSuperAdmin): ?>
                            <span class="badge bg-dark ms-2"><i class="bi bi-shield-lock-fill me-1"></i>PROTEGIDO</span>
                        <?php endif; ?>
                    </h5>
                    <div>
                        <button type="button" class="btn btn-sm btn-outline-dark check-all-btn" data-role="<?php echo e($roleKey); ?>" <?php if($isSuperAdmin): ?> disabled <?php endif; ?>>
                            <i class="bi bi-check2-all me-1"></i> Marcar todos
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-secondary uncheck-all-btn" data-role="<?php echo e($roleKey); ?>" <?php if($isSuperAdmin): ?> disabled <?php endif; ?>>
                            <i class="bi bi-x-lg me-1"></i> Desmarcar todos
                        </button>
                    </div>
                </div>
            </div>

            <form action="<?php echo e(route('roles.permissions.update')); ?>" method="POST" class="role-form">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="role" value="<?php echo e($roleKey); ?>">

                <div class="card-body p-4">
                    <div class="row g-4">
                        <?php $__currentLoopData = $modules; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $moduleSlug => $moduleName): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                                $perm = $permissions->where('module', $moduleSlug)->first();
                                $checked = $perm ? in_array($perm->id, $rolePermIds) : false;
                            ?>
                            <div class="col-md-6 col-lg-4 col-xl-3">
                                <label class="module-card card border h-100 p-3 mb-0 <?php if($checked): ?> border-<?php echo e($roleData['color']); ?> bg-<?php echo e($roleData['color']); ?> bg-opacity-5 <?php else: ?> border-light <?php endif; ?>" style="cursor: <?php if($isSuperAdmin): ?> not-allowed; opacity: 0.9; <?php else: ?> pointer; <?php endif; ?> transition: all 0.2s;">
                                    <div class="d-flex align-items-start">
                                        <div class="form-check form-switch me-2 mt-1">
                                            <input class="form-check-input module-checkbox" 
                                                   type="checkbox" 
                                                   name="permissions[]" 
                                                   value="<?php echo e($perm->id ?? 0); ?>"
                                                   <?php if($checked): ?> checked <?php endif; ?>
                                                   <?php if($isSuperAdmin): ?> disabled onchange="return false;" <?php endif; ?>
                                                   data-module="<?php echo e($moduleSlug); ?>">
                                        </div>
                                        <div>
                                            <div class="fw-bold small text-dark mb-1"><?php echo e($moduleName); ?></div>
                                            <div class="text-muted" style="font-size: 11px;">
                                                <i class="bi bi-folder2-open me-1"></i>
                                                Módulo: <?php echo e($moduleSlug); ?>

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

                <?php if(!$isSuperAdmin): ?>
                <div class="card-footer bg-light border-0 py-3 px-4 d-flex justify-content-end">
                    <button type="submit" class="btn btn-<?php echo e($roleData['color']); ?> fw-bold px-4 shadow-sm">
                        <i class="bi bi-save2-fill me-2"></i> Guardar permisos de <?php echo e($roleData['label']); ?>

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

</div>

<style>
    .module-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    }
    .module-checkbox:checked + * + * + * {
        background-color: var(--bs-primary);
    }
</style>

<script>
    const roleSelector = document.getElementById('roleSelector');
    const roleCards = document.querySelectorAll('.role-config-card');

    function showRole(roleKey) {
        roleCards.forEach(card => {
            card.style.display = card.getAttribute('data-role') === roleKey ? 'block' : 'none';
        });
    }

    if (roleSelector) {
        roleSelector.addEventListener('change', function () {
            showRole(this.value);
        });
        showRole(roleSelector.value);
    }

    document.querySelectorAll('.check-all-btn').forEach(btn => {
        btn.addEventListener('click', function () {
            const role = this.getAttribute('data-role');
            const card = document.querySelector('.role-config-card[data-role="' + role + '"]');
            if (card) {
                card.querySelectorAll('.module-checkbox').forEach(cb => {
                    if (!cb.disabled) cb.checked = true;
                });
            }
        });
    });

    document.querySelectorAll('.uncheck-all-btn').forEach(btn => {
        btn.addEventListener('click', function () {
            const role = this.getAttribute('data-role');
            const card = document.querySelector('.role-config-card[data-role="' + role + '"]');
            if (card) {
                card.querySelectorAll('.module-checkbox').forEach(cb => {
                    if (!cb.disabled) cb.checked = false;
                });
            }
        });
    });

    document.querySelectorAll('.module-card').forEach(card => {
        card.addEventListener('click', function (e) {
            const checkbox = this.querySelector('.module-checkbox');
            if (checkbox && !checkbox.disabled && e.target !== checkbox) {
                checkbox.checked = !checkbox.checked;
            }
        });
    });
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\restaurante\resources\views/roles_permissions/index.blade.php ENDPATH**/ ?>