<!-- Top Header (Dashboard) -->
<header class="bg-white px-4 md:px-6 py-2 flex flex-wrap md:flex-nowrap items-center justify-between border-b border-gray-200 shadow-sm sticky top-0 z-50 w-full gap-2 md:gap-0">
    @php
        $user = auth()->user();
        $myCompanies = collect();
        $activeCompanyName = 'Admin';

        if ($user->role_id == 1) {
            $myCompanies = \App\Models\Company::where('superadmin_id', $user->id)->get();
            $activeCompany = $myCompanies->where('user_id', session('active_company_id'))->first();
            if ($activeCompany) {
                $activeCompanyName = $activeCompany->company_name;
            } elseif ($myCompanies->isNotEmpty()) {
                $activeCompanyName = $myCompanies->first()->company_name;
            }
        } else {
            // For Company Admin, their company_id IS their user_id, so we find their company profile
            $activeCompany = \App\Models\Company::where('user_id', $user->id)->first();
            if ($activeCompany) {
                $activeCompanyName = $activeCompany->company_name;
            }
        }
    @endphp

    <div class="flex items-center space-x-4">
        <div class="flex flex-col text-[#ff5722] font-bold text-2xl leading-none tracking-tighter">
            <div class="flex items-center">
                <i class="fa-solid fa-chart-column mr-1 text-gray-500 text-xl"></i> VITXPERT
            </div>
            <span class="text-[10px] text-gray-500 font-normal italic mt-1 tracking-normal">Power of Biometrics</span>
        </div>
        <div class="pl-2 md:pl-4 border-l-2 border-gray-200 flex flex-col justify-center max-w-[150px] md:max-w-none overflow-hidden">
            <h1 class="text-lg font-bold text-gray-900 leading-tight">{{ Auth::user()->company->name ?? 'Vitxpert India (P) Limited' }}</h1>
            
            @if($user->role_id == 1 && $myCompanies->isNotEmpty())
                <form action="{{ route('admin.switch_company') }}" method="POST" class="inline-block mt-0.5 relative" id="switch-company-form">
                    @csrf
                    <select name="company_id" onchange="document.getElementById('switch-company-form').submit()" class="text-sm font-bold text-gray-800 bg-transparent border-none focus:ring-0 cursor-pointer p-0 pr-4 appearance-none hover:text-[#ff5722] transition relative z-10 w-full">
                        @foreach($myCompanies as $c)
                            <option value="{{ $c->id }}" {{ session('active_company_id') == $c->user_id ? 'selected' : '' }}>
                                {{ $c->company_name }}
                            </option>
                        @endforeach
                    </select>
                    <i class="fa-solid fa-caret-down text-gray-800 text-xs absolute right-0 top-1/2 transform -translate-y-1/2 pointer-events-none z-0 mt-0.5"></i>
                </form>
            @else
                <p class="text-sm font-bold text-gray-800 mt-0.5">{{ $activeCompanyName }}</p>
            @endif
        </div>
    </div>
    <div class="flex items-center space-x-4 md:space-x-6 w-full md:w-auto justify-end mt-2 md:mt-0">
        <div class="relative hidden sm:block">
            <input type="text" placeholder="Search Employee" class="border border-gray-300 rounded-full py-1 px-4 pr-10 text-sm focus:outline-none focus:border-[#ff5722] w-48 md:w-64 shadow-inner text-gray-600">
            <i class="fa-solid fa-search absolute right-3 top-2 text-[#ff5722] text-sm"></i>
        </div>
        <!-- Top Navbar Icons -->
        <div class="flex items-center space-x-4">
            <div class="relative">
                <i class="fa-solid fa-location-dot text-[#ff5722] text-xl"></i>
                <span class="absolute -top-2 -right-2 bg-red-500 text-white text-[10px] font-bold px-1.5 py-0.5 rounded-full shadow-sm">0</span>
            </div>
            <div class="relative">
                <i class="fa-solid fa-envelope text-green-500 text-xl"></i>
                <span class="absolute -top-2 -right-2 bg-red-500 text-white text-[10px] font-bold px-1.5 py-0.5 rounded-full shadow-sm">1</span>
            </div>
            
            <!-- Logout Form -->
            <form id="logout-form" action="{{ route('logout') }}" method="POST" class="hidden">
                @csrf
            </form>
            
            <button onclick="openLogoutModal()" class="text-[#ff5722] hover:text-[#e64a19] transition">
                <i class="fa-solid fa-power-off text-xl"></i>
            </button>
        </div>
    </div>
</header>
