<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">{{ __('الأسعار الأساسية') }}</h1>
        @can('pricing.create')
        <a href="{{ route('pricing.base-prices.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>
            <span class="hidden sm:inline">{{ __('إضافة سعر') }}</span>
        </a>
        @endcan
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-3 sm: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-3 sm: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-3 gap-3">
            <div class="col-span-2 md:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم...') }}"
                       class="w-full px-3 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="activeFilter" class="px-3 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-start font-medium text-gray-600">{{ __('العنصر') }}</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('الفرع') }}</th>
                        <x-ui.sort-header column="amount" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('المبلغ') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('الفترة') }}</th>
                        <x-ui.sort-header column="is_active" :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($prices as $price)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $price->name_ar }}</span>
                            @if($price->priority > 0)
                            <span class="ms-1 text-xs text-gray-400">({{ $price->priority }})</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            @if($price->priceable)
                                {{ $price->priceable->name_ar ?? '-' }}
                            @else
                                <span class="text-gray-400">-</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $price->branch?->name_ar ?? __('الكل') }}
                        </td>
                        <td class="px-4 py-3 text-center font-medium" dir="ltr">
                            {{ number_format($price->amount / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600 text-xs" dir="ltr">
                            {{ $price->effective_from?->format('Y-m-d') }}
                            @if($price->effective_to)
                                <br>{{ __('إلى') }} {{ $price->effective_to->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('{{ $price->uuid }}')"
                                    wire:loading.attr="disabled"
                                    class="px-2 py-0.5 text-xs rounded-full {{ $price->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $price->is_active ? __('نشط') : __('غير نشط') }}
                            </button>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('pricing.update')
                            <a href="{{ route('pricing.base-prices.edit', $price) }}" wire:navigate
                               class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تعديل') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" 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="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد أسعار أساسية') }}</p>
                                @can('pricing.create')
                                <a href="{{ route('pricing.base-prices.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($prices->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $prices->links() }}
        </div>
        @endif
    </div>

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

            <div class="space-y-1.5 text-sm text-gray-600">
                @if($price->priceable)
                <div class="flex items-center gap-2">
                    <span class="text-gray-400">{{ __('العنصر') }}:</span>
                    <span>{{ $price->priceable->name_ar ?? '-' }}</span>
                </div>
                @endif

                <div class="flex items-center gap-2">
                    <span class="text-gray-400">{{ __('الفرع') }}:</span>
                    <span>{{ $price->branch?->name_ar ?? __('الكل') }}</span>
                </div>

                <div class="flex items-center gap-2">
                    <span class="text-gray-400">{{ __('المبلغ') }}:</span>
                    <span class="font-bold text-gray-800" dir="ltr">{{ number_format($price->amount / 100, 2) }} {{ __('ج.م') }}</span>
                </div>

                <div class="flex items-center gap-2">
                    <span class="text-gray-400">{{ __('الفترة') }}:</span>
                    <span dir="ltr">
                        {{ $price->effective_from?->format('Y-m-d') }}
                        @if($price->effective_to)
                            - {{ $price->effective_to->format('Y-m-d') }}
                        @else
                            <span class="text-green-600">({{ __('مفتوح') }})</span>
                        @endif
                    </span>
                </div>
            </div>

            @can('pricing.update')
            <div class="mt-3 pt-3 border-t border-gray-100">
                <a href="{{ route('pricing.base-prices.edit', $price) }}" wire:navigate
                   class="inline-flex items-center gap-1 py-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                    {{ __('تعديل') }}
                </a>
            </div>
            @endcan
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8">
            <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="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
                <p class="text-gray-500 text-sm">{{ __('لا توجد أسعار أساسية') }}</p>
                @can('pricing.create')
                <a href="{{ route('pricing.base-prices.create') }}" wire:navigate
                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                    {{ __('إضافة أول سعر') }}
                </a>
                @endcan
            </div>
        </div>
        @endforelse

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