@extends('layouts.app')

@section('content')
<div class="bg-gray-50 min-h-screen">

    <!-- 🔷 HEADER BG -->
    <div class="h-48 md:h-64 bg-gradient-to-r from-blue-900 via-blue-600 to-blue-400 w-full absolute top-0 left-0 z-0"></div>

    <div class="container max-w-7xl mx-auto px-4 py-8 relative z-10">

        <!-- HEADER -->
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
            <div class="text-white">
                <h1 class="text-2xl md:text-3xl font-bold">📦 Produk Binaan</h1>
                <p class="text-blue-100 text-sm">Kelola produk UMKM / binaan</p>
            </div>

            <div class="flex flex-col sm:flex-row gap-2 w-full md:w-auto">

                <!-- SEARCH -->
                <form action="{{ route('admin.products.index') }}" method="GET" class="relative w-full sm:w-64">
                    <input type="text" name="search" value="{{ request('search') }}"
                        placeholder="Cari produk..."
                        class="w-full px-4 py-2 rounded-xl border border-gray-300 text-sm shadow-sm">
                </form>

                <!-- BACK -->
                <a href="{{ route('admin.dashboard') }}"
                    class="px-4 py-2 rounded-xl bg-white/10 text-white border border-white/20 hover:bg-white/20 text-center">
                    Kembali
                </a>

                <!-- ADD -->
                <a href="{{ route('admin.products.create') }}"
                    class="px-4 py-2 rounded-xl bg-white text-blue-700 font-bold shadow hover:shadow-lg transition text-center">
                    + Tambah
                </a>

            </div>
        </div>

        <!-- ALERT -->
        @if(session('success'))
        <div class="mb-6 p-4 rounded-xl bg-green-50 border-l-4 border-green-500 text-green-700 shadow">
            {{ session('success') }}
        </div>
        @endif

        <!-- CARD -->
        <div class="bg-white rounded-2xl shadow-sm border overflow-hidden">

            <!-- DESKTOP -->
            <div class="hidden md:block overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 text-xs uppercase text-gray-500">
                        <tr>
                            <th class="px-5 py-3">Gambar</th>
                            <th class="px-5 py-3">Produk</th>
                            <th class="px-5 py-3">Harga</th>
                            <th class="px-5 py-3">Kontak</th>
                            <th class="px-5 py-3">Status</th>
                            <th class="px-5 py-3 text-center">Aksi</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y">
                        @forelse($products as $product)
                        <tr class="hover:bg-gray-50 transition">

                            <!-- IMAGE -->
                            <td class="px-5 py-4">
                                @if ($product->gambar)
                                    <img src="{{ asset('storage/' . $product->gambar) }}"
                                        class="w-16 h-16 object-cover rounded-xl border">
                                @else
                                    <div class="w-16 h-16 bg-gray-100 rounded-xl flex items-center justify-center text-xs text-gray-400">
                                        No Img
                                    </div>
                                @endif
                            </td>

                            <!-- INFO -->
                            <td class="px-5 py-4">
                                <p class="font-bold text-gray-800">{{ $product->nama_produk }}</p>
                                <p class="text-xs text-gray-500">
                                    {{ Str::limit($product->deskripsi, 60) }}
                                </p>
                            </td>

                           <td class="px-5 py-4 whitespace-nowrap">
    <div style="display:inline-block;">
        <span style="background:#E1633D;color:white;padding:4px 10px;border-radius:999px;font-size:11px;display:inline-block;">
            Rp {{ number_format($product->harga, 0, ',', '.') }}
        </span>
    </div>
</td>

                            <!-- WA -->
                            <td class="px-5 py-4 text-sm text-gray-600">
                                {{ $product->no_hp ?? '-' }}
                            </td>

                            <!-- STATUS -->
                            <td class="px-5 py-4">
                                @if($product->status == 'tersedia')
                                    <span style="background:#10b981;color:white;padding:4px 10px;border-radius:999px;font-size:11px;">
                                        TERSEDIA
                                    </span>
                                @else
                                    <span style="background:#f43f5e;color:white;padding:4px 10px;border-radius:999px;font-size:11px;">
                                        HABIS
                                    </span>
                                @endif
                            </td>

                            <!-- ACTION -->
                            <td class="px-5 py-4 text-center">
                                <div class="flex justify-center gap-2">

                                    <a href="{{ route('admin.products.edit', $product->id) }}"
                                        style="background:#3b82f6;color:white;padding:5px 10px;border-radius:999px;font-size:11px;">
                                        Edit
                                    </a>

                                    <form action="{{ route('admin.products.destroy', $product->id) }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                            style="background:#f43f5e;color:white;padding:5px 10px;border-radius:999px;font-size:11px;">
                                            Hapus
                                        </button>
                                    </form>

                                </div>
                            </td>

                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-10 text-gray-500">
                                Tidak ada produk
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- 📱 MOBILE -->
            <div class="md:hidden p-4 space-y-4">
                @forelse($products as $product)
                <div class="border rounded-xl p-4 shadow-sm">

                    <div class="flex gap-3">
                        @if ($product->gambar)
                            <img src="{{ asset('storage/' . $product->gambar) }}"
                                class="w-16 h-16 object-cover rounded-lg">
                        @endif

                        <div>
                            <p class="font-bold text-gray-800">{{ $product->nama_produk }}</p>
                            <p class="text-xs text-gray-500">
                                {{ Str::limit($product->deskripsi, 50) }}
                            </p>
                        </div>
                    </div>

                    <div class="mt-3 flex justify-between items-center">

                        <div class="flex flex-col gap-1">
                            <span style="background:#10b981;color:white;padding:4px 10px;border-radius:999px;font-size:10px;">
                                Rp {{ number_format($product->harga, 0, ',', '.') }}
                            </span>

                            @if($product->status == 'tersedia')
                                <span style="background:#10b981;color:white;padding:3px 8px;border-radius:999px;font-size:10px;">
                                    TERSEDIA
                                </span>
                            @else
                                <span style="background:#f43f5e;color:white;padding:3px 8px;border-radius:999px;font-size:10px;">
                                    HABIS
                                </span>
                            @endif
                        </div>

                        <div class="flex gap-2">
                            <a href="{{ route('admin.products.edit', $product->id) }}"
                                style="background:#3b82f6;color:white;padding:4px 8px;border-radius:999px;font-size:10px;">
                                Edit
                            </a>

                            <form action="{{ route('admin.products.destroy', $product->id) }}" method="POST">
                                @csrf
                                @method('DELETE')
                                <button type="submit"
                                    style="background:#f43f5e;color:white;padding:4px 8px;border-radius:999px;font-size:10px;">
                                    Hapus
                                </button>
                            </form>
                        </div>

                    </div>

                </div>
                @empty
                <div class="text-center text-gray-500 py-10">
                    Tidak ada produk
                </div>
                @endforelse
            </div>

            <!-- PAGINATION -->
            <div class="p-4 border-t">
                {{ $products->links() }}
            </div>

        </div>

    </div>
</div>
@endsection