{{-- 侧边栏菜单项渲染组件 — 支持 header/submenu/route/url/can/target --}}

{{-- 菜单分组标题 --}}
@if(isset($item['header']))
    <li class="nav-header" role="heading" aria-level="2">{{ $item['header'] }}</li>

{{-- 子菜单 --}}
@elseif(isset($item['submenu']) && is_array($item['submenu']))
    @php
        $hasActive = false;
        foreach ($item['submenu'] as $sub) {
            $subRoute = $sub['route'] ?? null;
            $subUrl = $sub['url'] ?? null;
            $subActive = $sub['active'] ?? null;
            if ($subRoute && request()->routeIs($subRoute)) {
                $hasActive = true;
                break;
            }
            if ($subUrl && request()->is(trim($subUrl, '/'))) {
                $hasActive = true;
                break;
            }
            if ($subActive && request()->is(trim($subActive, '/'))) {
                $hasActive = true;
                break;
            }
        }
    @endphp
    <li class="nav-item {{ $hasActive ? 'menu-open' : '' }}">
        <a href="#" class="nav-link {{ $hasActive ? 'active' : '' }}">
            @if(isset($item['icon']))
                <i class="nav-icon {{ $item['icon'] }}"></i>
            @endif
            <p>
                {{ $item['text'] ?? '' }}
                <i class="right fas fa-angle-left"></i>
            </p>
        </a>
        <ul class="nav nav-treeview">
            @foreach($item['submenu'] as $sub)
                {{-- 子菜单权限检查 --}}
                @if(isset($sub['can']))
                    @can($sub['can'])
                        @includeIf('kylin-adminlte::partials.sidebar-subitem', ['item' => $sub])
                    @endcan
                    @continue
                @endif
                @includeIf('kylin-adminlte::partials.sidebar-subitem', ['item' => $sub])
            @endforeach
        </ul>
    </li>

{{-- 普通菜单项 --}}
@else
    @php
        $url = '#';
        $isActive = false;

        if (isset($item['route'])) {
            $url = route($item['route']);
            $isActive = request()->routeIs($item['route']);
        } elseif (isset($item['url'])) {
            $url = $item['url'];
            $isActive = request()->is(trim($item['url'], '/'));
        }

        if (isset($item['active'])) {
            $isActive = $isActive || request()->is(trim($item['active'], '/'));
        }
    @endphp
    <li class="nav-item">
        <a href="{{ $url }}"
           class="nav-link {{ $isActive ? 'active' : '' }}"
           @if(isset($item['target'])) target="{{ $item['target'] }}" rel="noopener noreferrer" @endif
           aria-label="{{ $item['text'] ?? '' }}">
            @if(isset($item['icon']))
                <i class="nav-icon {{ $item['icon'] }}"></i>
            @endif
            <p>{{ $item['text'] ?? '' }}</p>
        </a>
    </li>
@endif