@php
    $branches = $data['branches'] ?? collect();
@endphp

@if($branches->count())
<div class="site-section site-section--dark">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="text-center mb-12 reveal">
            <h2 class="section-title mx-auto">{{ $section->title ?? 'فروعنا' }}</h2>
            @if($section->subtitle)
                <p class="mt-4 text-white/60 text-lg">{{ $section->subtitle }}</p>
            @endif
        </div>

        {{-- Map --}}
        @if($section->getSetting('show_map', true) && $branches->where('latitude', '!=', null)->count())
            <div class="map-container mb-10 reveal" id="branches-map"></div>
            <script>
                document.addEventListener('DOMContentLoaded', function() {
                    if (typeof L === 'undefined') return;
                    const map = L.map('branches-map').setView([{{ $branches->first()->latitude ?? 30.0444 }}, {{ $branches->first()->longitude ?? 31.2357 }}], 11);
                    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                        attribution: '&copy; OpenStreetMap'
                    }).addTo(map);

                    @foreach($branches->where('latitude', '!=', null) as $branch)
                        L.marker([{{ $branch->latitude }}, {{ $branch->longitude }}])
                            .addTo(map)
                            .bindPopup('<strong>{{ $branch->name_ar }}</strong><br>{{ $branch->address }}');
                    @endforeach
                });
            </script>
            <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
            <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
        @endif

        {{-- Branch Cards --}}
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            @foreach($branches as $branch)
                @php
                    $branchImage = \App\Domain\Website\Models\Media::withoutGlobalScope('academy')
                        ->where('academy_id', $academy->id)
                        ->where('collection', 'branch_photo')
                        ->where('mediable_type', 'branch')
                        ->where('mediable_id', $branch->id)
                        ->first();
                @endphp
                <div class="site-card reveal reveal--delay-{{ ($loop->index % 3) + 1 }}">
                    @if($branchImage)
                        <div class="relative overflow-hidden aspect-[3/2]">
                            <img src="{{ $branchImage->url }}"
                                 alt="{{ $branch->name_ar }}"
                                 class="site-card__image">
                        </div>
                    @endif

                    <div class="p-5">
                        <div class="flex items-start justify-between">
                            <h3 class="text-lg font-bold text-white">{{ $branch->name_ar }}</h3>
                            @if($branch->is_main)
                                <span class="px-2 py-0.5 text-xs rounded-full bg-[var(--site-accent)]/10 text-[var(--site-accent)] border border-[var(--site-accent)]/20">
                                    رئيسي
                                </span>
                            @endif
                        </div>

                        @if($branch->address)
                            <p class="mt-2 text-white/50 text-sm flex items-start gap-2">
                                <svg class="w-4 h-4 mt-0.5 shrink-0 text-[var(--site-accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                                {{ $branch->address }}
                            </p>
                        @endif

                        @if($branch->phone)
                            <p class="mt-2 text-white/50 text-sm flex items-center gap-2">
                                <svg class="w-4 h-4 shrink-0 text-[var(--site-accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
                                <span dir="ltr">{{ $branch->phone }}</span>
                            </p>
                        @endif
                    </div>
                </div>
            @endforeach
        </div>
    </div>
</div>
@endif
