<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('branches.list') }}" wire:navigate class="text-sm text-gray-500 hover:text-gray-700">
            ← العودة للقائمة
        </a>
    </div>

    <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">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">الاسم بالعربية *</label>
                    <input type="text" 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 @error('name_ar') border-red-500 @enderror">
                    @error('name_ar') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">الاسم بالإنجليزية *</label>
                    <input type="text" 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 @error('name') border-red-500 @enderror">
                    @error('name') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">كود الفرع *</label>
                    <input type="text" 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 @error('code') border-red-500 @enderror" placeholder="e.g. BR001">
                    @error('code') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">المدير</label>
                    <select wire:model="manager_id" class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">— بدون مدير —</option>
                        @foreach($managers as $manager)
                            <option value="{{ $manager->id }}">{{ $manager->name_ar ?? $manager->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 sm:gap-4 mt-4">
                <label class="min-h-[44px] flex items-center gap-2 cursor-pointer">
                    <input type="checkbox" wire:model="is_main" class="w-4 h-4 rounded border-gray-300 text-blue-600">
                    <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="is_active" class="w-4 h-4 rounded border-gray-300 text-blue-600">
                    <span class="text-sm text-gray-700">نشط</span>
                </label>
            </div>

            {{-- A branch whose takings never pass through the system. Nobody
                 enrolled here is billed at all, and the branch's income arrives
                 afterwards as one figure on the external-revenue screen. --}}
            <div class="mt-4 pt-4 border-t border-gray-100">
                <label class="min-h-[44px] flex items-start gap-2 cursor-pointer">
                    <input type="checkbox" wire:model="billing_handled_externally" class="mt-1 w-4 h-4 rounded border-gray-300 text-amber-600">
                    <span>
                        <span class="block text-sm font-medium text-gray-700">{{ __('الإيرادات بتتحصل خارج النظام') }}</span>
                        <span class="block text-xs text-gray-500 mt-0.5">{{ __('مفيش فواتير اشتراك بتتعمل لأي حد في الفرع ده، والإيراد بيتسجل بعدين من شاشة "تسجيل إيراد خارجي".') }}</span>
                    </span>
                </label>
            </div>
        </div>

        <!-- Contact 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">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">الهاتف</label>
                    <input type="text" wire:model="phone" 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">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">البريد الإلكتروني</label>
                    <input type="email" wire:model="email" 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">
                    @error('email') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1">العنوان</label>
                    <input type="text" 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">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">المدينة</label>
                    <input type="text" wire:model="city" class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
            </div>
        </div>

        <!-- 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">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">خط العرض</label>
                    <input type="text" wire:model="latitude" 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" placeholder="30.0444">
                    @error('latitude') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">خط الطول</label>
                    <input type="text" wire:model="longitude" 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" placeholder="31.2357">
                    @error('longitude') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
        </div>

        <!-- Operating Hours -->
        <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="space-y-3">
                @php
                    $dayNames = [
                        'saturday' => 'السبت',
                        'sunday' => 'الأحد',
                        'monday' => 'الإثنين',
                        'tuesday' => 'الثلاثاء',
                        'wednesday' => 'الأربعاء',
                        'thursday' => 'الخميس',
                        'friday' => 'الجمعة',
                    ];
                @endphp
                @foreach($dayNames as $key => $dayLabel)
                <div class="flex flex-col sm:flex-row sm:items-center gap-2 sm:gap-4 py-2 border-b border-gray-100 last:border-0">
                    <label class="min-h-[44px] flex items-center gap-2 w-32">
                        <input type="checkbox"
                               wire:model="operating_hours.{{ $key }}.closed"
                               class="w-4 h-4 rounded border-gray-300 text-red-600"
                               value="1">
                        <span class="text-sm font-medium text-gray-700">{{ $dayLabel }}</span>
                    </label>
                    <div class="flex items-center gap-2" x-show="!$wire.operating_hours?.{{ $key }}?.closed">
                        <input type="time" wire:model="operating_hours.{{ $key }}.open" dir="ltr"
                               class="px-3 py-2.5 sm:py-1.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500">
                        <span class="text-gray-400 text-sm">—</span>
                        <input type="time" wire:model="operating_hours.{{ $key }}.close" dir="ltr"
                               class="px-3 py-2.5 sm:py-1.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500">
                    </div>
                    <span class="text-sm text-red-500" x-show="$wire.operating_hours?.{{ $key }}?.closed">مغلق</span>
                </div>
                @endforeach
            </div>
        </div>

        <!-- Submit -->
        <div class="flex flex-col-reverse sm:flex-row items-stretch sm:items-center justify-end gap-3">
            <a href="{{ route('branches.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 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 text-sm">
                <span wire:loading.remove wire:target="save">{{ $editing ? 'حفظ التعديلات' : 'إنشاء الفرع' }}</span>
                <span wire:loading wire:target="save">جارٍ الحفظ...</span>
            </button>
        </div>
    </form>
</div>
