<div>
    {{-- Header --}}
    <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">{{ __('قواعد التسعير') }}</h1>
        @can('pricing.create')
        <a href="{{ route('pricing.rules.wizard') }}" 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>
            <span class="hidden sm:inline">{{ __('إضافة قاعدة') }}</span>
        </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-3 sm:p-4 mb-4">
        <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
            <div class="col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم...') }}"
                       class="w-full px-4 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="ruleType" class="px-4 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الأنواع') }}</option>
                @foreach($ruleTypes as $value => $label)
                <option value="{{ $value }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="activeFilter" class="px-4 py-2 text-sm 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>

    {{-- Desktop Table --}}
    <div class="hidden md:block bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto" 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>
                        <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>
                        <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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($rules as $rule)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $rule->name_ar }}</span>
                            @if($rule->branch)
                            <span class="block text-xs text-gray-400">{{ $rule->branch->name_ar }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-0.5 text-xs bg-purple-100 text-purple-700 rounded-full">
                                {{ $rule->rule_type->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-0.5 text-xs bg-blue-100 text-blue-700 rounded-full">
                                {{ $rule->adjustment_type->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center font-medium" dir="ltr">
                            @if(in_array($rule->adjustment_type, [\App\Domain\Pricing\Enums\AdjustmentType::PercentageDiscount, \App\Domain\Pricing\Enums\AdjustmentType::PercentageIncrease]))
                                {{ \App\Domain\Pricing\Support\Percent::label($rule->adjustment_value) }}
                            @else
                                {{ number_format($rule->adjustment_value / 100, 2) }} {{ __('ج.م') }}
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($rule->is_stackable)
                            <span class="text-green-600">{{ __('نعم') }}</span>
                            @else
                            <span class="text-gray-400">{{ __('لا') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $rule->priority }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <button wire:click="toggleActive('{{ $rule->uuid }}')"
                                    wire:loading.attr="disabled"
                                    class="px-2 py-0.5 text-xs rounded-full {{ $rule->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $rule->is_active ? __('نشط') : __('غير نشط') }}
                            </button>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('pricing.update')
                            <a href="{{ route('pricing.rules.edit', $rule) }}" wire:navigate
                               class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تعديل') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" 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="M9 7h6m0 10v-3m-3 3h.01M9 17h.01M9 14h.01M12 14h.01M15 11h.01M12 11h.01M9 11h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد قواعد تسعير') }}</p>
                                @can('pricing.create')
                                <a href="{{ route('pricing.rules.wizard') }}" 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($rules->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $rules->links() }}
        </div>
        @endif
    </div>

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($rules as $rule)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            {{-- Card Header: Name + Status Toggle --}}
            <div class="flex items-start justify-between gap-3 mb-3">
                <div>
                    <span class="font-medium text-gray-800">{{ $rule->name_ar }}</span>
                    @if($rule->branch)
                    <span class="block text-xs text-gray-400 mt-0.5">{{ $rule->branch->name_ar }}</span>
                    @endif
                </div>
                <button wire:click="toggleActive('{{ $rule->uuid }}')"
                        wire:loading.attr="disabled"
                        class="px-2 py-1 text-xs rounded-full shrink-0 {{ $rule->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                    {{ $rule->is_active ? __('نشط') : __('غير نشط') }}
                </button>
            </div>

            {{-- Badges Row --}}
            <div class="flex flex-wrap items-center gap-2 mb-3">
                <span class="px-2 py-0.5 text-xs bg-purple-100 text-purple-700 rounded-full">
                    {{ $rule->rule_type->label() }}
                </span>
                <span class="px-2 py-0.5 text-xs bg-blue-100 text-blue-700 rounded-full">
                    {{ $rule->adjustment_type->label() }}
                </span>
            </div>

            {{-- Details Row --}}
            <div class="flex items-center justify-between text-sm text-gray-600 mb-3">
                <div class="flex items-center gap-4">
                    <span dir="ltr" class="font-medium text-gray-800">
                        @if(in_array($rule->adjustment_type, [\App\Domain\Pricing\Enums\AdjustmentType::PercentageDiscount, \App\Domain\Pricing\Enums\AdjustmentType::PercentageIncrease]))
                            {{ \App\Domain\Pricing\Support\Percent::label($rule->adjustment_value) }}
                        @else
                            {{ number_format($rule->adjustment_value / 100, 2) }} {{ __('ج.م') }}
                        @endif
                    </span>
                    <span>
                        {{ __('الأولوية') }}: <span dir="ltr">{{ $rule->priority }}</span>
                    </span>
                </div>
                <span>
                    @if($rule->is_stackable)
                    <span class="text-green-600 text-xs">{{ __('قابل للتكديس') }}</span>
                    @else
                    <span class="text-gray-400 text-xs">{{ __('غير قابل للتكديس') }}</span>
                    @endif
                </span>
            </div>

            {{-- Action --}}
            @can('pricing.update')
            <a href="{{ route('pricing.rules.edit', $rule) }}" wire:navigate
               class="block text-center py-2 text-blue-600 hover:text-blue-800 text-sm font-medium border-t border-gray-100 -mx-4 -mb-4 px-4 pt-3 pb-3 rounded-b-xl hover:bg-gray-50">
                {{ __('تعديل') }}
            </a>
            @endcan
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 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="M9 7h6m0 10v-3m-3 3h.01M9 17h.01M9 14h.01M12 14h.01M15 11h.01M12 11h.01M9 11h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
                </svg>
                <p class="text-gray-500 text-sm">{{ __('لا توجد قواعد تسعير') }}</p>
                @can('pricing.create')
                <a href="{{ route('pricing.rules.wizard') }}" wire:navigate
                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                    {{ __('إضافة أول قاعدة') }}
                </a>
                @endcan
            </div>
        </div>
        @endforelse

        @if($rules->hasPages())
        <div class="mt-4">
            {{ $rules->links() }}
        </div>
        @endif
    </div>
</div>
