@php
    use App\Models\Product\ProductType;

    $agency = auth()->user()?->agency;
    $allProductTypes = ProductType::where('is_active', true)
        ->orderBy('sort_order')
        ->get();

    $agencyActiveProductTypeIds = $agency ? $agency->productTypes()
        ->where('agency_product_types.is_active', true)
        ->pluck('product_types.id')
        ->toArray() : [];

    $pendingResCount = \App\Models\Sale\Sale::byAgency(auth()->user()->currentAgencyId() ?? 0)
        ->byStatus('pending')
        ->count();
@endphp

<!-- Sidebar -->
<aside class="agency-sidebar" id="agencySidebar">
    <div class="sidebar-header">
        <div class="logo-container">
            @if($agency && $agency->logo)
                <div class="logo-wrapper">
                    <img src="{{ asset('storage/' . $agency->logo) }}" alt="Logo" class="agency-logo">
                </div>
            @else
                <div class="logo-icon">
                    <i class="fas fa-building"></i>
                </div>
            @endif
            <div class="logo-text">
                <span class="logo-title">{{ $agency?->name ?? 'Acente' }}</span>
                <span class="logo-subtitle">Yönetim Paneli</span>
            </div>
        </div>
        <button class="sidebar-collapse-btn" id="sidebarCollapseBtn">
            <i class="fas fa-chevron-left"></i>
        </button>
    </div>

    <div class="sidebar-content">
        <!-- User Section -->
        <div class="user-profile-widget">
            <div class="user-avatar-container">
                <img src="https://ui-avatars.com/api/?name={{ urlencode(auth()->user()->full_name) }}&background=2563eb&color=fff&bold=true"
                    alt="Avatar" class="user-avatar">
                <span class="status-badge pulse"></span>
            </div>
            <div class="user-info">
                <h6 class="user-name">{{ auth()->user()->full_name }}</h6>
                <p class="user-role">{{ auth()->user()->position ?? 'Acente Sahibi' }}</p>
            </div>
        </div>

        <!-- Navigation Menu -->
        <nav class="sidebar-nav">
            <div class="nav-section-label">Ana Menü</div>
            <ul class="nav-list">
                <li class="nav-item">
                    <a href="{{ route('agency.dashboard') }}"
                        class="nav-link {{ request()->routeIs('agency.dashboard') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-th-large"></i>
                        </div>
                        <span class="nav-text">Panel</span>
                    </a>
                </li>

                <!-- Site Yönetimi -->
                <li
                    class="nav-item has-dropdown {{ request()->is('agency/page*') || request()->is('agency/website*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-magic"></i>
                        </div>
                        <span class="nav-text">Site Yönetimi</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.page.index') }}" target="_blank" class="dropdown-link">
                                <i class="fas fa-edit"></i>
                                <span>Düzenle</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.page.preview') }}" target="_blank" class="dropdown-link">
                                <i class="fas fa-external-link-alt"></i>
                                <span>Önizle</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.website.settings') }}"
                                class="dropdown-link {{ request()->routeIs('agency.website.settings') ? 'active' : '' }}">
                                <i class="fas fa-sliders-h"></i>
                                <span>Site Ayarları</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <!-- Ürün Yönetimi -->
                <li
                    class="nav-item has-dropdown {{ request()->is('agency/product*') || request()->is('agency/categories*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-box-open"></i>
                        </div>
                        <span class="nav-text">Ürün Yönetimi</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        @foreach($allProductTypes as $productType)
                            @php
                                $isActive = in_array($productType->id, $agencyActiveProductTypeIds);
                                $routeSlug = $productType->route_slug;
                                $routeName = $isActive ? "agency.products.{$routeSlug}.index" : "agency.product-types.apply";
                            @endphp
                            <li>
                                <a href="{{ $isActive ? route($routeName) : route($routeName, $productType->slug) }}"
                                    class="dropdown-link {{ $isActive && request()->is("agency/products/{$routeSlug}*") ? 'active' : '' }}">
                                    <i class="fas {{ $productType->icon }} {{ $isActive ? 'text-indigo' : 'text-muted' }}"></i>
                                    <span>{{ $productType->name }}</span>
                                    @if(!$isActive)
                                        <span class="badge-status inactive">Pasif</span>
                                    @endif
                                </a>
                            </li>
                        @endforeach
                        <li class="dropdown-divider"></li>
                        <li>
                            <a href="{{ route('agency.product-types.index') }}"
                                class="dropdown-link {{ request()->routeIs('agency.product-types.index') ? 'active' : '' }}">
                                <i class="fas fa-layer-group"></i>
                                <span>Hizmet Türleri</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <!-- Tanımlamalar -->
                @if(isset($sidebarDefinitionTypes) && $sidebarDefinitionTypes->isNotEmpty())
                    <li class="nav-item has-dropdown {{ request()->is('agency/definitions*') ? 'open' : '' }}">
                        <a href="javascript:void(0)" class="nav-link">
                            <div class="nav-icon-wrapper">
                                <i class="fas fa-tags"></i>
                            </div>
                            <span class="nav-text">Tanımlamalar</span>
                            <i class="fas fa-chevron-right dropdown-arrow"></i>
                        </a>
                        <ul class="nav-dropdown">
                            @foreach($sidebarDefinitionTypes as $type)
                                <li>
                                    <a href="{{ route('agency.definitions.index', ['type' => $type->id]) }}"
                                        class="dropdown-link {{ request()->get('type') == $type->id ? 'active' : '' }}">
                                        <i class="{{ $type->icon ?? 'fas fa-circle' }}"></i>
                                        <span>{{ $type->display_name }}</span>
                                        <span class="count-badge">{{ $type->definitions->count() }}</span>
                                    </a>
                                </li>
                            @endforeach
                            <li class="dropdown-divider"></li>
                            <li>
                                <a href="{{ route('agency.definitions.index') }}" class="dropdown-link">
                                    <i class="fas fa-list"></i>
                                    <span>Tüm Tanımlar</span>
                                </a>
                            </li>
                        </ul>
                    </li>
                @endif

                <div class="nav-section-label">Operasyon</div>

                <li class="nav-item has-dropdown {{ request()->routeIs('agency.sales.*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-shopping-cart"></i>
                            @if($pendingResCount > 0)
                                <span class="nav-notification-dot"></span>
                            @endif
                        </div>
                        <span class="nav-text">Satış</span>
                        @if($pendingResCount > 0)
                            <span class="nav-badge-new">{{ $pendingResCount }}</span>
                        @endif
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.sales.index') }}"
                               class="dropdown-link {{ request()->routeIs('agency.sales.index') ? 'active' : '' }}">
                                <i class="fas fa-chart-line"></i>
                                <span>Başlangıç</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.sales.leads') }}"
                               class="dropdown-link {{ request()->routeIs('agency.sales.leads') ? 'active' : '' }}">
                                <i class="fas fa-user-clock"></i>
                                <span>Müşteri Adayları</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.sales.deals') }}"
                               class="dropdown-link {{ request()->routeIs('agency.sales.deals') ? 'active' : '' }}">
                                <i class="fas fa-handshake"></i>
                                <span>Anlaşmalar</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.sales.cancellations') }}"
                               class="dropdown-link {{ request()->routeIs('agency.sales.cancellations') ? 'active' : '' }}">
                                <i class="fas fa-undo"></i>
                                <span>İptal ve İadeler</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <li class="nav-item has-dropdown {{ request()->is('agency/customers*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-user-friends"></i>
                        </div>
                        <span class="nav-text">Müşteriler</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.customers.index', ['type' => 'individual']) }}"
                                class="dropdown-link {{ request()->is('agency/customers*') && request('type') == 'individual' ? 'active' : '' }}">
                                <i class="fas fa-user"></i>
                                <span>Kişiler</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.customers.index', ['type' => 'corporate']) }}"
                                class="dropdown-link {{ request()->is('agency/customers*') && request('type') == 'corporate' ? 'active' : '' }}">
                                <i class="fas fa-building"></i>
                                <span>Şirketler</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.customers.index') }}"
                                class="dropdown-link {{ request()->is('agency/customers*') && !request('type') ? 'active' : '' }}">
                                <i class="fas fa-users"></i>
                                <span>Tüm Müşteriler</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <li class="nav-item has-dropdown {{ request()->is('agency/suppliers*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-handshake"></i>
                        </div>
                        <span class="nav-text">Tedarikçiler</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'hotel']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'hotel' ? 'active' : '' }}">
                                <i class="fas fa-hotel"></i>
                                <span>Oteller</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'transportation']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'transportation' ? 'active' : '' }}">
                                <i class="fas fa-shuttle-van"></i>
                                <span>Taşıma Firmaları</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'restaurant']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'restaurant' ? 'active' : '' }}">
                                <i class="fas fa-utensils"></i>
                                <span>Restoran & Mekanlar</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'insurance']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'insurance' ? 'active' : '' }}">
                                <i class="fas fa-shield-alt"></i>
                                <span>Sigorta Şirketleri</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'visa']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'visa' ? 'active' : '' }}">
                                <i class="fas fa-passport"></i>
                                <span>Vize Danışmanları</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index', ['supplier_type' => 'airline']) }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && request('supplier_type') == 'airline' ? 'active' : '' }}">
                                <i class="fas fa-plane"></i>
                                <span>Havayolu Firmaları</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.suppliers.index') }}"
                                class="dropdown-link {{ request()->is('agency/suppliers*') && !request('supplier_type') ? 'active' : '' }}">
                                <i class="fas fa-truck-loading"></i>
                                <span>Tüm Tedarikçiler</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.disk.index') }}"
                        class="nav-link {{ request()->routeIs('agency.disk.*') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-hdd"></i>
                        </div>
                        <span class="nav-text">Disk</span>
                    </a>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.finance.index') }}"
                        class="nav-link {{ request()->routeIs('agency.finance.*') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-chart-line"></i>
                        </div>
                        <span class="nav-text">Finansal Raporlar</span>
                    </a>
                </li>

                <div class="nav-section-label">Ayarlar</div>

                <li class="nav-item has-dropdown {{ request()->is('agency/staff-management*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-user-shield"></i>
                        </div>
                        <span class="nav-text">Kullanıcı ve Yetki</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.staff-management.staff.index') }}"
                                class="dropdown-link {{ request()->is('agency/staff-management/staff*') ? 'active' : '' }}">
                                <i class="fas fa-user-tie"></i>
                                <span>Personeller</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.staff-management.roles.index') }}"
                                class="dropdown-link {{ request()->is('agency/staff-management/roles*') ? 'active' : '' }}">
                                <i class="fas fa-key"></i>
                                <span>Yetkilendirme</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.settings') }}"
                        class="nav-link {{ request()->routeIs('agency.settings') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-cog"></i>
                        </div>
                        <span class="nav-text">Ayarlar</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>

    <div class="sidebar-footer">
        <a href="javascript:void(0)" onclick="document.getElementById('logout-form').submit();" class="logout-btn">
            <i class="fas fa-sign-out-alt"></i>
            <span>Oturumu Kapat</span>
        </a>
        <form id="logout-form" action="{{ route('agency.logout') }}" method="POST" style="display: none;">
            @csrf
        </form>
    </div>
</aside>