<div>
    @if($submitted)
        {{-- Success --}}
        <div class="bg-green-50 border border-green-200 rounded-2xl p-8 text-center">
            <div class="w-16 h-16 mx-auto bg-green-100 rounded-full flex items-center justify-center mb-4">
                <svg class="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
            </div>
            <h3 class="text-xl font-bold text-green-800 mb-2">{{ __('تم التسجيل بنجاح!') }}</h3>
            <p class="text-green-700 mb-4">{{ __('رقم التسجيل الخاص بك:') }}</p>
            <div class="inline-block bg-white border-2 border-green-300 rounded-xl px-6 py-3">
                <span class="text-2xl font-mono font-bold text-green-800" dir="ltr">{{ $registrationNumber }}</span>
            </div>
            <p class="text-sm text-green-600 mt-4">{{ __('يرجى الاحتفاظ بهذا الرقم للمتابعة') }}</p>
        </div>
    @else
        {{-- Registration Form --}}
        <form wire:submit="submit" class="space-y-5" x-data="eventForm(@js($event->form_fields), @js($fields))">
            @error('form')
                <div class="p-4 bg-red-50 border border-red-200 rounded-xl text-red-700 text-sm">{{ $message }}</div>
            @enderror

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                @foreach($event->form_fields as $fieldIndex => $field)
                    @php
                        $hasCondition = !empty($field['condition_field']) && !empty($field['condition_value']);
                        $colSpan = ($field['width'] ?? 'full') === 'full' || $field['type'] === 'section_divider' ? 'md:col-span-2' : '';
                    @endphp

                    {{-- Section Divider --}}
                    @if($field['type'] === 'section_divider')
                        <div class="{{ $colSpan }} pt-4 pb-1"
                             @if($hasCondition) x-show="isVisible('{{ $field['key'] }}')" x-transition @endif>
                            <h4 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-2">{{ $field['label'] }}</h4>
                            @if(!empty($field['description']))
                                <p class="text-xs text-gray-500 mt-1">{{ $field['description'] }}</p>
                            @endif
                        </div>
                        @continue
                    @endif

                    <div class="{{ $colSpan }}"
                         @if($hasCondition) x-show="isVisible('{{ $field['key'] }}')" x-transition @endif>

                        {{-- Label --}}
                        @if($field['type'] !== 'terms')
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                {{ $field['label'] }}
                                @if($field['is_required'] ?? false) <span class="text-red-500">*</span> @endif
                                @if($field['is_unique'] ?? false)
                                    <span class="text-xs text-orange-500 ms-1">({{ __('فريد') }})</span>
                                @endif
                            </label>
                        @endif

                        {{-- Description --}}
                        @if(!empty($field['description']) && $field['type'] !== 'section_divider')
                            <p class="text-xs text-gray-400 mb-1.5">{{ $field['description'] }}</p>
                        @endif

                        @switch($field['type'])
                            @case('text')
                            @case('email')
                            @case('phone')
                            @case('national_id')
                                <input type="{{ $field['type'] === 'phone' ? 'tel' : ($field['type'] === 'email' ? 'email' : 'text') }}"
                                       wire:model="fields.{{ $field['key'] }}"
                                       placeholder="{{ $field['placeholder'] ?? '' }}"
                                       @if(in_array($field['type'], ['email', 'phone', 'national_id'])) dir="ltr" @endif
                                       @if(!empty($field['max_length'])) maxlength="{{ $field['max_length'] }}" @endif
                                       @if(!empty($field['pattern'])) pattern="{{ $field['pattern'] }}" @endif
                                       class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror">
                                @if(!empty($field['max_length']))
                                    <p class="text-xs text-gray-400 mt-1 text-end" dir="ltr">
                                        <span x-text="($wire.fields['{{ $field['key'] }}'] || '').length"></span>/{{ $field['max_length'] }}
                                    </p>
                                @endif
                                @break

                            @case('number')
                                <input type="number" wire:model="fields.{{ $field['key'] }}" dir="ltr"
                                       placeholder="{{ $field['placeholder'] ?? '' }}"
                                       @if(!empty($field['min_value'])) min="{{ $field['min_value'] }}" @endif
                                       @if(!empty($field['max_value'])) max="{{ $field['max_value'] }}" @endif
                                       class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror">
                                @break

                            @case('textarea')
                                <textarea wire:model="fields.{{ $field['key'] }}" rows="3"
                                          placeholder="{{ $field['placeholder'] ?? '' }}"
                                          @if(!empty($field['max_length'])) maxlength="{{ $field['max_length'] }}" @endif
                                          class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror"></textarea>
                                @if(!empty($field['max_length']))
                                    <p class="text-xs text-gray-400 mt-1 text-end" dir="ltr">
                                        <span x-text="($wire.fields['{{ $field['key'] }}'] || '').length"></span>/{{ $field['max_length'] }}
                                    </p>
                                @endif
                                @break

                            @case('date')
                                <input type="date" wire:model="fields.{{ $field['key'] }}" dir="ltr"
                                       class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror">
                                @break

                            @case('select')
                                <select wire:model.live="fields.{{ $field['key'] }}"
                                        class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror">
                                    <option value="">{{ $field['placeholder'] ?? __('اختر...') }}</option>
                                    @foreach($field['options'] ?? [] as $option)
                                        <option value="{{ $option['value'] }}">{{ $option['label'] }}</option>
                                    @endforeach
                                    @if($field['allow_other'] ?? false)
                                        <option value="__other__">{{ __('أخرى') }}</option>
                                    @endif
                                </select>
                                @if($field['allow_other'] ?? false)
                                    <div x-show="$wire.fields['{{ $field['key'] }}'] === '__other__'" x-transition class="mt-2">
                                        <input type="text" wire:model="fields.{{ $field['key'] }}_other" placeholder="{{ __('حدد...') }}"
                                               class="w-full px-4 py-2.5 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                @endif
                                @break

                            @case('radio')
                                <div class="space-y-2 @error('fields.'.$field['key']) ring-1 ring-red-400 rounded-xl p-2 @enderror">
                                    @foreach($field['options'] ?? [] as $option)
                                        <label class="flex items-center gap-2.5 cursor-pointer p-2 rounded-lg hover:bg-gray-50 transition-colors">
                                            <input type="radio" wire:model.live="fields.{{ $field['key'] }}" value="{{ $option['value'] }}"
                                                   class="text-blue-600 focus:ring-blue-500">
                                            <span class="text-sm text-gray-700">{{ $option['label'] }}</span>
                                        </label>
                                    @endforeach
                                    @if($field['allow_other'] ?? false)
                                        <label class="flex items-center gap-2.5 cursor-pointer p-2 rounded-lg hover:bg-gray-50 transition-colors">
                                            <input type="radio" wire:model.live="fields.{{ $field['key'] }}" value="__other__"
                                                   class="text-blue-600 focus:ring-blue-500">
                                            <span class="text-sm text-gray-700">{{ __('أخرى') }}</span>
                                        </label>
                                        <div x-show="$wire.fields['{{ $field['key'] }}'] === '__other__'" x-transition class="ms-6">
                                            <input type="text" wire:model="fields.{{ $field['key'] }}_other" placeholder="{{ __('حدد...') }}"
                                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                        </div>
                                    @endif
                                </div>
                                @break

                            @case('multi_select')
                                <div class="space-y-2 border border-gray-300 rounded-xl p-3 @error('fields.'.$field['key']) border-red-500 @enderror">
                                    @foreach($field['options'] ?? [] as $option)
                                        <label class="flex items-center gap-2.5 cursor-pointer p-1.5 rounded-lg hover:bg-gray-50 transition-colors">
                                            <input type="checkbox" wire:model="fields.{{ $field['key'] }}" value="{{ $option['value'] }}"
                                                   class="rounded text-blue-600 focus:ring-blue-500">
                                            <span class="text-sm text-gray-700">{{ $option['label'] }}</span>
                                        </label>
                                    @endforeach
                                    @if($field['allow_other'] ?? false)
                                        <label class="flex items-center gap-2.5 cursor-pointer p-1.5 rounded-lg hover:bg-gray-50 transition-colors">
                                            <input type="checkbox" wire:model="fields.{{ $field['key'] }}" value="__other__"
                                                   class="rounded text-blue-600 focus:ring-blue-500">
                                            <span class="text-sm text-gray-700">{{ __('أخرى') }}</span>
                                        </label>
                                        <div x-show="($wire.fields['{{ $field['key'] }}'] || []).includes('__other__')" x-transition class="ms-6">
                                            <input type="text" wire:model="fields.{{ $field['key'] }}_other" placeholder="{{ __('حدد...') }}"
                                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                        </div>
                                    @endif
                                </div>
                                @break

                            @case('checkbox')
                                <label class="flex items-center gap-2.5 cursor-pointer">
                                    <input type="checkbox" wire:model="fields.{{ $field['key'] }}" class="rounded text-blue-600 focus:ring-blue-500">
                                    <span class="text-sm text-gray-700">{{ $field['placeholder'] ?? $field['label'] }}</span>
                                </label>
                                @break

                            @case('terms')
                                <label class="flex items-start gap-2.5 cursor-pointer">
                                    <input type="checkbox" wire:model="fields.{{ $field['key'] }}" class="rounded text-blue-600 focus:ring-blue-500 mt-0.5">
                                    <span class="text-sm text-gray-700">
                                        {{ $field['label'] }}
                                        @if($field['is_required'] ?? false) <span class="text-red-500">*</span> @endif
                                        @if(!empty($field['terms_url']))
                                            <a href="{{ $field['terms_url'] }}" target="_blank" rel="noopener" class="text-blue-600 underline hover:text-blue-800">{{ __('اقرأ الشروط') }}</a>
                                        @endif
                                    </span>
                                </label>
                                @break

                            @case('location')
                                <input type="text" wire:model="fields.{{ $field['key'] }}" dir="ltr"
                                       placeholder="{{ $field['placeholder'] ?? __('أدخل رابط الموقع أو الإحداثيات') }}"
                                       class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 @error('fields.'.$field['key']) border-red-500 @enderror">
                                @break

                            @case('file_upload')
                                <input type="file" wire:model="fields.{{ $field['key'] }}"
                                       class="w-full px-4 py-3 border border-gray-300 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 file:me-3 file:py-1 file:px-3 file:rounded-lg file:border-0 file:text-sm file:bg-blue-50 file:text-blue-700 @error('fields.'.$field['key']) border-red-500 @enderror">
                                @break
                        @endswitch

                        @error('fields.'.$field['key']) <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                    </div>
                @endforeach
            </div>

            <button type="submit" wire:loading.attr="disabled" wire:target="submit"
                    class="w-full py-3.5 bg-blue-600 text-white rounded-xl font-medium hover:bg-blue-700 transition-colors disabled:opacity-50">
                <span wire:loading.remove wire:target="submit">{{ __('تسجيل') }}</span>
                <span wire:loading wire:target="submit">{{ __('جارٍ التسجيل...') }}</span>
            </button>
        </form>

        <script>
            function eventForm(formFields, fields) {
                return {
                    formFields: formFields,
                    isVisible(fieldKey) {
                        const field = this.formFields.find(f => f.key === fieldKey);
                        if (!field || !field.condition_field || !field.condition_value) return true;
                        const condVal = this.$wire.fields[field.condition_field];
                        if (condVal === null || condVal === undefined || condVal === '') return false;
                        if (Array.isArray(condVal)) return condVal.includes(field.condition_value);
                        return String(condVal) === String(field.condition_value);
                    }
                };
            }
        </script>
    @endif
</div>
