@php
    $cols = [
        '1' => '',
        '2' => 'sm:grid-cols-2',
        '3' => 'sm:grid-cols-2 lg:grid-cols-3',
        '4' => 'sm:grid-cols-2 lg:grid-cols-4',
    ][$block->get('columns', '3')] ?? 'sm:grid-cols-2 lg:grid-cols-3';

    $ar = app()->getLocale() === 'ar';
    $season = $variant === 'season_cards';
    $showSeason = $season && $block->get('show_season', true);
    $today = now()->startOfDay();

    // A branch with no declared window is open all year: the absence of dates
    // must never read as "closed".
    $inSeason = function ($b) use ($today) {
        $from = filled($b->season_starts_on ?? null) ? \Illuminate\Support\Carbon::parse($b->season_starts_on)->startOfDay() : null;
        $to = filled($b->season_ends_on ?? null) ? \Illuminate\Support\Carbon::parse($b->season_ends_on)->startOfDay() : null;

        if (! $from && ! $to) {
            return true;
        }

        return ! (($from && $today->lt($from)) || ($to && $today->gt($to)));
    };
@endphp

<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"
     @if ($block->get('show_search')) x-data="{ q: '' }" @endif>
    @include('website.blocks._header')

    @if ($block->get('show_search'))
        <div class="max-w-md mx-auto mb-8">
            <input type="search" x-model="q"
                   placeholder="{{ __('ابحث بالاسم أو المنطقة') }}"
                   class="ec-surface w-full rounded-full px-5 py-3 focus:outline-none focus:ring-2"
                   aria-label="{{ __('بحث في الفروع') }}">
        </div>
    @endif

    @if ($items->isEmpty())
        @include('website.blocks._empty')
    @else
        <div class="grid grid-cols-1 {{ $cols }} gap-6">
            @foreach ($items as $branch)
                @php
                    $name = $ar ? ($branch->name_ar ?? $branch->name) : ($branch->name ?? $branch->name_ar);
                    $loc = $branch->address ?? $branch->location ?? null;
                    $photo = $branch->photo_path ?? null;
                    // A branch reads as dormant when it is switched off, or when
                    // today falls outside its declared season.
                    $open = ($branch->is_active ?? true) && $inSeason($branch);
                    $from = filled($branch->season_starts_on ?? null) ? \Illuminate\Support\Carbon::parse($branch->season_starts_on) : null;
                    $to = filled($branch->season_ends_on ?? null) ? \Illuminate\Support\Carbon::parse($branch->season_ends_on) : null;
                @endphp

                <article class="ec-stagger-item ec-surface relative flex flex-col overflow-hidden rounded-2xl shadow-sm transition hover:-translate-y-1
                                {{ $season ? 'border-t-2 border-t-[var(--site-accent)]' : '' }}
                                {{ $showSeason && ! $open ? 'ec-card-dormant' : '' }}"
                         @if ($block->get('show_search')) x-show="!q || @js(mb_strtolower($name.' '.$loc)).includes(q.toLowerCase())" @endif>

                    @if ($block->get('show_image', true) && $photo)
                        <img src="{{ $photo }}" alt="{{ $name }}" loading="lazy" class="w-full aspect-[3/2] object-cover">
                    @endif

                    <div class="flex flex-1 flex-col gap-2 p-6">
                        <div class="flex items-start gap-3">
                            @if ($season)
                                <span class="ec-accent inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-xl"
                                      style="background: color-mix(in srgb, var(--site-accent) 15%, transparent)" aria-hidden="true">
                                    <x-website.icon name="building-office" class="w-5 h-5" />
                                </span>
                            @endif
                            <div class="min-w-0">
                                <h3 class="ec-heading text-lg font-extrabold leading-tight">{{ $name }}</h3>
                                @if ($block->get('show_location', true) && $loc)
                                    @if ($season)
                                        <span class="ec-accent mt-1.5 inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-[0.7rem] font-bold uppercase tracking-wide"
                                              style="background: color-mix(in srgb, var(--site-accent) 18%, transparent)">
                                            <x-website.icon name="map-pin" class="w-3.5 h-3.5" />
                                            {{ $loc }}
                                        </span>
                                    @else
                                        <p class="ec-muted mt-1 flex items-start gap-2 text-sm">
                                            <x-website.icon name="map-pin" class="w-4 h-4 mt-0.5 shrink-0" />
                                            <span>{{ $loc }}</span>
                                        </p>
                                    @endif
                                @endif
                            </div>
                        </div>

                        @if ($block->get('show_phone') && ($branch->phone ?? null))
                            <p class="ec-muted flex items-center gap-2 text-sm" dir="ltr">
                                <x-website.icon name="phone" class="w-4 h-4 shrink-0" />
                                <a href="tel:{{ $branch->phone }}">{{ $branch->phone }}</a>
                            </p>
                        @endif

                        @if ($block->get('show_manager') && ($branch->manager_name ?? null))
                            <p class="ec-muted text-sm">{{ __('المسؤول') }}: {{ $branch->manager_name }}</p>
                        @endif

                        @if ($showSeason && ! $open && ($from || $to))
                            <p class="ec-muted mt-2 text-sm font-medium" dir="ltr">
                                {{ __('يفتح') }}
                                {{ $from?->format('d/m/Y') }}@if ($from && $to) – @endif{{ $to?->format('d/m/Y') }}
                            </p>
                        @endif

                        @if ($season && $block->get('card_hint'))
                            <p class="ec-muted mt-auto pt-4 text-center text-sm">{{ $block->get('card_hint') }}</p>
                        @endif
                    </div>
                </article>
            @endforeach
        </div>
    @endif

    @include('website.blocks._footer_buttons')
</div>
