<nav x-data="{ open: false, fleetOpen: false, financeOpen: false, reportsOpen: false, systemOpen: false }" class="bg-white border-b border-gray-100">
    <!-- Primary Navigation Menu -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between h-16">
            <div class="flex">
                <!-- Logo -->
                <div class="shrink-0 flex items-center">
                    <a href="{{ route('dashboard') }}">
                        <x-application-logo class="block h-9 w-auto fill-current text-gray-800" />
                    </a>
                </div>

                <!-- Navigation Links -->
                <div class="hidden space-x-4 sm:-my-px sm:ms-10 sm:flex items-center">
                    <x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
                        {{ __('Dashboard') }}
                        @if(isset($globalNotificationCount) && $globalNotificationCount > 0)
                            <span class="ml-1 inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                {{ $globalNotificationCount }}
                            </span>
                        @endif
                    </x-nav-link>
                    
                    @hasanyrole('Admin|Staff')
                        {{-- Finance Dropdown --}}
                        <div class="relative" x-data="{ ddOpen: false }" @click.away="ddOpen = false">
                            <button @click="ddOpen = !ddOpen" class="inline-flex items-center px-1 pt-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none
                                {{ request()->routeIs('trips.*') || request()->routeIs('fuel.*') || request()->routeIs('payments.*') ? 'border-b-2 border-indigo-400 text-gray-900' : 'border-b-2 border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' }}">
                                {{ __('Operations') }}
                                <svg class="ml-1 h-4 w-4 transition-transform" :class="ddOpen ? 'rotate-180' : ''" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/></svg>
                            </button>
                            <div x-show="ddOpen" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95"
                                class="absolute left-0 z-50 mt-2 w-48 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:none;">
                                <div class="py-1">
                                    <a href="{{ route('trips.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('trips.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">🚛 Trips</a>
                                    <a href="{{ route('fuel.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('fuel.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">⛽ Fuel</a>
                                    <a href="{{ route('payments.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('payments.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">💰 Payments</a>
                                </div>
                            </div>
                        </div>
                    @endhasanyrole

                    @hasrole('Admin')
                        {{-- Manage Dropdown --}}
                        <div class="relative" x-data="{ ddOpen: false }" @click.away="ddOpen = false">
                            <button @click="ddOpen = !ddOpen" class="inline-flex items-center px-1 pt-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none
                                {{ request()->routeIs('vehicles.*') || request()->routeIs('drivers.*') || request()->routeIs('maintenances.*') || request()->routeIs('admin.driver-users.*') ? 'border-b-2 border-indigo-400 text-gray-900' : 'border-b-2 border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' }}">
                                {{ __('Manage') }}
                                <svg class="ml-1 h-4 w-4 transition-transform" :class="ddOpen ? 'rotate-180' : ''" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/></svg>
                            </button>
                            <div x-show="ddOpen" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95"
                                class="absolute left-0 z-50 mt-2 w-52 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:none;">
                                <div class="py-1">
                                    <a href="{{ route('vehicles.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('vehicles.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">🚗 Vehicles</a>
                                    <a href="{{ route('drivers.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('drivers.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">👤 Drivers</a>
                                    <a href="{{ route('admin.driver-users.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('admin.driver-users.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">🔑 Driver Accounts</a>
                                    <hr class="my-1">
                                    <a href="{{ route('maintenances.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('maintenances.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">🔧 Maintenance</a>
                                </div>
                            </div>
                        </div>

                        {{-- Reports Dropdown --}}
                        <div class="relative" x-data="{ ddOpen: false }" @click.away="ddOpen = false">
                            <button @click="ddOpen = !ddOpen" class="inline-flex items-center px-1 pt-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none
                                {{ request()->routeIs('reports.*') || request()->routeIs('ledgers.*') ? 'border-b-2 border-indigo-400 text-gray-900' : 'border-b-2 border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' }}">
                                {{ __('Reports') }}
                                <svg class="ml-1 h-4 w-4 transition-transform" :class="ddOpen ? 'rotate-180' : ''" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/></svg>
                            </button>
                            <div x-show="ddOpen" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95"
                                class="absolute left-0 z-50 mt-2 w-48 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:none;">
                                <div class="py-1">
                                    <a href="{{ route('reports.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('reports.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">📊 Reports</a>
                                    <a href="{{ route('ledgers.vehicle') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('ledgers.vehicle') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">📒 Vehicle Ledger</a>
                                    <a href="{{ route('ledgers.driver') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('ledgers.driver') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">📒 Driver Ledger</a>
                                </div>
                            </div>
                        </div>

                        {{-- System Dropdown --}}
                        <div class="relative" x-data="{ ddOpen: false }" @click.away="ddOpen = false">
                            <button @click="ddOpen = !ddOpen" class="inline-flex items-center px-1 pt-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none
                                {{ request()->routeIs('backups.*') || request()->routeIs('settings.*') || request()->routeIs('activity-logs.*') || request()->routeIs('login-history.*') ? 'border-b-2 border-indigo-400 text-gray-900' : 'border-b-2 border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' }}">
                                {{ __('System') }}
                                <svg class="ml-1 h-4 w-4 transition-transform" :class="ddOpen ? 'rotate-180' : ''" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/></svg>
                            </button>
                            <div x-show="ddOpen" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95"
                                class="absolute left-0 z-50 mt-2 w-48 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:none;">
                                <div class="py-1">
                                    <a href="{{ route('settings.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('settings.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">⚙️ Settings</a>
                                    <a href="{{ route('backups.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('backups.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">💾 Backups</a>
                                    <a href="{{ route('activity-logs.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('activity-logs.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">📋 Activity Logs</a>
                                    <a href="{{ route('login-history.index') }}" class="block px-4 py-2 text-sm {{ request()->routeIs('login-history.*') ? 'bg-gray-100 text-gray-900 font-semibold' : 'text-gray-700 hover:bg-gray-50' }}">🕐 Login History</a>
                                </div>
                            </div>
                        </div>
                    @endhasrole
                    
                    @hasrole('Driver')
                        <x-nav-link :href="route('trips.my')" :active="request()->routeIs('trips.my')">
                            {{ __('My Trips') }}
                        </x-nav-link>
                        <x-nav-link :href="route('trips.create')" :active="request()->routeIs('trips.create')">
                            {{ __('Add Trip') }}
                        </x-nav-link>
                    @endhasrole
                </div>
            </div>

            <!-- Settings Dropdown -->
            <div class="hidden sm:flex sm:items-center sm:ms-6">
                <x-dropdown align="right" width="48">
                    <x-slot name="trigger">
                        <button class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-gray-500 bg-white hover:text-gray-700 focus:outline-none transition ease-in-out duration-150">
                            <div>{{ Auth::user()->name }}</div>

                            <div class="ms-1">
                                <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
                                </svg>
                            </div>
                        </button>
                    </x-slot>

                    <x-slot name="content">
                        <x-dropdown-link :href="route('profile.edit')">
                            {{ __('Profile') }}
                        </x-dropdown-link>

                        <!-- Authentication -->
                        <form method="POST" action="{{ route('logout') }}">
                            @csrf

                            <x-dropdown-link :href="route('logout')"
                                    onclick="event.preventDefault();
                                                this.closest('form').submit();">
                                {{ __('Log Out') }}
                            </x-dropdown-link>
                        </form>
                    </x-slot>
                </x-dropdown>
            </div>

            <!-- Hamburger -->
            <div class="-me-2 flex items-center sm:hidden">
                <button @click="open = ! open" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out">
                    <svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
                        <path :class="{'hidden': open, 'inline-flex': ! open }" class="inline-flex" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
                        <path :class="{'hidden': ! open, 'inline-flex': open }" class="hidden" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </button>
            </div>
        </div>
    </div>

    <!-- Responsive Navigation Menu -->
    <div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
        <div class="pt-2 pb-3 space-y-1">
            <x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
                {{ __('Dashboard') }}
            </x-responsive-nav-link>
            
            @hasanyrole('Admin|Staff')
                <div class="px-4 pt-2 pb-1"><span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Operations</span></div>
                <x-responsive-nav-link :href="route('trips.index')" :active="request()->routeIs('trips.*')">
                    🚛 {{ __('Trips') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('fuel.index')" :active="request()->routeIs('fuel.*')">
                    ⛽ {{ __('Fuel') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('payments.index')" :active="request()->routeIs('payments.*')">
                    💰 {{ __('Payments') }}
                </x-responsive-nav-link>
            @endhasanyrole

            @hasrole('Admin')
                <div class="px-4 pt-3 pb-1 border-t border-gray-100"><span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Manage</span></div>
                <x-responsive-nav-link :href="route('vehicles.index')" :active="request()->routeIs('vehicles.*')">
                    🚗 {{ __('Vehicles') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('drivers.index')" :active="request()->routeIs('drivers.*')">
                    👤 {{ __('Drivers') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('admin.driver-users.index')" :active="request()->routeIs('admin.driver-users.*')">
                    🔑 {{ __('Driver Accounts') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('maintenances.index')" :active="request()->routeIs('maintenances.*')">
                    🔧 {{ __('Maintenance') }}
                </x-responsive-nav-link>

                <div class="px-4 pt-3 pb-1 border-t border-gray-100"><span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Reports</span></div>
                <x-responsive-nav-link :href="route('reports.index')" :active="request()->routeIs('reports.*')">
                    📊 {{ __('Reports') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('ledgers.vehicle')" :active="request()->routeIs('ledgers.vehicle')">
                    📒 {{ __('Vehicle Ledger') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('ledgers.driver')" :active="request()->routeIs('ledgers.driver')">
                    📒 {{ __('Driver Ledger') }}
                </x-responsive-nav-link>

                <div class="px-4 pt-3 pb-1 border-t border-gray-100"><span class="text-xs font-bold uppercase text-gray-400 tracking-wider">System</span></div>
                <x-responsive-nav-link :href="route('settings.index')" :active="request()->routeIs('settings.*')">
                    ⚙️ {{ __('Settings') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('backups.index')" :active="request()->routeIs('backups.*')">
                    💾 {{ __('Backups') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('activity-logs.index')" :active="request()->routeIs('activity-logs.*')">
                    📋 {{ __('Activity Logs') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('login-history.index')" :active="request()->routeIs('login-history.*')">
                    🕐 {{ __('Login History') }}
                </x-responsive-nav-link>
            @endhasrole
            
            @hasrole('Driver')
                <x-responsive-nav-link :href="route('trips.my')" :active="request()->routeIs('trips.my')">
                    {{ __('My Trips') }}
                </x-responsive-nav-link>
                <x-responsive-nav-link :href="route('trips.create')" :active="request()->routeIs('trips.create')">
                    {{ __('Add Trip') }}
                </x-responsive-nav-link>
            @endhasrole
        </div>

        <!-- Responsive Settings Options -->
        <div class="pt-4 pb-1 border-t border-gray-200">
            <div class="px-4">
                <div class="font-medium text-base text-gray-800">{{ Auth::user()->name }}</div>
                <div class="font-medium text-sm text-gray-500">{{ Auth::user()->email }}</div>
            </div>

            <div class="mt-3 space-y-1">
                <x-responsive-nav-link :href="route('profile.edit')">
                    {{ __('Profile') }}
                </x-responsive-nav-link>

                <!-- Authentication -->
                <form method="POST" action="{{ route('logout') }}">
                    @csrf

                    <x-responsive-nav-link :href="route('logout')"
                            onclick="event.preventDefault();
                                        this.closest('form').submit();">
                        {{ __('Log Out') }}
                    </x-responsive-nav-link>
                </form>
            </div>
        </div>
    </div>
</nav>
