@php // Determine if the current dealer (from session or user) is the default dealer. static $currentDealerIsDefault = null; if ($currentDealerIsDefault === null) { $currentDealerIsDefault = false; $dealerIdFromSession = session('current_dealer_id'); $dealerId = $dealerIdFromSession ?: auth()->user()->dealer_id; if ($dealerId) { $dealer = \App\Models\Dealer::query()->select(['id', 'is_default'])->find($dealerId); $currentDealerIsDefault = (bool) optional($dealer)->is_default; } } $defaultDealerOnly = (bool) ($item['default_dealer_only'] ?? false); $canView = empty($item['permission_key']) || auth()->user()->can($item['permission_key']); if ($canView && $defaultDealerOnly && !$currentDealerIsDefault) { $canView = false; } $children = $item['children'] ?? []; $hasChildren = !empty($children); $isGroup = (bool) ($item['is_group'] ?? false); $routeName = $item['route_name'] ?? null; $routePrefix = $item['route_prefix'] ?? null; $routeExists = is_string($routeName) && $routeName !== '' && Route::has($routeName); $canViewNode = function (array $node) use (&$canViewNode): bool { $nodeCanView = empty($node['permission_key']) || auth()->user()->can($node['permission_key']); if (!$nodeCanView) { return false; } $nodeChildren = $node['children'] ?? []; if (empty($nodeChildren)) { return true; } foreach ($nodeChildren as $nodeChild) { if ($canViewNode($nodeChild)) { return true; } } // Non-group parents with a route remain visible even if all children are hidden. return !((bool) ($node['is_group'] ?? false)) && !empty($node['route_name']); }; $visibleChildren = array_values(array_filter($children, $canViewNode)); $hasVisibleChildren = !empty($visibleChildren); $isNodeActive = function (array $node) use (&$isNodeActive): bool { $nodeRoute = $node['route_name'] ?? null; if (is_string($nodeRoute) && $nodeRoute !== '' && request()->routeIs($nodeRoute)) { return true; } foreach (($node['children'] ?? []) as $childNode) { if ($isNodeActive($childNode)) { return true; } } return false; }; $groupActive = $hasChildren && $isNodeActive($item); $leafActive = !empty($routeName) && request()->routeIs($routeName); $parentActive = $isNodeActive($item); $depth = (int) ($depth ?? 0); $keyPath = (string) ($keyPath ?? 'root'); $nodeKey = (string) ($item['key'] ?? $routeName ?? 'item_'.$depth); $currentKeyPath = trim($keyPath.'.'.$nodeKey, '.'); $leftPadding = 10 + ($depth * 12); $rightPadding = 10; $toggleKey = str_replace('.', '_', $currentKeyPath); @endphp @php $isDashboardRoot = is_string($routeName) && $routeName === 'dashboard.index'; $canCollapseDashboard = $isDashboardRoot && ( auth()->user()->hasSystemAdminRole() || auth()->user()->hasAnyRole(['Managing Director', 'Branch Manager', 'Financial Manager', 'F&I']) ); $effectiveHasChildren = $hasVisibleChildren && (!$isDashboardRoot || $canCollapseDashboard); @endphp @if($canView) @if($effectiveHasChildren)