@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();

    // Görevler rozeti: kullanıcının açık görevleri (geciken dahil, arşiv hariç)
    $openTaskCount = \App\Models\Task\Task::byAgency(auth()->user()->currentAgencyId() ?? 0)
        ->notArchived()
        ->assignedTo(auth()->id())
        ->open()
        ->count();
    $overdueTaskCount = \App\Models\Task\Task::byAgency(auth()->user()->currentAgencyId() ?? 0)
        ->notArchived()
        ->assignedTo(auth()->id())
        ->overdue()
        ->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>

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

                <li class="nav-item">
                    <a href="{{ route('agency.tasks.index') }}"
                        class="nav-link {{ request()->routeIs('agency.tasks.*') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-list-check"></i>
                            @if($overdueTaskCount > 0)
                                <span class="nav-notification-dot"></span>
                            @endif
                        </div>
                        <span class="nav-text">Görevler</span>
                        @if($openTaskCount > 0)
                            <span class="nav-badge-new">{{ $openTaskCount }}</span>
                        @endif
                    </a>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.chat.page') }}" target="_blank" rel="noopener"
                        class="nav-link {{ request()->routeIs('agency.chat.page') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-comment-dots"></i>
                        </div>
                        <span class="nav-text">Mesajlarım</span>
                        <i class="fas fa-arrow-up-right-from-square nav-ext-icon" style="margin-left:auto;font-size:.7rem;opacity:.6"></i>
                    </a>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.mail.index') }}" target="_blank" rel="noopener"
                        class="nav-link {{ request()->routeIs('agency.mail.index') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-envelope"></i>
                        </div>
                        <span class="nav-text">Mail</span>
                        <i class="fas fa-arrow-up-right-from-square nav-ext-icon" style="margin-left:auto;font-size:.7rem;opacity:.6"></i>
                    </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">
                    <a href="{{ route('agency.suppliers.index') }}"
                        class="nav-link {{ request()->is('agency/suppliers*') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-handshake"></i>
                        </div>
                        <span class="nav-text">Tedarikçiler</span>
                    </a>
                </li>

                <li class="nav-item has-dropdown {{ request()->is('agency/resources*') ? 'open' : '' }}">
                    <a href="javascript:void(0)" class="nav-link">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-cubes"></i>
                        </div>
                        <span class="nav-text">Kaynak Yönetimi</span>
                        <i class="fas fa-chevron-right dropdown-arrow"></i>
                    </a>
                    <ul class="nav-dropdown">
                        <li>
                            <a href="{{ route('agency.resources.index') }}"
                                class="dropdown-link {{ request()->routeIs('agency.resources.index') ? 'active' : '' }}">
                                <i class="fas fa-list"></i>
                                <span>Kaynak Listesi</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.resources.calendar') }}"
                                class="dropdown-link {{ request()->routeIs('agency.resources.calendar') ? 'active' : '' }}">
                                <i class="fas fa-calendar-alt"></i>
                                <span>Müsaitlik & Planlama</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.resources.my-schedule') }}"
                                class="dropdown-link {{ request()->routeIs('agency.resources.my-schedule') ? 'active' : '' }}">
                                <i class="fas fa-calendar-check"></i>
                                <span>Takvimim</span>
                            </a>
                        </li>
                        <li>
                            <a href="{{ route('agency.guide-registry.index') }}"
                                class="dropdown-link {{ request()->routeIs('agency.guide-registry.*') ? 'active' : '' }}">
                                <i class="fas fa-address-book"></i>
                                <span>TUREB Rehber Sicili</span>
                            </a>
                        </li>
                    </ul>
                </li>

                <li class="nav-item">
                    <a href="{{ route('agency.operations.index') }}"
                        class="nav-link {{ request()->is('agency/operations*') ? 'active' : '' }}">
                        <div class="nav-icon-wrapper">
                            <i class="fas fa-clipboard-list"></i>
                        </div>
                        <span class="nav-text">Operasyon Yönetimi</span>
                    </a>
                </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>
                        <li>
                            <a href="{{ route('agency.staff-management.task-rules.index') }}"
                                class="dropdown-link {{ request()->is('agency/staff-management/task-rules*') ? 'active' : '' }}">
                                <i class="fas fa-robot"></i>
                                <span>Görev Atama Kuralları</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>

    <form id="logout-form" action="{{ route('agency.logout') }}" method="POST" style="display: none;">
        @csrf
    </form>
</aside>