<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-6" 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 flex items-center gap-2">
                    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"/></svg>
                    {{ $message }}
                </div>
            @enderror

            <div class="space-y-5">
                @foreach($event->form_fields as $fieldIndex => $field)
                    @php
                        $hasCondition = !empty($field['condition_field']) && !empty($field['condition_value']);
                    @endphp

                    {{-- Section Divider --}}
                    @if($field['type'] === 'section_divider')
                        <div class="pt-3 pb-1"
                             @if($hasCondition) x-show="isVisible('{{ $field['key'] }}')" x-transition @endif>
                            <div class="flex items-center gap-3">
                                <div class="h-px flex-1 bg-gray-200"></div>
                                <h4 class="text-sm font-bold text-gray-700 uppercase tracking-wide">{{ $field['label'] }}</h4>
                                <div class="h-px flex-1 bg-gray-200"></div>
                            </div>
                            @if(!empty($field['description']))
                                <p class="text-xs text-gray-500 mt-1.5 text-center">{{ $field['description'] }}</p>
                            @endif
                        </div>
                        @continue
                    @endif

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

                        {{-- Label --}}
                        @if($field['type'] !== 'terms')
                            <label class="flex items-center gap-1.5 text-sm font-semibold text-gray-800">
                                {{ $field['label'] }}
                                @if($field['is_required'] ?? false)
                                    <span class="text-red-500 text-xs">*</span>
                                @endif
                                @if($field['is_unique'] ?? false)
                                    <span class="inline-flex items-center gap-0.5 text-[10px] text-amber-600 bg-amber-50 px-1.5 py-0.5 rounded-full font-medium">
                                        <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                                        {{ __('فريد') }}
                                    </span>
                                @endif
                            </label>
                        @endif

                        {{-- Description --}}
                        @if(!empty($field['description']) && $field['type'] !== 'section_divider')
                            <p class="text-xs text-gray-500">{{ $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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                @if(!empty($field['max_length']))
                                    <p class="text-[11px] text-gray-400 mt-0.5 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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow resize-none @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror"></textarea>
                                @if(!empty($field['max_length']))
                                    <p class="text-[11px] text-gray-400 mt-0.5 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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                @break

                            @case('select')
                                <select wire:model.live="fields.{{ $field['key'] }}"
                                        class="w-full px-4 py-3 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)]">
                                    </div>
                                @endif
                                @break

                            @case('radio')
                                <div class="bg-white border border-gray-200 rounded-xl overflow-hidden divide-y divide-gray-100 shadow-sm @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                    @foreach($field['options'] ?? [] as $option)
                                        <label class="flex items-center gap-3 cursor-pointer px-4 py-3 hover:bg-gray-50 transition-colors">
                                            <input type="radio" wire:model.live="fields.{{ $field['key'] }}" value="{{ $option['value'] }}"
                                                   class="w-4 h-4 text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)]">
                                            <span class="text-sm text-gray-800 font-medium">{{ $option['label'] }}</span>
                                        </label>
                                    @endforeach
                                    @if($field['allow_other'] ?? false)
                                        <label class="flex items-center gap-3 cursor-pointer px-4 py-3 hover:bg-gray-50 transition-colors">
                                            <input type="radio" wire:model.live="fields.{{ $field['key'] }}" value="__other__"
                                                   class="w-4 h-4 text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)]">
                                            <span class="text-sm text-gray-800 font-medium">{{ __('أخرى') }}</span>
                                        </label>
                                        <div x-show="$wire.fields['{{ $field['key'] }}'] === '__other__'" x-transition class="px-4 py-3 bg-gray-50">
                                            <input type="text" wire:model="fields.{{ $field['key'] }}_other" placeholder="{{ __('حدد...') }}"
                                                   class="w-full px-3 py-2 bg-white text-gray-900 border border-gray-200 rounded-lg text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)]">
                                        </div>
                                    @endif
                                </div>
                                @break

                            @case('multi_select')
                                <div class="bg-white border border-gray-200 rounded-xl overflow-hidden divide-y divide-gray-100 shadow-sm @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                    @foreach($field['options'] ?? [] as $option)
                                        <label class="flex items-center gap-3 cursor-pointer px-4 py-3 hover:bg-gray-50 transition-colors">
                                            <input type="checkbox" wire:model="fields.{{ $field['key'] }}" value="{{ $option['value'] }}"
                                                   class="w-4 h-4 rounded text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)]">
                                            <span class="text-sm text-gray-800 font-medium">{{ $option['label'] }}</span>
                                        </label>
                                    @endforeach
                                    @if($field['allow_other'] ?? false)
                                        <label class="flex items-center gap-3 cursor-pointer px-4 py-3 hover:bg-gray-50 transition-colors">
                                            <input type="checkbox" wire:model="fields.{{ $field['key'] }}" value="__other__"
                                                   class="w-4 h-4 rounded text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)]">
                                            <span class="text-sm text-gray-800 font-medium">{{ __('أخرى') }}</span>
                                        </label>
                                        <div x-show="($wire.fields['{{ $field['key'] }}'] || []).includes('__other__')" x-transition class="px-4 py-3 bg-gray-50">
                                            <input type="text" wire:model="fields.{{ $field['key'] }}_other" placeholder="{{ __('حدد...') }}"
                                                   class="w-full px-3 py-2 bg-white text-gray-900 border border-gray-200 rounded-lg text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)]">
                                        </div>
                                    @endif
                                </div>
                                @break

                            @case('checkbox')
                                <label class="flex items-center gap-3 cursor-pointer bg-white border border-gray-200 rounded-xl px-4 py-3 shadow-sm hover:bg-gray-50 transition-colors">
                                    <input type="checkbox" wire:model="fields.{{ $field['key'] }}"
                                           class="w-4 h-4 rounded text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)]">
                                    <span class="text-sm text-gray-800 font-medium">{{ $field['placeholder'] ?? $field['label'] }}</span>
                                </label>
                                @break

                            @case('terms')
                                <label class="flex items-start gap-3 cursor-pointer bg-amber-50 border border-amber-200 rounded-xl px-4 py-3.5">
                                    <input type="checkbox" wire:model="fields.{{ $field['key'] }}"
                                           class="w-4 h-4 rounded text-[var(--site-accent,#2563eb)] border-gray-300 focus:ring-[var(--site-accent,#2563eb)] mt-0.5">
                                    <span class="text-sm text-gray-800">
                                        {{ $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-[var(--site-accent,#2563eb)] underline hover:no-underline font-medium">{{ __('اقرأ الشروط') }}</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 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm placeholder:text-gray-400 focus:ring-2 focus:ring-[var(--site-accent,#2563eb)] focus:border-[var(--site-accent,#2563eb)] transition-shadow @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                @break

                            @case('file_upload')
                                <div class="relative">
                                    <input type="file" wire:model="fields.{{ $field['key'] }}"
                                           class="w-full px-4 py-3 bg-white text-gray-900 border border-gray-200 rounded-xl text-sm shadow-sm file:me-3 file:py-1.5 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-medium file:bg-[var(--site-accent,#2563eb)]/10 file:text-[var(--site-accent,#2563eb)] hover:file:bg-[var(--site-accent,#2563eb)]/20 @error('fields.'.$field['key']) border-red-400 ring-1 ring-red-400 @enderror">
                                </div>
                                @break
                        @endswitch

                        @error('fields.'.$field['key'])
                            <p class="flex items-center gap-1 mt-1 text-xs text-red-600">
                                <svg class="w-3.5 h-3.5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01"/></svg>
                                {{ $message }}
                            </p>
                        @enderror
                    </div>
                @endforeach
            </div>

            <button type="submit" wire:loading.attr="disabled" wire:target="submit"
                    class="w-full py-4 bg-[var(--site-accent,#2563eb)] text-white rounded-xl font-bold text-base shadow-lg shadow-[var(--site-accent,#2563eb)]/25 hover:shadow-xl hover:shadow-[var(--site-accent,#2563eb)]/30 hover:brightness-110 transition-all disabled:opacity-50 disabled:shadow-none">
                <span wire:loading.remove wire:target="submit" class="flex items-center justify-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                    {{ __('تسجيل') }}
                </span>
                <span wire:loading wire:target="submit" class="flex items-center justify-center gap-2">
                    <svg class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                    {{ __('جارٍ التسجيل...') }}
                </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>
