<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">{{ __('استيراد مشتركين من CSV') }}</h1>
        <a href="{{ route('participants.list') }}" wire:navigate
           class="text-sm text-blue-600 hover:text-blue-800">{{ __('العودة للقائمة') }}</a>
    </div>

    {{-- Instructions --}}
    <div class="bg-blue-50 border border-blue-200 rounded-xl p-4 sm:p-6 mb-4 sm:mb-6">
        <h3 class="text-sm font-semibold text-blue-800 mb-2">{{ __('تعليمات الاستيراد') }}</h3>
        <ul class="text-sm text-blue-700 space-y-1 list-disc list-inside">
            <li>{{ __('الملف يجب أن يكون بصيغة CSV') }}</li>
            <li>{{ __('الأعمدة المطلوبة: name_ar, phone') }}</li>
            <li>{{ __('أعمدة اختيارية: name, email, gender, date_of_birth, nationality, membership_type, membership_id') }}</li>
            <li>{{ __('إذا كان رقم الهاتف موجود مسبقاً، يتم تخطي السجل') }}</li>
            {{-- Column names are part of the file format and never translate;
                 what they mean in this branch does, so it is spelled out. --}}
            <li>
                {{ __('عمود') }} <code dir="ltr" class="font-mono">membership_type</code>
                {{ __('يقبل') }} <code dir="ltr" class="font-mono">member</code> ({{ term('member') }})
                {{ __('أو') }} <code dir="ltr" class="font-mono">non_member</code> ({{ term('non_member') }}) —
                {{ __('والفارغ يُعتبر') }} {{ term('non_member') }}.
            </li>
            <li>
                <code dir="ltr" class="font-mono">membership_id</code> {{ __('هو') }} {{ term('membership_id') }}.
            </li>
        </ul>
    </div>

    {{-- Upload --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6 mb-4 sm:mb-6">
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 mb-2">{{ __('ملف CSV') }}</label>
            <input type="file" wire:model="file" accept=".csv,.txt"
                   class="block w-full text-sm text-gray-500 file:me-4 file:py-2.5 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
            @error('file') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
        </div>

        <div wire:loading wire:target="file" class="text-sm text-gray-500">{{ __('جارٍ تحميل الملف...') }}</div>
    </div>

    {{-- Preview --}}
    @if(!empty($preview))
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6 mb-4 sm:mb-6">
        <h3 class="text-sm font-semibold text-gray-800 mb-3">{{ __('معاينة (أول 5 سجلات)') }}</h3>
        <div class="overflow-x-auto -mx-4 sm:mx-0">
            <table class="min-w-full text-sm">
                <thead class="bg-gray-50">
                    <tr>
                        @foreach(array_keys($preview[0]) as $col)
                        {{-- The CSV column name stays, because that is what the
                             file must literally contain; the branch's word for
                             it sits underneath so nobody has to guess. --}}
                        <th class="px-3 py-2 text-start text-xs font-medium text-gray-500 whitespace-nowrap">
                            <span dir="ltr" class="font-mono block">{{ $col }}</span>
                            @if(isset($columnLabels[$col]))
                                <span class="block font-normal text-gray-400">{{ $columnLabels[$col] }}</span>
                            @endif
                        </th>
                        @endforeach
                    </tr>
                </thead>
                <tbody>
                    @foreach($preview as $row)
                    <tr class="border-t">
                        @foreach($row as $col => $cell)
                        <td class="px-3 py-2 text-gray-700 whitespace-nowrap">
                            @if($col === 'membership_type' && filled($cell))
                                {{ membership_label($cell) }}
                                <span class="text-xs text-gray-400" dir="ltr">({{ $cell }})</span>
                            @else
                                {{ $cell }}
                            @endif
                        </td>
                        @endforeach
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        <div class="flex flex-col-reverse sm:flex-row gap-3 mt-4">
            <button wire:click="import" wire:loading.attr="disabled"
                    class="w-full sm:w-auto px-6 py-2.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 text-sm font-medium">
                <span wire:loading.remove wire:target="import">{{ __('بدء الاستيراد') }}</span>
                <span wire:loading wire:target="import">{{ __('جارٍ الاستيراد...') }}</span>
            </button>
        </div>
    </div>
    @endif

    {{-- Errors --}}
    @if(!empty($importErrors))
    <div class="bg-red-50 border border-red-200 rounded-xl p-4 mb-4 sm:mb-6">
        <h3 class="text-sm font-semibold text-red-800 mb-2">{{ __('أخطاء') }}</h3>
        <ul class="text-sm text-red-700 space-y-1 max-h-40 overflow-y-auto">
            @foreach($importErrors as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif

    {{-- Results --}}
    @if($done)
    <div class="bg-green-50 border border-green-200 rounded-xl p-4 sm:p-6">
        <h3 class="text-base sm:text-lg font-semibold text-green-800 mb-3">{{ __('اكتمل الاستيراد') }}</h3>
        <div class="grid grid-cols-2 gap-3 sm:gap-6 text-sm">
            <span class="text-green-700">{{ __('تم استيراد:') }} <span class="font-bold">{{ $imported }}</span></span>
            <span class="text-amber-700">{{ __('تم تخطي:') }} <span class="font-bold">{{ $skipped }}</span></span>
        </div>
    </div>
    @endif
</div>
