<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ $editing ? __('تعديل منشأة') : __('إضافة منشأة جديدة') }}</h1>
        <a href="{{ route('facilities.list') }}" wire:navigate class="text-sm text-gray-500 hover:text-gray-700">{{ __('← العودة') }}</a>
    </div>

    {{-- Flash Messages --}}
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-50 border border-red-200 text-red-700 rounded-lg text-sm">
            {{ session('error') }}
        </div>
    @endif

    <form wire:submit="save" class="space-y-6">
        {{-- Basic Info --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h2 class="text-base sm:text-lg font-semibold text-gray-700 mb-4">{{ __('المعلومات الأساسية') }}</h2>
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 sm:gap-4">
                {{-- Name AR --}}
                <div>
                    <label for="name_ar" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الاسم بالعربية') }} *</label>
                    <input type="text" id="name_ar" wire:model="name_ar"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('name_ar') border-red-500 @enderror">
                    @error('name_ar') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Name EN --}}
                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الاسم بالإنجليزية') }}</label>
                    <input type="text" id="name" wire:model="name" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('name') border-red-500 @enderror">
                    @error('name') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Code --}}
                <div>
                    <label for="code" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الكود') }}</label>
                    <input type="text" id="code" wire:model="code" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('code') border-red-500 @enderror"
                           placeholder="{{ __('اختياري') }}">
                    @error('code') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Branch. With a single selectable branch there is nothing to
                     choose — the topbar switcher already decided it. --}}
                <div>
                    <label for="branch_id" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الفرع') }} *</label>
                    @if($branches->count() > 1)
                        <select id="branch_id" wire:model="branch_id"
                                class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('branch_id') border-red-500 @enderror">
                            <option value="">{{ __('اختر الفرع') }}</option>
                            @foreach($branches as $branch)
                                <option value="{{ $branch->id }}">{{ $branch->name_ar }}</option>
                            @endforeach
                        </select>
                    @else
                        <p id="branch_id" class="w-full text-sm px-4 py-2.5 border border-gray-200 bg-gray-50 rounded-lg text-gray-700">
                            {{ $branches->first()?->name_ar ?? __('لا يوجد فرع نشط') }}
                        </p>
                    @endif
                    @error('branch_id') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Type --}}
                <div>
                    <label for="type" class="block text-sm font-medium text-gray-700 mb-1">{{ __('النوع') }} *</label>
                    <select id="type" wire:model="type"
                            class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('type') border-red-500 @enderror">
                        @foreach($typeOptions as $key => $label)
                            <option value="{{ $key }}">{{ $label }}</option>
                        @endforeach
                    </select>
                    @error('type') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Status --}}
                <div>
                    <label for="status" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الحالة') }} *</label>
                    <select id="status" wire:model="status"
                            class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('status') border-red-500 @enderror">
                        @foreach($statusOptions as $key => $label)
                            <option value="{{ $key }}">{{ $label }}</option>
                        @endforeach
                    </select>
                    @error('status') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Description AR --}}
                <div class="sm:col-span-2">
                    <label for="description_ar" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الوصف بالعربية') }}</label>
                    <textarea id="description_ar" wire:model="description_ar" rows="2"
                              class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>

                {{-- Description EN --}}
                <div class="sm:col-span-2">
                    <label for="description" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الوصف بالإنجليزية') }}</label>
                    <textarea id="description" wire:model="description" rows="2" dir="ltr"
                              class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>
            </div>
        </div>

        {{-- Specifications --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h2 class="text-base sm:text-lg font-semibold text-gray-700 mb-4">{{ __('المواصفات') }}</h2>
            <div class="grid grid-cols-2 sm:grid-cols-3 gap-3 sm:gap-4">
                {{-- Capacity --}}
                <div>
                    <label for="capacity" class="block text-sm font-medium text-gray-700 mb-1">{{ __('السعة') }}</label>
                    <input type="number" id="capacity" wire:model="capacity" min="1" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('capacity') border-red-500 @enderror">
                    @error('capacity') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Surface Type --}}
                <div>
                    <label for="surface_type" class="block text-sm font-medium text-gray-700 mb-1">{{ __('نوع السطح') }}</label>
                    <input type="text" id="surface_type" wire:model="surface_type"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="{{ __('مثال: نجيل صناعي') }}">
                </div>

                {{-- Area --}}
                <div>
                    <label for="area_sqm" class="block text-sm font-medium text-gray-700 mb-1">{{ __('المساحة (م²)') }}</label>
                    <input type="number" id="area_sqm" wire:model="area_sqm" min="0" step="0.01" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('area_sqm') border-red-500 @enderror">
                    @error('area_sqm') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Length --}}
                <div>
                    <label for="length_m" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الطول (م)') }}</label>
                    <input type="number" id="length_m" wire:model="length_m" min="0" step="0.01" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('length_m') border-red-500 @enderror">
                    @error('length_m') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Width --}}
                <div>
                    <label for="width_m" class="block text-sm font-medium text-gray-700 mb-1">{{ __('العرض (م)') }}</label>
                    <input type="number" id="width_m" wire:model="width_m" min="0" step="0.01" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('width_m') border-red-500 @enderror">
                    @error('width_m') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Rental Cost per Hour --}}
                <div>
                    <label for="rental_cost_display" class="block text-sm font-medium text-gray-700 mb-1">{{ __('تكلفة الإيجار/ساعة (ج.م)') }}</label>
                    <input type="number" id="rental_cost_display" wire:model="rental_cost_display" min="0" step="0.01" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('rental_cost_display') border-red-500 @enderror">
                    @error('rental_cost_display') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Monthly Rental Cost --}}
                <div>
                    <label for="monthly_rental_cost_display" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الإيجار الشهري (ج.م)') }}</label>
                    <input type="number" id="monthly_rental_cost_display" wire:model="monthly_rental_cost_display" min="0" step="0.01" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('monthly_rental_cost_display') border-red-500 @enderror"
                           placeholder="{{ __('المبلغ الذي تدفعه شهرياً لاستئجار هذه المنشأة') }}">
                    @error('monthly_rental_cost_display') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>

            {{-- Boolean Options --}}
            <div class="flex flex-wrap gap-4 sm:gap-6 mt-4 pt-4 border-t border-gray-100">
                <label class="min-h-[44px] flex items-center gap-2 cursor-pointer">
                    <input type="checkbox" wire:model="is_indoor"
                           class="w-4 h-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                    <span class="text-sm text-gray-700">{{ __('مغطى (داخلي)') }}</span>
                </label>
                <label class="min-h-[44px] flex items-center gap-2 cursor-pointer">
                    <input type="checkbox" wire:model="has_lighting"
                           class="w-4 h-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                    <span class="text-sm text-gray-700">{{ __('يوجد إضاءة') }}</span>
                </label>
                <label class="min-h-[44px] flex items-center gap-2 cursor-pointer">
                    <input type="checkbox" wire:model="has_ac"
                           class="w-4 h-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                    <span class="text-sm text-gray-700">{{ __('يوجد تكييف') }}</span>
                </label>
            </div>
        </div>

        {{-- Operating Hours & Location --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h2 class="text-base sm:text-lg font-semibold text-gray-700 mb-4">{{ __('ساعات العمل والموقع') }}</h2>
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 sm:gap-4">
                {{-- Operating Start --}}
                <div>
                    <label for="operating_start" class="block text-sm font-medium text-gray-700 mb-1">{{ __('وقت بداية التشغيل') }}</label>
                    <input type="time" id="operating_start" wire:model="operating_start" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('operating_start') border-red-500 @enderror">
                    @error('operating_start') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Operating End --}}
                <div>
                    <label for="operating_end" class="block text-sm font-medium text-gray-700 mb-1">{{ __('وقت نهاية التشغيل') }}</label>
                    <input type="time" id="operating_end" wire:model="operating_end" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('operating_end') border-red-500 @enderror">
                    @error('operating_end') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Address --}}
                <div class="sm:col-span-2">
                    <label for="address" class="block text-sm font-medium text-gray-700 mb-1">{{ __('العنوان') }}</label>
                    <input type="text" id="address" wire:model="address"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('address') border-red-500 @enderror">
                    @error('address') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Latitude --}}
                <div>
                    <label for="latitude" class="block text-sm font-medium text-gray-700 mb-1">{{ __('خط العرض') }}</label>
                    <input type="number" id="latitude" wire:model="latitude" step="0.0000001" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('latitude') border-red-500 @enderror"
                           placeholder="30.0444196">
                    @error('latitude') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Longitude --}}
                <div>
                    <label for="longitude" class="block text-sm font-medium text-gray-700 mb-1">{{ __('خط الطول') }}</label>
                    <input type="number" id="longitude" wire:model="longitude" step="0.0000001" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('longitude') border-red-500 @enderror"
                           placeholder="31.2357116">
                    @error('longitude') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                {{-- Sort Order --}}
                <div>
                    <label for="sort_order" class="block text-sm font-medium text-gray-700 mb-1">{{ __('الترتيب') }}</label>
                    <input type="number" id="sort_order" wire:model="sort_order" min="0" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('sort_order') border-red-500 @enderror">
                    @error('sort_order') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
        </div>

        {{-- Sports hosted here --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <div class="flex items-start gap-2 mb-1">
                <x-ui.icon name="bolt" class="w-5 h-5 text-blue-600 shrink-0 mt-0.5" />
                <div>
                    <h2 class="text-base font-bold text-gray-800">{{ __('الرياضات في هذه المنشأة') }}</h2>
                    <p class="text-xs text-gray-500">
                        {{ __('تُستخدم لتصفية المجموعات في جدولة المنشآت — اتركها فارغة لعرض كل الرياضات') }}
                    </p>
                </div>
            </div>

            <div class="flex flex-wrap gap-2 mt-4">
                @foreach($activities as $activity)
                    <label class="cursor-pointer">
                        <input type="checkbox" value="{{ $activity->id }}" wire:model="activityIds" class="peer sr-only">
                        <span class="inline-block px-3 py-1.5 rounded-full border-2 text-sm font-medium
                                     border-gray-200 text-gray-600
                                     peer-checked:border-blue-500 peer-checked:bg-blue-50 peer-checked:text-blue-700">
                            {{ $activity->name_ar }}
                        </span>
                    </label>
                @endforeach
                @if($activities->isEmpty())
                    <p class="text-sm text-gray-400">{{ __('لا توجد أنشطة معرّفة بعد') }}</p>
                @endif
            </div>
        </div>

        {{-- Grid size --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <div class="flex items-start gap-2 mb-1">
                <x-ui.icon name="grid" class="w-5 h-5 text-blue-600 shrink-0 mt-0.5" />
                <div>
                    <h2 class="text-base font-bold text-gray-800">{{ __('تقسيم المساحة') }}</h2>
                    <p class="text-xs text-gray-500">
                        {{ __('عدد الصفوف والأعمدة التي تُقسم بها المنشأة فعلياً — بدون حد أقصى') }}
                    </p>
                </div>
            </div>

            <div class="grid grid-cols-2 sm:grid-cols-3 gap-4 mt-4">
                <div>
                    <label for="gridRows" class="block text-sm font-medium text-gray-700 mb-1">{{ __('عدد الصفوف') }}</label>
                    <input type="number" id="gridRows" wire:model.live="gridRows" min="1" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label for="gridColumns" class="block text-sm font-medium text-gray-700 mb-1">{{ __('عدد الأعمدة') }}</label>
                    <input type="number" id="gridColumns" wire:model.live="gridColumns" min="1" dir="ltr"
                           class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div class="flex items-end">
                    <p class="text-sm text-gray-600 pb-2.5">
                        {{ __('الناتج') }}:
                        <span class="font-bold text-gray-900">{{ max(1, (int) $gridRows) * max(1, (int) $gridColumns) }}</span>
                        {{ __('قطعة') }}
                    </p>
                </div>
            </div>

            {{-- Live preview of the grid being described --}}
            @php
                $pr = min(max(1, (int) $gridRows), 12);
                $pc = min(max(1, (int) $gridColumns), 12);
            @endphp
            <div class="mt-4">
                <div class="inline-grid gap-1 p-2 bg-gray-50 border border-gray-200 rounded-lg"
                     style="grid-template-columns: repeat({{ $pc }}, minmax(0, 1fr));">
                    @for($r = 1; $r <= $pr; $r++)
                        @for($c = 1; $c <= $pc; $c++)
                            <div class="w-9 h-9 rounded bg-white border border-blue-200 flex items-center justify-center
                                        text-[10px] text-blue-700 font-medium" dir="ltr">R{{ $r }}C{{ $c }}</div>
                        @endfor
                    @endfor
                </div>
                @if($pr < (int) $gridRows || $pc < (int) $gridColumns)
                    <p class="text-xs text-gray-500 mt-2">
                        {{ __('المعاينة تعرض أول 12×12 فقط — سيتم إنشاء كل القطع عند الحفظ') }}
                    </p>
                @endif
            </div>

            @if($gridError)
                <div class="mt-3 p-3 bg-amber-50 border border-amber-200 rounded-lg text-sm text-amber-800">
                    {{ $gridError }}
                </div>
            @endif
        </div>

        {{-- Actions --}}
        <div class="flex flex-col-reverse sm:flex-row items-stretch sm:items-center justify-end gap-3">
            <a href="{{ route('facilities.list') }}" wire:navigate
               class="text-center px-4 sm:px-6 py-2.5 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors text-sm font-medium">{{ __('إلغاء') }}</a>
            <button type="submit" wire:loading.attr="disabled" wire:target="save"
                    class="text-center px-4 sm:px-6 py-2.5 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 disabled:bg-blue-400 transition-colors text-sm">
                <span wire:loading.remove wire:target="save">{{ $editing ? __('حفظ التعديلات') : __('إنشاء المنشأة') }}</span>
                <span wire:loading wire:target="save">{{ __('جارٍ الحفظ...') }}</span>
            </button>
        </div>
    </form>
</div>
