<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('العروض والكوبونات') }}</h1>
        @can('pricing.create')
        <a href="{{ route('pricing.promotions.create') }}" wire:navigate
           class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
            {{ __('إضافة عرض') }}
        </a>
        @endcan
    </div>

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

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-4">
        <div class="grid grid-cols-1 sm:grid-cols-4 gap-4">
            <div class="sm:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم أو الكود...') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="type" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الأنواع') }}</option>
                @foreach($promotionTypes as $value => $label)
                <option value="{{ $value }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="activeFilter" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('الكل') }}</option>
                <option value="1">{{ __('نشط') }}</option>
                <option value="0">{{ __('غير نشط') }}</option>
            </select>
        </div>
    </div>

    {{-- Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div wire:loading.class="opacity-50 pointer-events-none">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('الاسم') }}</th>
                        <x-ui.sort-header column="code" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الكود') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('النوع') }}</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('التعديل') }}</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('الاستخدام') }}</th>
                        <x-ui.sort-header column="start_date" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الفترة') }}</x-ui.sort-header>
                        <x-ui.sort-header column="is_active" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                        <x-ui.sort-header column="created_at" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('تاريخ الإنشاء') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($promotions as $promotion)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $promotion->name_ar }}</span>
                            @if($promotion->branch)
                            <span class="block text-xs text-gray-400">{{ $promotion->branch->name_ar }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($promotion->code)
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 bg-gray-100 rounded font-mono text-xs" dir="ltr"
                                  x-data x-on:click="navigator.clipboard.writeText('{{ $promotion->code }}')" role="button" title="{{ __('نسخ') }}">
                                {{ $promotion->code }}
                                <svg class="w-3 h-3 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/></svg>
                            </span>
                            @else
                            <span class="text-gray-400">-</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $typeColors = [
                                    'automatic' => 'bg-green-100 text-green-700',
                                    'coupon' => 'bg-blue-100 text-blue-700',
                                    'campaign' => 'bg-purple-100 text-purple-700',
                                ];
                                $colorClass = $typeColors[$promotion->type->value] ?? 'bg-gray-100 text-gray-700';
                            @endphp
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $colorClass }}">
                                {{ $promotion->type->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center text-xs" dir="ltr">
                            @if(in_array($promotion->adjustment_type, [\App\Domain\Pricing\Enums\AdjustmentType::PercentageDiscount, \App\Domain\Pricing\Enums\AdjustmentType::PercentageIncrease]))
                                {{ \App\Domain\Pricing\Support\Percent::label($promotion->adjustment_value) }} {{ $promotion->adjustment_type->label() }}
                            @else
                                {{ number_format($promotion->adjustment_value / 100, 2) }} {{ __('ج.م') }}
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="text-xs" dir="ltr">
                                <span class="font-medium">{{ $promotion->usage_count ?? 0 }}</span>
                                @if($promotion->usage_limit)
                                <span class="text-gray-400"> / {{ $promotion->usage_limit }}</span>
                                @else
                                <span class="text-gray-400"> / {{ __('غير محدود') }}</span>
                                @endif
                            </div>
                            @if($promotion->usage_limit)
                            <div class="w-full bg-gray-200 rounded-full h-1 mt-1">
                                <div class="bg-blue-600 h-1 rounded-full" style="width: {{ min(100, ($promotion->usage_count / $promotion->usage_limit) * 100) }}%"></div>
                            </div>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600 text-xs" dir="ltr">
                            {{ $promotion->start_date?->format('Y-m-d') }}
                            @if($promotion->end_date)
                                <br>{{ __('إلى') }} {{ $promotion->end_date->format('Y-m-d') }}
                            @else
                                <br><span class="text-green-600">{{ __('مفتوح') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <button wire:click="toggleActive('{{ $promotion->uuid }}')"
                                    wire:loading.attr="disabled"
                                    class="px-2 py-0.5 text-xs rounded-full {{ $promotion->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $promotion->is_active ? __('نشط') : __('غير نشط') }}
                            </button>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-500 text-xs" dir="ltr">
                            {{ $promotion->created_at?->format('Y-m-d') ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('pricing.update')
                            <a href="{{ route('pricing.promotions.edit', $promotion) }}" wire:navigate
                               class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تعديل') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="9" class="px-4 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد عروض أو كوبونات') }}</p>
                                @can('pricing.create')
                                <a href="{{ route('pricing.promotions.create') }}" wire:navigate
                                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                                    {{ __('إضافة أول عرض') }}
                                </a>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($promotions->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $promotions->links() }}
        </div>
        @endif
    </div>
</div>
