<nav x-data="{
    open: false,
    sidebarOpen: () => { try { return $store.sidebar.open } catch { return false } }
}"
class="bg-white text-gray-800 border-b border-gray-200 fixed top-0 left-0 right-0 z-40 transition-all duration-300"
:style="sidebarOpen() ? 'padding-right: 16rem;' : 'padding-right: 0;' ">
    <!-- Primary Navigation Menu -->
    <div class="max-w-7xl m-auto p-2 sm:p-3 lg:p-4">
        <div class="flex justify-between h-12">
            <div class="flex">
                <!-- Sidebar Toggle -->
                <div class="flex items-center" x-data="{
                    sidebarOpen: () => { try { return $store.sidebar.open } catch { return false } },
                    toggleSidebar: () => { try { $store.sidebar.toggle() } catch { console.log('Sidebar store not available yet') } }
                }">
                    <button @click="toggleSidebar()" class="text-gray-700 hover:text-red-700 focus:outline-none transition-colors duration-200 p-2 rounded-md hover:bg-gray-100">
                        <svg x-show="sidebarOpen()" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 19l-7-7 7-7m8 14l-7-7 7-7" />
                        </svg>
                        <svg x-show="!sidebarOpen()" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 5l7 7-7 7M5 5l7 7-7 7" />
                        </svg>
                    </button>
                </div>

                <!-- Logo -->
                <div class="shrink-0 flex items-center mr-4">
                    <a href="{{ route('dashboard') }}">
                        <x-application-logo class="block h-6 w-auto fill-current text-gray-800" />
                    </a>
                </div>

                <!-- Navigation Links -->
                <div class="hidden space-y-8 sm:-my-px sm:mr-10 sm:ml-10 sm:flex">
                    <x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')" class="text-gray-800 hover:text-red-800">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1 inline-block" viewBox="0 0 20 20" fill="currentColor">
                            <path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z" />
                        </svg>
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('dashboard.title', __('dashboard.title')) }}
                    </x-nav-link>

                    <x-nav-link :href="route('employees.index')" :active="request()->routeIs('employees*')" class="text-gray-800 hover:text-red-800">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1 inline-block" viewBox="0 0 20 20" fill="currentColor">
                            <path d="M13 6a3 3 0 11-6 0 3 3 0 016 0zM18 8a2 2 0 11-4 0 2 2 0 014 0zM14 15a4 4 0 00-8 0v3h8v-3zM6 8a2 2 0 11-4 0 2 2 0 014 0zM16 18v-3a5.972 5.972 0 00-.75-2.906A3.005 3.005 0 0119 15v3h-3zM4.75 12.094A5.973 5.973 0 004 15v3H1v-3a3 3 0 013.75-2.906z" />
                        </svg>
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('navigation.hr', __('navigation.hr')) }}
                    </x-nav-link>
                    <x-nav-link :href="route('accounting.chart-of-accounts.index')" :active="request()->routeIs('accounting.*')" class="text-gray-800 hover:text-red-800">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1 inline-block" viewBox="0 0 20 20" fill="currentColor">
                            <path d="M8.433 7.418c.155-.103.346-.196.567-.267v1.698a2.305 2.305 0 01-.567-.267C8.07 8.34 8 8.114 8 8c0-.114.07-.34.433-.582zM11 12.849v-1.698c.22.071.412.164.567.267.364.243.433.468.433.582 0 .114-.07.34-.433.582a2.305 2.305 0 01-.567.267z" />
                        </svg>
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('navigation.accounting', __('navigation.accounting')) }}
                    </x-nav-link>
                </div>
            </div>

            <!-- Settings Dropdown -->
            <div class="hidden sm:flex sm:items-center ltr:sm:ml-6 rtl:sm:mr-6 gap-4">
                <!-- Language Switcher -->
                <x-dropdown align="right" width="48">
                    <x-slot name="trigger">
                        <button class="flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 focus:outline-none transition duration-150 ease-in-out">
                            @php
                            $currentLocale = session('locale', 'ar');
                            $languages = \App\Models\Language::active()->orderBy('sort_order')->get();
                            $currentLang = $languages->firstWhere('code', $currentLocale);
                            @endphp
                            <span class="text-lg mr-2">{{ $currentLang?->flag_emoji ?? '🌐' }}</span>
                            <span class="hidden md:inline">{{ $currentLang?->code ?? 'AR' }}</span>
                        </button>
                    </x-slot>

                    <x-slot name="content">
                        @foreach($languages as $lang)
                        <a href="{{ route('localization.set-language', $lang->code) }}"
                           class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 w-full text-left @if(session('locale', 'ar') == $lang->code) bg-gray-100 font-semibold @endif">
                            <span class="text-lg mr-2">{{ $lang->flag_emoji }}</span>
                            {{ $lang->name }}
                        </a>
                        @endforeach
                    </x-slot>
                </x-dropdown>

                <!-- User Menu -->
                <x-dropdown align="right" width="48">
                    <x-slot name="trigger">
                        <button class="flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
                            <div>{{ Auth::user()->name }}</div>

                            <div class="ml-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')">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('common.profile', __('common.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();">
                               {{ \App\Helpers\TranslationHelper::transFromDatabase('common.logout', __('common.logout')) }}

                            </x-dropdown-link>
                        </form>
                    </x-slot>
                </x-dropdown>
            </div>

            <!-- Hamburger -->
            <div class="-mr-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-1 pb-2 space-y-0">
            <x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')" class="text-gray-800 hover:text-red-800">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1 inline-block" viewBox="0 0 20 20" fill="currentColor">
                    <path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z" />
                </svg>
                {{ \App\Helpers\TranslationHelper::transFromDatabase('dashboard.title', __('dashboard.title')) }}
            </x-responsive-nav-link>
        </div>

        <!-- Responsive Settings Options -->
        <div class="pt-2 pb-1 border-t border-gray-200">
            <div class="px-4 py-2">
                <p class="text-xs font-medium text-gray-500 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Choose Language', 'اختر اللغة:') }}:</p>
                <div class="flex gap-2 flex-wrap">
                    @php
                    $currentLocale = session('locale', 'ar');
                    $languages = \App\Models\Language::active()->orderBy('sort_order')->get();
                    @endphp
                    @foreach($languages as $lang)
                    <a href="{{ route('localization.set-language', $lang->code) }}"
                       class="px-2 py-1 text-xs rounded @if($currentLocale == $lang->code) bg-red-600 text-white font-semibold @else bg-gray-100 text-gray-800 hover:bg-gray-200 @endif transition-colors">
                        {{ $lang->flag_emoji }} {{ $lang->code }}
                    </a>
                    @endforeach
                </div>
            </div>

            <div class="px-4 pt-2">
                <div class="text-sm font-medium text-gray-800">{{ Auth::user()->name }}</div>
                <div class="text-xs font-medium text-gray-500">{{ Auth::user()->email }}</div>
            </div>

            <div class="mt-2 space-y-0">
                <x-responsive-nav-link :href="route('profile.edit')">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('common.profile', __('common.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();">
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('common.logout', __('common.logout')) }}
                    </x-responsive-nav-link>
                </form>
            </div>
        </div>
    </div>
</nav>
