<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('الأطقم (BOM)') }}</h1>
        @can('inventory.manage')
        <a href="{{ route('inventory.kits.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-3 gap-4">
            <div class="sm:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم أو SKU...') }}"
                       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="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">{{ __('SKU') }}</th>
                        <x-ui.sort-header column="name_ar" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('اسم الطقم') }}</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>
                        <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($kits as $kit)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-gray-600 text-xs" dir="ltr">
                            {{ $kit->sku }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $kit->name_ar }}</span>
                            @if($kit->name)
                            <p class="text-xs text-gray-500" dir="ltr">{{ $kit->name }}</p>
                            @endif
                        </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">
                                {{ $kit->components_count }} {{ __('مكون') }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center font-mono" dir="ltr">
                            {{ number_format($kit->selling_price / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <button wire:click="toggleActive({{ $kit->id }})" wire:loading.attr="disabled"
                                    class="px-2 py-0.5 text-xs rounded-full {{ $kit->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $kit->is_active ? __('نشط') : __('غير نشط') }}
                            </button>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex items-center justify-center gap-2">
                                @can('inventory.manage')
                                <a href="{{ route('inventory.kits.edit', $kit) }}" wire:navigate
                                   class="text-green-600 hover:text-green-800 text-sm">{{ __('تعديل') }}</a>
                                <button wire:click="openAssembleModal({{ $kit->id }})"
                                        class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تجميع') }}</button>
                                <button wire:click="openDisassembleModal({{ $kit->id }})"
                                        class="text-amber-600 hover:text-amber-800 text-sm">{{ __('تفكيك') }}</button>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" 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="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد أطقم') }}</p>
                                @can('inventory.manage')
                                <a href="{{ route('inventory.kits.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($kits->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $kits->links() }}
        </div>
        @endif
    </div>

    {{-- Assemble Modal --}}
    @if($showAssembleModal)
    <div class="fixed inset-0 z-50 overflow-y-auto" x-data x-init="$el.focus()">
        <div class="flex items-center justify-center min-h-screen px-4">
            <div class="fixed inset-0 bg-black/50" wire:click="$set('showAssembleModal', false)"></div>
            <div class="relative bg-white rounded-xl shadow-xl w-full max-w-md p-6">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">{{ __('تجميع طقم') }}</h3>

                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('المستودع') }} *</label>
                        <select wire:model="selectedWarehouseId"
                                class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <option value="">{{ __('-- اختر المستودع --') }}</option>
                            @foreach($warehouses as $warehouse)
                            <option value="{{ $warehouse->id }}">{{ $warehouse->name_ar }}</option>
                            @endforeach
                        </select>
                        @error('selectedWarehouseId') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الكمية') }} *</label>
                        <input type="number" wire:model="assemblyQuantity" min="1" dir="ltr"
                               class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        @error('assemblyQuantity') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>
                </div>

                <div class="flex items-center justify-end gap-3 mt-6">
                    <button wire:click="$set('showAssembleModal', false)"
                            class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm">
                        {{ __('إلغاء') }}
                    </button>
                    <button wire:click="assemble" wire:loading.attr="disabled" wire:target="assemble"
                            class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:bg-blue-400 text-sm font-medium">
                        <span wire:loading.remove wire:target="assemble">{{ __('تجميع') }}</span>
                        <span wire:loading wire:target="assemble">{{ __('جارٍ التجميع...') }}</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
    @endif

    {{-- Disassemble Modal --}}
    @if($showDisassembleModal)
    <div class="fixed inset-0 z-50 overflow-y-auto" x-data x-init="$el.focus()">
        <div class="flex items-center justify-center min-h-screen px-4">
            <div class="fixed inset-0 bg-black/50" wire:click="$set('showDisassembleModal', false)"></div>
            <div class="relative bg-white rounded-xl shadow-xl w-full max-w-md p-6">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">{{ __('تفكيك طقم') }}</h3>

                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('المستودع') }} *</label>
                        <select wire:model="selectedWarehouseId"
                                class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <option value="">{{ __('-- اختر المستودع --') }}</option>
                            @foreach($warehouses as $warehouse)
                            <option value="{{ $warehouse->id }}">{{ $warehouse->name_ar }}</option>
                            @endforeach
                        </select>
                        @error('selectedWarehouseId') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الكمية') }} *</label>
                        <input type="number" wire:model="assemblyQuantity" min="1" dir="ltr"
                               class="w-full text-sm px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        @error('assemblyQuantity') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>
                </div>

                <div class="flex items-center justify-end gap-3 mt-6">
                    <button wire:click="$set('showDisassembleModal', false)"
                            class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm">
                        {{ __('إلغاء') }}
                    </button>
                    <button wire:click="disassemble" wire:loading.attr="disabled" wire:target="disassemble"
                            class="px-4 py-2 bg-amber-600 text-white rounded-lg hover:bg-amber-700 disabled:bg-amber-400 text-sm font-medium">
                        <span wire:loading.remove wire:target="disassemble">{{ __('تفكيك') }}</span>
                        <span wire:loading wire:target="disassemble">{{ __('جارٍ التفكيك...') }}</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
    @endif
</div>
