@extends('layouts.admin')
@section('title', 'Manage Products')
@section('header_title', 'Products')

@section('content')
<div class="bg-white rounded-lg border border-gray-200 shadow-sm overflow-hidden">
    <div class="p-6 border-b border-gray-200 flex flex-col sm:flex-row justify-between items-start sm:items-center space-y-4 sm:space-y-0">
        <div>
            <h2 class="text-xl font-extrabold text-brand-dark uppercase tracking-wider mb-1">Product Inventory</h2>
            <p class="text-sm text-gray-500">Manage all store products, prices, and stock levels.</p>
        </div>
        <a href="{{ route('admin.products.create') }}" class="group flex items-center space-x-2 bg-brand-red text-white px-4 py-2.5 rounded font-bold uppercase tracking-widest text-sm hover:bg-black transition-colors shadow-sm cursor-pointer whitespace-nowrap">
            <i data-lucide="plus" class="w-4 h-4"></i>
            <span>Add Product</span>
        </a>
    </div>

    <div class="overflow-x-auto">
        <table class="w-full text-left text-sm whitespace-nowrap">
            <thead class="bg-gray-50 border-b border-gray-200 text-gray-500">
                <tr>
                    <th class="px-6 py-4 font-bold uppercase tracking-wider text-xs">Image</th>
                    <th class="px-6 py-4 font-bold uppercase tracking-wider text-xs">Name & Details</th>
                    <th class="px-6 py-4 font-bold uppercase tracking-wider text-xs">Price</th>
                    <th class="px-6 py-4 font-bold uppercase tracking-wider text-xs">Stock</th>
                    <th class="px-6 py-4 font-bold uppercase tracking-wider text-xs text-right">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @if($products->count() > 0)
                    @foreach($products as $product)
                    <tr class="hover:bg-gray-50 transition-colors">
                        <td class="px-6 py-4 w-20">
                            <div class="w-14 h-14 bg-white border border-gray-100 rounded overflow-hidden flex-shrink-0">
                                @if($product->image_path)
                                    <img src="{{ asset('images/' . $product->image_path) }}" class="w-full h-full object-contain mix-blend-multiply">
                                @else
                                    <div class="w-full h-full bg-gray-100 flex items-center justify-center text-gray-400"><i data-lucide="image" class="w-5 h-5"></i></div>
                                @endif
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="font-bold text-gray-800 text-base mb-1 truncate max-w-[300px]">{{ $product->name }}</div>
                            <div class="text-xs text-brand-red font-bold uppercase tracking-widest">{{ $product->category }} • {{ $product->brand }}</div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="font-bold text-brand-dark">${{ number_format($product->price, 2) }}</div>
                            @if($product->old_price)
                                <div class="text-xs text-gray-400 line-through">${{ number_format($product->old_price, 2) }}</div>
                            @endif
                        </td>
                        <td class="px-6 py-4">
                            <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-bold {{ $product->stock > 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                <span class="w-1.5 h-1.5 rounded-full {{ $product->stock > 0 ? 'bg-green-500' : 'bg-red-500' }} mr-2 animate-pulse"></span>
                                {{ $product->stock }}
                            </span>
                        </td>
                        <td class="px-6 py-4 text-right space-x-2">
                            <a href="{{ url('/product/' . $product->id) }}" target="_blank" class="inline-block p-2 text-gray-400 hover:text-blue-500 hover:bg-blue-50 rounded transition-colors" title="View Storefront">
                                <i data-lucide="external-link" class="w-4 h-4"></i>
                            </a>
                            <a href="{{ route('admin.products.edit', $product->id) }}" class="inline-block p-2 text-gray-400 hover:text-brand-dark hover:bg-gray-100 rounded transition-colors" title="Edit">
                                <i data-lucide="edit-2" class="w-4 h-4"></i>
                            </a>
                            <form method="POST" action="{{ route('admin.products.destroy', $product->id) }}" class="inline-block" onsubmit="return confirm('Are you sure you want to delete this product?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="p-2 text-gray-400 hover:text-brand-red hover:bg-red-50 rounded transition-colors" title="Delete">
                                    <i data-lucide="trash-2" class="w-4 h-4"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    @endforeach
                @else
                <tr>
                    <td colspan="5" class="px-6 py-12 text-center">
                        <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-gray-100 text-gray-400 mb-4"><i data-lucide="package-x" class="w-8 h-8"></i></div>
                        <h3 class="text-lg font-bold text-gray-800 mb-2">No products found</h3>
                        <p class="text-gray-500 mb-4 max-w-sm mx-auto">Get started by creating your first product. It will immediately appear on the storefront.</p>
                        <a href="{{ route('admin.products.create') }}" class="inline-flex items-center space-x-2 bg-brand-red text-white px-6 py-2.5 rounded font-bold uppercase tracking-widest text-sm hover:bg-black transition-colors">
                            <span>Add First Product</span>
                        </a>
                    </td>
                </tr>
                @endif
            </tbody>
        </table>
    </div>
    
    @if($products->hasPages())
    <div class="px-6 py-4 border-t border-gray-200 bg-gray-50">
        {{ $products->links() }}
    </div>
    @endif
</div>
@endsection
