<div class="flex h-full w-full flex-1 flex-col gap-4 rounded-xl">
    {{-- Ini adalah satu-satunya root element untuk komponen ini --}}

    {{-- Pesan Sukses/Error --}}
    @if (session()->has('message'))
        <div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 3000)"
             class="bg-green-500 text-white p-3 rounded-lg shadow-md mb-4">
            {{ session('message') }}
        </div>
    @endif

    {{-- Kotak Utama untuk Tabel --}}
    <div class="relative h-full flex-1 overflow-hidden rounded-xl border border-neutral-200 dark:border-neutral-700 p-4">
        <div class="flex justify-between items-center mb-4">
            <h2 class="text-xl font-semibold text-neutral-800 dark:text-neutral-100">Daftar Bundle</h2>
            <button wire:click="create()" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded-lg">
                Tambah Bundle
            </button>
        </div>

        {{-- Search Input --}}
        <div class="mb-4">
            <input type="text" wire:model.live.debounce.300ms="search" placeholder="Cari bundle..."
                   class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-neutral-300 dark:border-neutral-700 rounded-md dark:bg-neutral-800 dark:text-neutral-200">
        </div>

        <div class="overflow-x-auto"> {{-- Membuat tabel bisa discroll jika lebar --}}
            <table class="min-w-full divide-y divide-neutral-200 dark:divide-neutral-700">
                <thead class="bg-neutral-50 dark:bg-neutral-800">
                    <tr>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 dark:text-neutral-300 uppercase tracking-wider cursor-pointer"
                            wire:click="sortBy('name')">
                            Nama Bundle
                            @if ($sortBy == 'name')
                                @if ($sortDirection == 'asc')
                                    &uarr;
                                @else
                                    &darr;
                                @endif
                            @endif
                        </th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 dark:text-neutral-300 uppercase tracking-wider cursor-pointer"
                            wire:click="sortBy('price')">
                            Harga
                            @if ($sortBy == 'price')
                                @if ($sortDirection == 'asc')
                                    &uarr;
                                @else
                                    &darr;
                                @endif
                            @endif
                        </th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 dark:text-neutral-300 uppercase tracking-wider cursor-pointer"
                            wire:click="sortBy('is_active')">
                            Aktif
                            @if ($sortBy == 'is_active')
                                @if ($sortDirection == 'asc')
                                    &uarr;
                                @else
                                    &darr;
                                @endif
                            @endif
                        </th>
                        <th scope="col" class="relative px-6 py-3">
                            <span class="sr-only">Aksi</span>
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white dark:bg-neutral-900 divide-y divide-neutral-200 dark:divide-neutral-700">
                    @forelse ($bundles as $bundle)
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-neutral-900 dark:text-neutral-100">
                                {{ $bundle->name }}
                                <p class="text-xs text-neutral-500 dark:text-neutral-400">{{ Str::limit($bundle->description, 50) ?? 'Tidak ada deskripsi' }}</p>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-neutral-500 dark:text-neutral-300">
                                @if($bundle->price)
                                    Rp{{ number_format($bundle->price, 0, ',', '.') }}
                                @else
                                    Harga ditentukan Item
                                @endif
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                @if ($bundle->is_active)
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-800 dark:text-green-100">
                                        Ya
                                    </span>
                                @else
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800 dark:bg-red-800 dark:text-red-100">
                                        Tidak
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                <button wire:click="edit({{ $bundle->id }})" class="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-500 mr-2">Edit</button>
                                <button wire:click="confirmDelete({{ $bundle->id }})" class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-500">Hapus</button>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="px-6 py-4 text-center text-sm text-neutral-500 dark:text-neutral-300">
                                Tidak ada data bundle yang ditemukan.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Pagination Links --}}
        <div class="mt-4">
            {{ $bundles->links() }}
        </div>
    </div>

    {{-- Modal Create/Edit --}}
    @if($isModalOpen)
        <div class="fixed inset-0 bg-gray-600 bg-opacity-75 flex items-center justify-center z-50">
            <div class="bg-white dark:bg-neutral-800 rounded-lg shadow-xl p-6 w-full max-w-lg mx-auto overflow-y-auto max-h-[90vh]">
                <h3 class="text-lg leading-6 font-medium text-neutral-900 dark:text-neutral-100 mb-4">
                    {{ $bundleId ? 'Edit Bundle' : 'Tambah Bundle Baru' }}
                </h3>
                <form wire:submit.prevent="store">
                    <div class="mb-4">
                        <label for="name" class="block text-sm font-medium text-neutral-700 dark:text-neutral-300">Nama Bundle</label>
                        <input type="text" id="name" wire:model.lazy="name"
                               class="mt-1 block w-full rounded-md border-neutral-300 dark:border-neutral-700 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-neutral-700 dark:text-neutral-100">
                        @error('name') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                    </div>
                    <div class="mb-4">
                        <label for="description" class="block text-sm font-medium text-neutral-700 dark:text-neutral-300">Deskripsi</label>
                        <textarea id="description" wire:model.lazy="description" rows="3"
                                  class="mt-1 block w-full rounded-md border-neutral-300 dark:border-neutral-700 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-neutral-700 dark:text-neutral-100"></textarea>
                        @error('description') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                    </div>
                    <div class="mb-4">
                        <label for="price" class="block text-sm font-medium text-neutral-700 dark:text-neutral-300">Harga (Opsional)</label>
                        <input type="number" step="0.01" id="price" wire:model.lazy="price"
                               class="mt-1 block w-full rounded-md border-neutral-300 dark:border-neutral-700 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-neutral-700 dark:text-neutral-100">
                        @error('price') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                    </div>
                    <div class="mb-4">
                        <label for="is_active" class="block text-sm font-medium text-neutral-700 dark:text-neutral-300">Aktif?</label>
                        <input type="checkbox" id="is_active" wire:model="is_active"
                               class="mt-1 rounded border-neutral-300 dark:border-neutral-700 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        @error('is_active') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                    </div>

                    {{-- Jika Anda ingin mengelola item yang termasuk dalam bundle, tambahkan di sini --}}
                    {{--
                    <div class="mb-4">
                        <label for="selectedItems" class="block text-sm font-medium text-neutral-700 dark:text-neutral-300">Pilih Item dalam Bundle</label>
                        <select multiple id="selectedItems" wire:model.lazy="selectedItems"
                                class="mt-1 block w-full rounded-md border-neutral-300 dark:border-neutral-700 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-neutral-700 dark:text-neutral-100 h-32">
                            @foreach($allItems as $item)
                                <option value="{{ $item->id }}">{{ $item->name }}</option>
                            @endforeach
                        </select>
                        @error('selectedItems') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                        @error('selectedItems.*') <span class="text-red-500 text-xs">{{ $message }}</span> @enderror
                    </div>
                    --}}

                    <div class="flex justify-end gap-2">
                        <button type="button" wire:click="closeModal()"
                                class="inline-flex justify-center rounded-md border border-neutral-300 dark:border-neutral-700 shadow-sm px-4 py-2 bg-white dark:bg-neutral-700 text-base font-medium text-neutral-700 dark:text-neutral-200 hover:bg-neutral-50 dark:hover:bg-neutral-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">
                            Batal
                        </button>
                        <button type="submit"
                                class="inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:ml-3 sm:w-auto sm:text-sm">
                            Simpan
                        </button>
                    </div>
                </form>
            </div>
        </div>
    @endif

    {{-- Modal Konfirmasi Hapus --}}
    @if($isDeleteModalOpen)
        <div class="fixed inset-0 bg-gray-600 bg-opacity-75 flex items-center justify-center z-50">
            <div class="bg-white dark:bg-neutral-800 rounded-lg shadow-xl p-6 w-full max-w-sm mx-auto">
                <h3 class="text-lg leading-6 font-medium text-neutral-900 dark:text-neutral-100 mb-4">
                    Konfirmasi Hapus
                </h3>
                <p class="text-neutral-600 dark:text-neutral-300 mb-6">
                    Apakah Anda yakin ingin menghapus bundle ini? Tindakan ini tidak dapat dibatalkan.
                    <br><span class="font-bold text-red-500">Peringatan: Item yang terkait dengan bundle ini akan kehilangan referensi bundle mereka.</span>
                </p>
                <div class="flex justify-end gap-2">
                    <button type="button" wire:click="closeDeleteModal()"
                            class="inline-flex justify-center rounded-md border border-neutral-300 dark:border-neutral-700 shadow-sm px-4 py-2 bg-white dark:bg-neutral-700 text-base font-medium text-neutral-700 dark:text-neutral-200 hover:bg-neutral-50 dark:hover:bg-neutral-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">
                        Batal
                    </button>
                    <button type="button" wire:click="delete()"
                            class="inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm">
                        Hapus
                    </button>
                </div>
            </div>
        </div>
    @endif
</div>
