@extends('layouts.app')

@section('title', 'Daftar Produk')

@section('content')
<div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
    <div class="px-4 py-6 sm:px-0">
        <div class="flex justify-between items-center mb-8">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">Daftar Produk Adenium</h1>
                <p class="text-gray-600">Kelola produk yang tersedia di toko Anda.</p>
            </div>
            <a href="{{ route('products.create') }}" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-md font-medium">
                Tambah Produk Baru
            </a>
        </div>

        @if(session('success'))
            <div class="mb-6 rounded-lg bg-green-50 border border-green-200 p-4 text-green-800">
                {{ session('success') }}
            </div>
        @endif

        @if($products->isEmpty())
            <div class="rounded-lg border border-dashed border-gray-300 bg-white p-10 text-center">
                <h2 class="text-2xl font-semibold text-gray-900 mb-2">Belum ada produk</h2>
                <p class="text-gray-600 mb-6">Tambahkan produk Adenium untuk mulai menyimpan data di database.</p>
                <a href="{{ route('products.create') }}" class="inline-flex items-center justify-center rounded-md bg-green-600 px-5 py-3 text-white hover:bg-green-700">
                    Tambah Produk Baru
                </a>
            </div>
        @else
            <div class="mb-6 rounded-lg bg-blue-50 border border-blue-200 p-5 text-blue-900">
                <p class="text-sm">Untuk pelanggan, gunakan tombol <span class="font-semibold">Pesan</span> agar checkout produk langsung ke formulir pesanan.</p>
            </div>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                @foreach($products as $product)
                    <div class="bg-white overflow-hidden shadow rounded-lg hover:shadow-xl transition-shadow duration-300">
                        <div class="h-48 overflow-hidden">
                            <img class="w-full h-full object-cover" src="{{ $product->image ? asset('storage/' . $product->image) : 'https://via.placeholder.com/640x360?text=No+Image' }}" alt="{{ $product->name }}">
                        </div>
                        <div class="p-6">
                            <div class="flex items-center justify-between mb-2">
                                <h3 class="text-xl font-semibold text-gray-900">{{ $product->name }}</h3>
                                @if($product->popular)
                                    <span class="inline-flex items-center rounded-full bg-yellow-100 px-2.5 py-1 text-xs font-semibold text-yellow-800">Populer</span>
                                @endif
                            </div>
                            <p class="text-gray-600 text-sm mb-4">{{ \Illuminate\Support\Str::limit($product->description, 80) ?: 'Tidak ada deskripsi.' }}</p>
                            <div class="flex justify-between items-center mb-4">
                                <span class="text-2xl font-bold text-green-600">Rp {{ number_format($product->price, 0, ',', '.') }}</span>
                                <span class="text-sm text-gray-500">ID #{{ $product->id }}</span>
                            </div>
                            <div class="flex flex-wrap gap-2">
                                <a href="{{ route('orders.create', ['product_id' => $product->id]) }}" class="bg-green-600 hover:bg-green-700 text-white px-3 py-2 rounded text-sm">
                                    Checkout Cepat
                                </a>
                                <a href="{{ route('products.show', $product) }}" class="bg-blue-500 hover:bg-blue-600 text-white px-3 py-2 rounded text-sm">
                                    Lihat
                                </a>
                                @if(Auth::check() && Auth::user()->role === 'admin')
                                    <a href="{{ route('products.edit', $product) }}" class="bg-yellow-500 hover:bg-yellow-600 text-white px-3 py-2 rounded text-sm">
                                        Edit
                                    </a>
                                @endif
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>
</div>
@endsection

