@php
    /** Resolve a menu item's href + whether the current route matches it. */
    $href = function (array $item) {
        if (($item['route'] ?? '#') === '#') return '#';
        return \Illuminate\Support\Facades\Route::has($item['route']) ? route($item['route']) : '#';
    };
    $isActive = function (array $item) {
        $r = $item['route'] ?? null;
        if (! $r || $r === '#') return false;
        // active when the current route name starts with the item's base (e.g. branches.*)
        $base = explode('.', $r)[0];
        return request()->routeIs($base.'*');
    };
    $canSee = function (array $item) use (&$canSee) {
        $user = auth()->user();
        if (! empty($item['super']) && ! $user->isSuperAdmin()) return false;
        if (! empty($item['children'])) {
            foreach ($item['children'] as $child) {
                if ($canSee($child)) return true;
            }
            return isset($item['permission']) ? $user->can($item['permission']) : true;
        }
        return ! isset($item['permission']) || $user->can($item['permission']);
    };
@endphp
<aside class="sms-sidebar">
    <a href="{{ route('dashboard') }}" class="sms-brand">
        <span class="logo"><i class="bi bi-mortarboard-fill"></i></span>
        <span>{{ config('app.name') }}</span>
    </a>

    <ul class="sms-menu">
        @foreach(config('sidebar') as $item)
            @if(($item['type'] ?? null) === 'header')
                <li class="header">{{ $item['text'] }}</li>
            @elseif($canSee($item))
                @php $hasChildren = ! empty($item['children']); $active = $isActive($item); @endphp
                @if($hasChildren)
                    @php $childActive = collect($item['children'])->contains(fn($c) => $isActive($c)); @endphp
                    <li class="{{ $childActive ? 'active open' : '' }}">
                        <a href="#" data-submenu-toggle>
                            <i class="bi {{ $item['icon'] }}"></i>
                            <span>{{ $item['text'] }}</span>
                            <i class="bi bi-chevron-right caret"></i>
                        </a>
                        <ul class="submenu" @if(!$childActive) style="display:none" @endif>
                            @foreach($item['children'] as $child)
                                @if($canSee($child))
                                    <li class="{{ $isActive($child) ? 'active' : '' }}">
                                        <a href="{{ $href($child) }}">
                                            <span>{{ $child['text'] }}</span>
                                            @if(!empty($child['soon']))<span class="badge-soon">soon</span>@endif
                                        </a>
                                    </li>
                                @endif
                            @endforeach
                        </ul>
                    </li>
                @else
                    <li class="{{ $active ? 'active' : '' }}">
                        <a href="{{ $href($item) }}">
                            <i class="bi {{ $item['icon'] }}"></i>
                            <span>{{ $item['text'] }}</span>
                            @if(!empty($item['soon']))<span class="badge-soon">soon</span>@endif
                        </a>
                    </li>
                @endif
            @endif
        @endforeach
    </ul>
</aside>
