<?php $__env->startSection('title', 'Role Profiles'); ?>
<?php $__env->startSection('page-title', 'Role Profiles'); ?>
<?php $__env->startSection('page-subtitle', 'Create staff access profiles and choose exactly what each profile can use'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $selectedProfileId = (int) request('profile', $profiles->first()?->id);
    $selectedProfile = $profiles->firstWhere('id', $selectedProfileId) ?? $profiles->first();
?>

<div class="row g-4">
    <div class="col-xl-4">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <div class="d-flex justify-content-between gap-3 align-items-start mb-3">
                    <div>
                        <h2 class="h5 mb-1">Profiles</h2>
                        <div class="text-muted small">Select a profile to view or edit its permissions.</div>
                    </div>
                    <span class="badge text-bg-light text-dark"><?php echo e($profiles->count()); ?> total</span>
                </div>

                <?php if($profiles->isNotEmpty()): ?>
                    <label class="form-label">Role profile</label>
                    <select class="form-select mb-3" id="roleProfilePicker">
                        <?php $__currentLoopData = $profiles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $profile): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($profile->id); ?>" <?php if($selectedProfile?->id === $profile->id): echo 'selected'; endif; ?>>
                                <?php echo e($profile->name); ?> · <?php echo e($profile->users_count); ?> staff
                            </option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>

                    <?php if($selectedProfile): ?>
                        <div class="border rounded p-3 bg-light" id="selectedProfileCard">
                            <div class="fw-semibold" data-selected-profile-name><?php echo e($selectedProfile->name); ?></div>
                            <div class="d-flex flex-wrap gap-2 mt-2">
                                <span class="badge text-bg-light text-dark" data-selected-profile-role><?php echo e($roles[$selectedProfile->base_role] ?? 'Custom'); ?></span>
                                <span class="badge <?php echo e($selectedProfile->is_active ? 'text-bg-success' : 'text-bg-secondary'); ?>" data-selected-profile-status><?php echo e($selectedProfile->is_active ? 'Active' : 'Inactive'); ?></span>
                                <span class="badge text-bg-light text-dark" data-selected-profile-permissions><?php echo e($selectedProfile->permissionCount()); ?> permissions</span>
                                <span class="badge text-bg-light text-dark" data-selected-profile-staff><?php echo e($selectedProfile->users_count); ?> staff</span>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php else: ?>
                    <div class="text-muted">No role profiles have been created yet.</div>
                <?php endif; ?>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <button class="btn btn-primary w-100 d-flex justify-content-center align-items-center gap-2" type="button" data-bs-toggle="collapse" data-bs-target="#createRoleProfilePanel" aria-expanded="<?php echo e($errors->any() ? 'true' : 'false'); ?>" aria-controls="createRoleProfilePanel">
                    <i class="bi bi-shield-plus"></i> Create new profile
                </button>

                <div class="collapse <?php echo e($errors->any() ? 'show' : ''); ?>" id="createRoleProfilePanel">
                    <div class="pt-3">
                        <form method="POST" action="<?php echo e(route('tenant.role-profiles.store')); ?>" class="vstack gap-3" id="roleProfileCreateForm">
                            <?php echo csrf_field(); ?>
                            <input name="name" value="<?php echo e(old('name')); ?>" class="form-control" placeholder="Profile name, e.g. Field Collector" required>
                            <textarea name="description" class="form-control" rows="3" placeholder="What this staff profile should do"><?php echo e(old('description')); ?></textarea>
                            <select name="base_role" id="roleTemplateSelect" class="form-select">
                                <option value="">No template</option>
                                <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($value); ?>" <?php if(old('base_role') === $value): echo 'selected'; endif; ?>><?php echo e($label); ?> template</option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" role="switch" name="is_active" value="1" id="createProfileActive" checked>
                                <label class="form-check-label" for="createProfileActive">Profile active</label>
                            </div>

                            <div class="accordion" id="createPermissionsAccordion">
                                <?php $__currentLoopData = $permissionGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group => $permissions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div class="accordion-item">
                                        <h3 class="accordion-header">
                                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#create-permissions-<?php echo e(\Illuminate\Support\Str::slug($group)); ?>">
                                                <?php echo e($group); ?>

                                            </button>
                                        </h3>
                                        <div id="create-permissions-<?php echo e(\Illuminate\Support\Str::slug($group)); ?>" class="accordion-collapse collapse" data-bs-parent="#createPermissionsAccordion">
                                            <div class="accordion-body vstack gap-2">
                                                <?php $__currentLoopData = $permissions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $permission => $meta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <label class="form-check">
                                                        <input class="form-check-input" type="checkbox" name="permissions[]" value="<?php echo e($permission); ?>" data-create-permission <?php if(in_array($permission, old('permissions', []), true)): echo 'checked'; endif; ?>>
                                                        <span class="form-check-label">
                                                            <span class="fw-semibold"><?php echo e($meta['label']); ?></span>
                                                            <span class="d-block text-muted small"><?php echo e($meta['description']); ?></span>
                                                        </span>
                                                    </label>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>

                            <button class="btn btn-primary"><i class="bi bi-shield-plus me-1"></i> Create profile</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-8">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex flex-column flex-lg-row justify-content-between gap-3 align-items-lg-start mb-3">
                    <div>
                        <h2 class="h5 mb-1">Edit selected profile</h2>
                        <div class="text-muted small">Only the selected profile opens here, so you can focus on one access setup at a time.</div>
                    </div>
                    <div id="selectedProfileSummary" class="text-lg-end small text-muted">
                        <?php if($selectedProfile): ?>
                            <?php echo e($selectedProfile->name); ?> · <?php echo e($selectedProfile->permissionCount()); ?> permissions · <?php echo e($selectedProfile->users_count); ?> staff
                        <?php endif; ?>
                    </div>
                </div>

                <?php $__empty_1 = true; $__currentLoopData = $profiles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $profile): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <div class="role-profile-editor <?php echo e($selectedProfile?->id === $profile->id ? '' : 'd-none'); ?>"
                        data-profile-editor="<?php echo e($profile->id); ?>"
                        data-profile-summary="<?php echo e($profile->name); ?> · <?php echo e($profile->permissionCount()); ?> permissions · <?php echo e($profile->users_count); ?> staff"
                        data-profile-name="<?php echo e($profile->name); ?>"
                        data-profile-role="<?php echo e($roles[$profile->base_role] ?? 'Custom'); ?>"
                        data-profile-status="<?php echo e($profile->is_active ? 'Active' : 'Inactive'); ?>"
                        data-profile-status-class="<?php echo e($profile->is_active ? 'text-bg-success' : 'text-bg-secondary'); ?>"
                        data-profile-permissions="<?php echo e($profile->permissionCount()); ?> permissions"
                        data-profile-staff="<?php echo e($profile->users_count); ?> staff">
                        <form method="POST" action="<?php echo e(route('tenant.role-profiles.update', $profile)); ?>">
                            <?php echo csrf_field(); ?>
                            <?php echo method_field('PATCH'); ?>
                            <div class="row g-3 mb-3">
                                <div class="col-lg-7">
                                    <label class="form-label">Profile name</label>
                                    <input name="name" value="<?php echo e($profile->name); ?>" class="form-control form-control-lg fw-semibold" required>
                                </div>
                                <div class="col-lg-5">
                                    <label class="form-label">Base role</label>
                                    <?php if(! $profile->is_system): ?>
                                        <select name="base_role" class="form-select form-select-lg">
                                            <option value="">No base role</option>
                                            <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($value); ?>" <?php if($profile->base_role === $value): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    <?php else: ?>
                                        <input type="hidden" name="base_role" value="<?php echo e($profile->base_role); ?>">
                                        <div class="form-control form-control-lg bg-light"><?php echo e($roles[$profile->base_role] ?? $profile->base_role); ?></div>
                                    <?php endif; ?>
                                </div>
                                <div class="col-12">
                                    <label class="form-label">Description</label>
                                    <textarea name="description" class="form-control" rows="2" placeholder="Profile description"><?php echo e($profile->description); ?></textarea>
                                </div>
                            </div>

                            <div class="d-flex flex-wrap justify-content-between gap-3 align-items-center mb-3">
                                <div class="d-flex flex-wrap gap-2">
                                    <?php if($profile->is_system): ?>
                                        <span class="badge text-bg-info">Default profile</span>
                                    <?php endif; ?>
                                    <span class="badge text-bg-light text-dark"><?php echo e($profile->permissionCount()); ?> permissions</span>
                                    <span class="badge text-bg-light text-dark"><?php echo e($profile->users_count); ?> staff assigned</span>
                                </div>
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" role="switch" name="is_active" value="1" id="profile-active-<?php echo e($profile->id); ?>" <?php if($profile->is_active): echo 'checked'; endif; ?>>
                                    <label class="form-check-label" for="profile-active-<?php echo e($profile->id); ?>">Active</label>
                                </div>
                            </div>

                            <ul class="nav nav-tabs" id="profile-permission-tabs-<?php echo e($profile->id); ?>" role="tablist">
                                <?php $__currentLoopData = $permissionGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group => $permissions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php ($tabId = 'profile-'.$profile->id.'-'.\Illuminate\Support\Str::slug($group)); ?>
                                    <li class="nav-item" role="presentation">
                                        <button class="nav-link <?php echo e($loop->first ? 'active' : ''); ?>" id="<?php echo e($tabId); ?>-tab" data-bs-toggle="tab" data-bs-target="#<?php echo e($tabId); ?>" type="button" role="tab">
                                            <?php echo e($group); ?>

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

                            <div class="tab-content border border-top-0 rounded-bottom p-3 mb-3">
                                <?php $__currentLoopData = $permissionGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group => $permissions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php ($tabId = 'profile-'.$profile->id.'-'.\Illuminate\Support\Str::slug($group)); ?>
                                    <div class="tab-pane fade <?php echo e($loop->first ? 'show active' : ''); ?>" id="<?php echo e($tabId); ?>" role="tabpanel">
                                        <div class="row g-3">
                                            <?php $__currentLoopData = $permissions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $permission => $meta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <div class="col-lg-6">
                                                    <label class="form-check border rounded p-3 h-100">
                                                        <input class="form-check-input" type="checkbox" name="permissions[]" value="<?php echo e($permission); ?>" <?php if(in_array($permission, $profile->permissions ?? [], true)): echo 'checked'; endif; ?>>
                                                        <span class="form-check-label">
                                                            <span class="fw-semibold"><?php echo e($meta['label']); ?></span>
                                                            <span class="d-block text-muted small"><?php echo e($meta['description']); ?></span>
                                                        </span>
                                                    </label>
                                                </div>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </div>
                                    </div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>

                            <div class="d-flex flex-wrap justify-content-between gap-2">
                                <button class="btn btn-primary"><i class="bi bi-save me-1"></i> Save profile</button>
                                <?php if(! $profile->is_system): ?>
                                    <button type="submit" form="delete-role-profile-<?php echo e($profile->id); ?>" class="btn btn-outline-danger" <?php if($profile->users_count > 0): echo 'disabled'; endif; ?>>
                                        <i class="bi bi-trash me-1"></i> Delete profile
                                    </button>
                                <?php endif; ?>
                            </div>
                        </form>

                        <?php if(! $profile->is_system): ?>
                            <form id="delete-role-profile-<?php echo e($profile->id); ?>" method="POST" action="<?php echo e(route('tenant.role-profiles.destroy', $profile)); ?>">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('DELETE'); ?>
                            </form>
                        <?php endif; ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <div class="text-muted">Create a profile to start assigning custom access to staff.</div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const defaults = <?php echo json_encode($defaultPermissionsByRole, 15, 512) ?>;
    const select = document.getElementById('roleTemplateSelect');
    const checkboxes = document.querySelectorAll('[data-create-permission]');
    const picker = document.getElementById('roleProfilePicker');
    const editors = document.querySelectorAll('[data-profile-editor]');
    const summary = document.getElementById('selectedProfileSummary');
    const selectedName = document.querySelector('[data-selected-profile-name]');
    const selectedRole = document.querySelector('[data-selected-profile-role]');
    const selectedStatus = document.querySelector('[data-selected-profile-status]');
    const selectedPermissions = document.querySelector('[data-selected-profile-permissions]');
    const selectedStaff = document.querySelector('[data-selected-profile-staff]');

    select?.addEventListener('change', () => {
        const selected = defaults[select.value] || [];
        checkboxes.forEach((checkbox) => {
            checkbox.checked = selected.includes(checkbox.value);
        });
    });

    const showProfile = (profileId) => {
        editors.forEach((editor) => {
            const active = editor.dataset.profileEditor === profileId;
            editor.classList.toggle('d-none', !active);

            if (active && summary) {
                summary.textContent = editor.dataset.profileSummary || '';
            }

            if (active) {
                if (selectedName) selectedName.textContent = editor.dataset.profileName || '';
                if (selectedRole) selectedRole.textContent = editor.dataset.profileRole || 'Custom';
                if (selectedStatus) {
                    selectedStatus.textContent = editor.dataset.profileStatus || '';
                    selectedStatus.className = `badge ${editor.dataset.profileStatusClass || 'text-bg-secondary'}`;
                }
                if (selectedPermissions) selectedPermissions.textContent = editor.dataset.profilePermissions || '';
                if (selectedStaff) selectedStaff.textContent = editor.dataset.profileStaff || '';
            }
        });

        if (picker && picker.value !== profileId) {
            picker.value = profileId;
        }

        const url = new URL(window.location.href);
        url.searchParams.set('profile', profileId);
        window.history.replaceState({}, '', url);
    };

    picker?.addEventListener('change', () => showProfile(picker.value));
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\tenant\role-profiles\index.blade.php ENDPATH**/ ?>