<x-admin-layout>
    <x-slot name="header">
        {{ __('Katalog Produk Digital') }}
    </x-slot>

    <div class="flex justify-end mb-4">
        <a href="{{ route('admin.products.create') }}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors">
            <x-heroicon-o-plus class="-ml-1 mr-2 h-5 w-5" />
            Tambah Produk
        </a>
    </div>

    <!-- Table Section -->
    <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 overflow-hidden">
        <div class="px-6 py-5 border-b border-gray-100 dark:border-slate-700 flex flex-col sm:flex-row justify-between items-center gap-4">
            <h3 class="font-semibold text-gray-800 dark:text-gray-100">Daftar Produk</h3>
            <div class="w-full sm:w-64">
                <div class="relative rounded-md shadow-sm">
                    <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                        <x-heroicon-o-magnifying-glass class="h-4 w-4 text-gray-400" />
                    </div>
                    <input type="text" class="focus:ring-indigo-500 focus:border-indigo-500 block w-full pl-10 text-sm border-gray-300 dark:border-slate-600 rounded-lg placeholder-gray-400 dark:bg-slate-700 dark:text-white dark:border-slate-600" placeholder="Cari produk...">
                </div>
            </div>
        </div>
        
        <div class="overflow-x-auto">
            <table class="w-full text-sm text-left whitespace-nowrap">
                <thead class="text-xs text-gray-500 dark:text-gray-400 uppercase bg-gray-50 dark:bg-slate-700/50 border-b border-gray-100 dark:border-slate-700">
                    <tr>
                        <th scope="col" class="px-6 py-4 font-medium tracking-wider">Info Produk</th>
                        <th scope="col" class="px-6 py-4 font-medium tracking-wider">Kategori</th>
                        <th scope="col" class="px-6 py-4 font-medium tracking-wider">Harga</th>
                        <th scope="col" class="px-6 py-4 font-medium tracking-wider">Status</th>
                        <th scope="col" class="px-6 py-4 font-medium tracking-wider text-right">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-slate-700 bg-white dark:bg-slate-800">
                    @forelse ($products as $product)
                    <tr class="hover:bg-gray-50 dark:hover:bg-slate-700/50 dark:bg-slate-700/50 transition-colors">
                        <td class="px-6 py-4">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10 flex items-center justify-center rounded-lg bg-gray-100 dark:bg-slate-700 mr-4 overflow-hidden border border-gray-200 dark:border-slate-700">
                                    @if($product->thumbnail)
                                        <img src="{{ Storage::url($product->thumbnail) }}" alt="{{ $product->name }}" class="h-full w-full object-cover">
                                    @else
                                        <x-heroicon-o-cube class="h-6 w-6 text-gray-400" />
                                    @endif
                                </div>
                                <div>
                                    <div class="font-medium text-gray-900 dark:text-white">{{ $product->name }}</div>
                                    <div class="flex items-center gap-1 mt-0.5">
                                        <x-heroicon-o-link class="w-3 h-3 text-gray-400" />
                                        <a href="{{ $product->drive_link }}" target="_blank" class="text-xs text-indigo-500 hover:underline max-w-[150px] truncate" title="{{ $product->drive_link }}">Google Drive</a>
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-md text-xs font-medium bg-gray-100 dark:bg-slate-700 text-gray-800 dark:text-gray-100">
                                @if($product->category)
                                    <x-dynamic-component :component="'heroicon-o-' . ($product->category->icon ?: 'folder')" class="h-3 w-3 mr-1" />
                                    {{ $product->category->name }}
                                @else
                                    Tanpa Kategori
                                @endif
                            </span>
                        </td>
                        <td class="px-6 py-4">
                            <div class="font-medium text-gray-900 dark:text-white">Rp {{ number_format($product->price, 0, ',', '.') }}</div>
                        </td>
                        <td class="px-6 py-4">
                            @if($product->is_active)
                                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-xs font-semibold bg-emerald-50 dark:bg-emerald-900/30 text-emerald-700 dark:text-emerald-400 border border-emerald-100 dark:border-emerald-800">
                                    <span class="w-1.5 h-1.5 rounded-full bg-emerald-50 dark:bg-emerald-900/300"></span> Valid
                                </span>
                            @else
                                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-xs font-semibold bg-gray-50 dark:bg-slate-700/50 text-gray-600 dark:text-gray-300 border border-gray-200 dark:border-slate-700">
                                    <span class="w-1.5 h-1.5 rounded-full bg-gray-400"></span> Draft
                                </span>
                            @endif
                        </td>
                        <td class="px-6 py-4 text-right">
                            <div class="flex items-center justify-end gap-2">
                                <a href="{{ route('admin.products.edit', $product) }}" class="p-2 text-gray-400 hover:text-indigo-600 dark:hover:text-indigo-400 dark:text-indigo-400 hover:bg-indigo-50 dark:hover:bg-indigo-900/30 dark:bg-indigo-900/30 rounded-lg transition-colors" title="Edit">
                                    <x-heroicon-o-pencil-square class="w-5 h-5" />
                                </a>
                                <form action="{{ route('admin.products.destroy', $product) }}" method="POST" data-confirm="Hapus produk ini secara permanen?" class="inline-block">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="p-2 text-gray-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-900/30 dark:bg-red-900/30 rounded-lg transition-colors" title="Delete">
                                        <x-heroicon-o-trash class="w-5 h-5" />
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-6 py-12 text-center text-gray-500 dark:text-gray-400">
                            <div class="flex flex-col items-center justify-center">
                                <div class="w-16 h-16 rounded-full bg-gray-100 dark:bg-slate-700 flex items-center justify-center mb-4 text-gray-400">
                                    <x-heroicon-o-cube class="w-8 h-8" />
                                </div>
                                <h4 class="text-base font-medium text-gray-900 dark:text-white mb-1">Tidak ada produk</h4>
                                <p class="text-sm">Silakan tambah produk digital pertama Anda.</p>
                                <a href="{{ route('admin.products.create') }}" class="mt-4 text-indigo-600 dark:text-indigo-400 font-medium hover:text-indigo-800 text-sm">
                                    + Tambah Produk
                                </a>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        @if($products->hasPages())
        <div class="px-6 py-4 border-t border-gray-100 dark:border-slate-700 bg-gray-50 dark:bg-slate-700/50">
            {{ $products->links() }}
        </div>
        @endif
    </div>
</x-admin-layout>
