<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Restok Produk - Glow Beauty</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Poppins', 'Inter', sans-serif; }
        body { margin: 0; padding: 0; width: 100%; overflow-x: hidden; background: linear-gradient(135deg, #fdf2f8 0%, #fff1f3 100%); }

        .nav-link {
            transition: all 0.2s ease;
            padding: 0.5rem 1rem;
            border-radius: 0.5rem;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
        }
        .nav-link:hover { background-color: #fef2f8; color: #ec489a; }
        .active-nav { background-color: #fef2f8; color: #ec489a; font-weight: 600; }

        .btn-primary {
            background: linear-gradient(135deg, #f472b6 0%, #ec489a 100%);
            transition: all 0.3s ease;
        }
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(244, 114, 182, 0.3);
        }

        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid #fbcfe8;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
            border-color: #f9a8d4;
        }

        .stok-bar {
            transition: width 0.8s ease;
        }

        /* Notifikasi dropdown */
        #notifDropdown {
            animation: fadeInDown 0.2s ease;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-10px); }
            to   { opacity: 1; transform: translateY(0); }
        }

        /* Modal */
        #modalRestok {
            backdrop-filter: blur(4px);
        }
        .modal-content {
            animation: slideUp 0.3s ease;
        }
        @keyframes slideUp {
            from { opacity: 0; transform: translateY(40px) scale(0.97); }
            to   { opacity: 1; transform: translateY(0) scale(1); }
        }

        /* Shake animation for bell */
        @keyframes shake {
            0%, 100% { transform: rotate(0); }
            25% { transform: rotate(15deg); }
            75% { transform: rotate(-15deg); }
        }
        .bell-shake { animation: shake 0.4s ease-in-out; }

        /* Dropdown user */
        .dropdown-enter { opacity: 0; transform: scale(0.95); }
        .dropdown-enter-active { opacity: 1; transform: scale(1); transition: opacity 200ms, transform 200ms; }

        /* Animasi peringatan stok habis */
        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }
        .stok-habis-warning {
            animation: pulse 1s ease-in-out infinite;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateX(-100px);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }
        .warning-slide {
            animation: slideIn 0.5s ease-out;
        }
@keyframes bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

.animate-bounce-custom {
    animation: bounce 0.5s ease-in-out infinite;
}

.border-pink-light { border-color: #fbcfe8; }
.bg-gradient-pink { background: linear-gradient(135deg, #fdf2f8 0%, #fff1f3 100%); }
    </style>
</head>

<body class="bg-gradient-pink min-h-screen">

    <!-- ===== NAVBAR ===== -->
    <nav class="bg-white/95 backdrop-blur-md border-b border-pink-light shadow-sm sticky top-0 z-50 w-full">
        <div class="px-4 sm:px-6 lg:px-8 py-3">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between">

                <!-- LOGO -->
                <div class="flex items-center gap-3 mb-3 md:mb-0">
                    <div class="w-10 h-10 bg-gradient-to-br from-pink-400 to-pink-500 rounded-lg flex items-center justify-center shadow-md">
                        <span class="text-white font-bold text-lg">✨</span>
                    </div>
                    <div>
                        <h1 class="font-bold text-lg text-gray-800">Glow Beauty</h1>
                        <p class="text-xs text-pink-400">Manajemen Restok Skincare</p>
                    </div>
                </div>

                <!-- MENU -->
                <div class="flex flex-wrap items-center gap-1">

                    @php
                        // Badge Pesanan Masuk — pesanan pending yang mengandung produk milik penjual ini
                        $pendingPesananCount = \App\Models\Pesanan::where('status', 'pending')
                            ->whereHas('detail', function($q) {
                                $q->whereHas('produk', function($q2) {
                                    $q2->where('user_id', auth()->id());
                                });
                            })
                            ->count();

                        // Badge Restok — produk milik penjual dengan stok < 5
                        $restokCount = \App\Models\Produk::where('user_id', auth()->id())
                            ->where('stok', '<', 5)
                            ->count();

                        // Badge Chat — pesan belum dibaca dari pembeli
                        $unreadChatCount = 0;
                        $chats = \App\Models\Chat::where('penjual_id', auth()->id())->get();
                        foreach ($chats as $chatItem) {
                            $unreadChatCount += \App\Models\ChatMessage::where('chat_id', $chatItem->id)
                                ->where('sender_id', $chatItem->user_id)
                                ->where('is_read', false)
                                ->count();
                        }
                    @endphp

                    <a href="{{ route('dashboard') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-lg
                        {{ request()->routeIs('dashboard') ? 'bg-pink-100 text-pink-600' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
                        transition">
                        <i class="fas fa-home"></i> Dashboard
                    </a>

                    <a href="{{ route('kategori.index') }}"
                        class="px-4 py-2 rounded-lg
                        {{ request()->routeIs('kategori.*') ? 'bg-pink-500 text-white shadow' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
                        transition">
                        <i class="fas fa-tags mr-2"></i>Kategori
                    </a>

                    <a href="{{ route('produk.index') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-lg
                        {{ request()->routeIs('produk.*') ? 'bg-pink-100 text-pink-600' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
                        transition">
                        <i class="fas fa-store"></i> Produk
                    </a>

                    {{-- Pesanan Masuk --}}
                    <a href="{{ route('pesanan.masuk.index') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-lg
                        {{ request()->routeIs('pesanan.*') ? 'bg-pink-100 text-pink-600' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
                        transition">
                        <i class="fas fa-truck"></i>
                        Pesanan Masuk
                        @if($pendingPesananCount > 0)
                            <span class="bg-red-500 text-white text-xs font-bold px-1.5 py-0.5 rounded-full min-w-[18px] text-center">
                                {{ $pendingPesananCount > 99 ? '99+' : $pendingPesananCount }}
                            </span>
                        @endif
                    </a>

{{-- Restok --}}
<a href="{{ route('restok.index') }}"
    class="flex items-center gap-2 px-4 py-2 rounded-lg
    {{ request()->routeIs('restok.*') ? 'bg-pink-100 text-pink-600' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
    transition">
    <i class="fas fa-boxes"></i>
    Restok
    @if($restokCount > 0)
        <span class="bg-red-500 text-white text-xs font-bold px-1.5 py-0.5 rounded-full min-w-[18px] text-center animate-pulse">
            {{ $restokCount > 99 ? '99+' : $restokCount }}
        </span>
    @endif
</a>

{{-- Chat --}}
<a href="{{ route('chat.index') }}"
class="flex items-center gap-2 px-4 py-2 rounded-lg {{ request()->routeIs('chat.*') ? 'bg-pink-100 text-pink-600' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }} transition">
    <i class="fas fa-comments"></i>
    Chat
    @if($unreadChatCount > 0)
        <span class="bg-red-500 text-white text-xs font-bold px-1.5 py-0.5 rounded-full min-w-[18px] text-center animate-pulse">
            {{ $unreadChatCount > 99 ? '99+' : $unreadChatCount }}
        </span>
    @endif
</a>

                    <a href="{{ route('user.index') }}"
                        class="px-4 py-2 rounded-lg
                        {{ request()->routeIs('user.*') ? 'bg-pink-500 text-white shadow' : 'text-gray-600 hover:bg-pink-50 hover:text-pink-600' }}
                        transition">
                        <i class="fas fa-users mr-2"></i>User
                    </a>

                </div>

                <!-- USER MENU -->
                <div class="flex items-center gap-2 mt-3 md:mt-0">

                    <!-- NOTIFIKASI STOK -->
                    <div class="relative" id="notifWrapper">
                        <button onclick="toggleNotif()" id="bellBtn"
                            class="relative p-2.5 text-gray-600 hover:text-pink-600 hover:bg-pink-50 rounded-full transition-all duration-200">
                            <i class="fas fa-bell text-lg" id="bellIcon"></i>
                            <span id="notifBadge"
                                class="absolute top-1 right-1 w-4 h-4 bg-red-500 rounded-full text-white text-xs hidden items-center justify-center">
                                0
                            </span>
                        </button>

                        <!-- DROPDOWN NOTIFIKASI -->
                        <div id="notifDropdown"
                            class="hidden absolute right-0 mt-2 w-80 bg-white rounded-xl shadow-2xl border border-pink-100 z-50 overflow-hidden">
                            <div class="bg-gradient-to-r from-pink-400 to-pink-500 px-4 py-3 flex items-center justify-between">
                                <span class="text-white font-semibold text-sm flex items-center gap-2">
                                    <i class="fas fa-bell"></i> Stok Skincare Hampir Habis
                                </span>
                                <a href="{{ route('restok.index') }}" class="text-white/80 hover:text-white text-xs underline">
                                    Lihat Semua
                                </a>
                            </div>
                            <div id="notifList" class="max-h-64 overflow-y-auto divide-y divide-pink-50">
                                <div class="px-4 py-6 text-center text-gray-400 text-sm">
                                    <i class="fas fa-spinner fa-spin mr-2"></i> Memuat...
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- USER DROPDOWN -->
                    <div class="relative" id="userDropdown">
                        <button onclick="toggleDropdown()"
                            class="flex items-center gap-3 px-3 py-2 hover:bg-pink-50 rounded-xl transition-all duration-200 border border-transparent hover:border-pink-200 group">
                            <div class="w-9 h-9 bg-gradient-to-br from-pink-400 to-pink-500 rounded-full flex items-center justify-center text-white font-semibold shadow-md">
                                {{ substr(Auth::user()->name, 0, 1) }}
                            </div>
                            <div class="hidden md:block text-left">
                                <p class="text-sm font-medium text-gray-700 group-hover:text-pink-600">{{ Auth::user()->name }}</p>
                                <p class="text-xs text-gray-500">
                                    @if(Auth::user()->role == 'penjual') <i class="fas fa-store mr-1 text-pink-500"></i> Penjual
                                    @elseif(Auth::user()->role == 'admin') <i class="fas fa-shield-alt mr-1 text-purple-500"></i> Admin
                                    @else <i class="fas fa-user mr-1 text-pink-500"></i> Customer @endif
                                </p>
                            </div>
                            <i class="fas fa-chevron-down text-sm text-gray-400 group-hover:text-pink-600 transition-transform duration-200" id="dropdownIcon"></i>
                        </button>

                        <div id="dropdownMenu" class="hidden absolute right-0 mt-2 w-64 bg-white rounded-xl shadow-xl border border-gray-100 py-2 z-50">
                            <div class="px-4 py-4 border-b border-gray-100">
                                <div class="flex items-center gap-3">
                                    <div class="w-12 h-12 bg-gradient-to-br from-pink-400 to-pink-500 rounded-full flex items-center justify-center text-white font-bold text-xl shadow-md">
                                        {{ substr(Auth::user()->name, 0, 1) }}
                                    </div>
                                    <div>
                                        <p class="font-semibold text-gray-800">{{ Auth::user()->name }}</p>
                                        <p class="text-sm text-gray-500">{{ Auth::user()->email }}</p>
                                        <span class="inline-block mt-1 px-2 py-0.5 rounded-full text-xs font-medium
                                            @if(Auth::user()->role == 'penjual') bg-pink-100 text-pink-600
                                            @elseif(Auth::user()->role == 'admin') bg-purple-100 text-purple-600
                                            @else bg-pink-100 text-pink-600 @endif">
                                            @if(Auth::user()->role == 'penjual') <i class="fas fa-store mr-1"></i> Penjual
                                            @elseif(Auth::user()->role == 'admin') <i class="fas fa-shield-alt mr-1"></i> Admin
                                            @else <i class="fas fa-user mr-1"></i> Customer @endif
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <a href="{{ route('profile.edit') }}" class="flex items-center gap-3 px-4 py-3 hover:bg-pink-50 text-gray-700 transition group">
                                <div class="w-8 h-8 bg-gray-100 rounded-lg flex items-center justify-center group-hover:bg-pink-100">
                                    <i class="fas fa-user text-gray-500 group-hover:text-pink-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium">Profil Saya</p>
                                    <p class="text-xs text-gray-500">Lihat dan edit profil</p>
                                </div>
                            </a>

                            @if(Auth::user()->role == 'admin')
                            <a href="{{ route('user.index') }}" class="flex items-center gap-3 px-4 py-3 hover:bg-pink-50 text-gray-700 transition group">
                                <div class="w-8 h-8 bg-gray-100 rounded-lg flex items-center justify-center group-hover:bg-pink-100">
                                    <i class="fas fa-users text-gray-500 group-hover:text-pink-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium">Kelola User</p>
                                    <p class="text-xs text-gray-500">Atur pengguna</p>
                                </div>
                            </a>
                            <a href="{{ route('kategori.index') }}" class="flex items-center gap-3 px-4 py-3 hover:bg-pink-50 text-gray-700 transition group">
                                <div class="w-8 h-8 bg-gray-100 rounded-lg flex items-center justify-center group-hover:bg-pink-100">
                                    <i class="fas fa-tags text-gray-500 group-hover:text-pink-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium">Kelola Kategori</p>
                                    <p class="text-xs text-gray-500">Atur kategori produk skincare</p>
                                </div>
                            </a>
                            @endif

                            <div class="border-t border-gray-100 mt-1 pt-1">
                                <form method="POST" action="{{ route('logout') }}">
                                    @csrf
                                    <button type="submit" class="w-full flex items-center gap-3 px-4 py-3 hover:bg-red-50 text-red-600 transition group">
                                        <div class="w-8 h-8 bg-red-50 rounded-lg flex items-center justify-center group-hover:bg-red-100">
                                            <i class="fas fa-sign-out-alt text-red-500"></i>
                                        </div>
                                        <div class="text-left">
                                            <p class="font-medium">Keluar</p>
                                            <p class="text-xs text-red-400">Akhiri sesi</p>
                                        </div>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <!-- ===== KONTEN UTAMA ===== -->
    <div class="w-full px-4 sm:px-6 lg:px-8 py-6">

        <!-- HEADER -->
        <div class="relative mb-8 bg-gradient-to-r from-pink-400 via-pink-500 to-pink-600 rounded-2xl p-6 text-white shadow-lg overflow-hidden">
            <div class="absolute top-0 right-0 opacity-10">
                <i class="fas fa-boxes text-9xl absolute top-4 right-8"></i>
                <i class="fas fa-spa text-7xl absolute bottom-0 right-32"></i>
            </div>
            <div class="relative flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
                <div>
                    <div class="flex items-center gap-3 mb-2">
                        <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                            <i class="fas fa-boxes text-white text-2xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold">Manajemen Restok Skincare</h2>
                    </div>
                    <p class="text-pink-100">Pantau dan tambah stok produk skincare Anda</p>
                    <div class="flex items-center gap-4 mt-3">
                        <span class="bg-white/20 px-3 py-1.5 rounded-lg text-sm flex items-center gap-2">
                            <i class="fas fa-exclamation-triangle"></i>
                            Hampir Habis: <span class="font-bold ml-1">{{ $produksHampirHabis->count() }}</span>
                        </span>
                        <span class="bg-white/20 px-3 py-1.5 rounded-lg text-sm flex items-center gap-2">
                            <i class="fas fa-box"></i>
                            Total Produk Skincare: <span class="font-bold ml-1">{{ $semuaProduk->count() }}</span>
                        </span>
                    </div>
                </div>
                    <button onclick="openModal(null)"
                        class="inline-flex items-center gap-2 border-2 border-white/30 text-white bg-white/20 backdrop-blur-sm px-6 py-2.5 rounded-xl font-semibold hover:bg-white/30 transition-all action-btn">
                        <i class="fas fa-plus-circle"></i> Tambah Stok Produk
                    </button>
            </div>
        </div>

        {{-- ALERT PERINGATAN STOK HABIS --}}
        @php
            $produkStokHabis = $semuaProduk->filter(function($p) {
                return $p->stok == 0;
            });
        @endphp
        
        @if($produkStokHabis->count() > 0)
        <div class="mb-6 warning-slide">
            <div class="bg-red-50 border-l-4 border-red-500 rounded-xl p-5 shadow-md">
                <div class="flex items-start gap-4">
                    <div class="flex-shrink-0">
                        <div class="w-12 h-12 bg-red-100 rounded-full flex items-center justify-center stok-habis-warning">
                            <i class="fas fa-exclamation-triangle text-red-500 text-xl"></i>
                        </div>
                    </div>
                    <div class="flex-1">
                        <div class="flex items-center gap-2 mb-2">
                            <h4 class="font-bold text-red-700 text-lg">⚠️ PERINGATAN STOK HABIS!</h4>
                            <span class="bg-red-500 text-white text-xs font-bold px-2 py-1 rounded-full">
                                {{ $produkStokHabis->count() }} Produk
                            </span>
                        </div>
                        <p class="text-red-600 text-sm mb-3">Berikut adalah produk skincare yang stoknya sudah habis (0 unit). Segera lakukan restok untuk memenuhi pesanan!</p>
                        <div class="flex flex-wrap gap-2">
                            @foreach($produkStokHabis as $p)
                                <div class="bg-white rounded-lg px-3 py-2 flex items-center gap-2 shadow-sm border border-red-200">
                                    <i class="fas fa-box-open text-red-500 text-xs"></i>
                                    <span class="text-sm font-medium text-gray-700">{{ $p->nama_produk }}</span>
                                    <button onclick="openModal('{{ $p->id }}', '{{ $p->nama_produk }}', '{{ $p->stok }}')"
                                        class="bg-red-500 hover:bg-red-600 text-white text-xs font-semibold px-3 py-1 rounded-lg transition">
                                        Restok Sekarang
                                    </button>
                                </div>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @endif

        @if(session('success'))
        <div id="toast-success"
            class="fixed bottom-6 right-6 z-50 flex items-center gap-3 bg-white border border-green-200 shadow-2xl rounded-2xl px-5 py-4 max-w-sm w-full"
            style="animation: slideInRight 0.4s ease;">
            <div class="w-10 h-10 bg-green-500 rounded-xl flex items-center justify-center flex-shrink-0">
                <i class="fas fa-check text-white text-sm"></i>
            </div>
            <div class="flex-1 min-w-0">
                <p class="font-semibold text-gray-800 text-sm">Berhasil!</p>
                <p class="text-gray-500 text-xs mt-0.5">{{ session('success') }}</p>
            </div>
            <button onclick="closeToast()" class="text-gray-400 hover:text-gray-600 flex-shrink-0 ml-1">
                <i class="fas fa-times text-xs"></i>
            </button>
            <div class="absolute bottom-0 left-0 h-1 bg-green-500 rounded-b-2xl" id="toastProgress"
                style="width: 100%; transition: width 5s linear;"></div>
        </div>

        <style>
        @keyframes slideInRight {
            from { opacity: 0; transform: translateX(100px); }
            to   { opacity: 1; transform: translateX(0); }
        }
        @keyframes slideOutRight {
            from { opacity: 1; transform: translateX(0); }
            to   { opacity: 0; transform: translateX(100px); }
        }
        </style>

        <script>
            setTimeout(() => {
                const bar = document.getElementById('toastProgress');
                if (bar) bar.style.width = '0%';
            }, 100);

            setTimeout(() => closeToast(), 5100);

            function closeToast() {
                const toast = document.getElementById('toast-success');
                if (toast) {
                    toast.style.animation = 'slideOutRight 0.3s ease forwards';
                    setTimeout(() => toast.remove(), 300);
                }
            }
        </script>
        @endif

        @if($errors->any())
            <div class="mb-6 p-4 bg-red-50 border-l-4 border-red-500 rounded-r-lg flex items-center shadow-md">
                <div class="w-10 h-10 bg-red-100 rounded-full flex items-center justify-center mr-3">
                    <i class="fas fa-times-circle text-red-600 text-xl"></i>
                </div>
                <div class="flex-1">
                    <p class="font-medium text-red-800">Terjadi Kesalahan!</p>
                    <ul class="text-red-700 text-sm mt-1 list-disc list-inside">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @endif

        <div class="grid grid-cols-1 xl:grid-cols-3 gap-6">

            <!-- ===== KOLOM KIRI: PRODUK HAMPIR HABIS ===== -->
            <div class="xl:col-span-2">
                <div class="bg-white rounded-xl shadow-lg border border-pink-100 overflow-hidden">
         <div class="p-5 border-b border-pink-100 flex items-center justify-between">
    <div class="flex items-center gap-3">
        <div class="w-10 h-10 bg-red-100 rounded-lg flex items-center justify-center">
            <i class="fas fa-exclamation-triangle text-red-500"></i>
        </div>
        <div>
            <h3 class="font-bold text-gray-800">Produk Skincare Hampir Habis</h3>
            <p class="text-xs text-gray-500">Stok tersisa ≤ 5 unit</p>
        </div>
    </div>
    @if($produksHampirHabis->count() > 0)
        <span class="bg-red-100 text-red-600 text-sm font-semibold px-3 py-1 rounded-full">
            {{ $produksHampirHabis->count() }} produk
        </span>
    @endif
</div>

                    @if($produksHampirHabis->isEmpty())
                        <div class="p-12 text-center">
                            <div class="w-20 h-20 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">
                                <i class="fas fa-check-circle text-green-500 text-3xl"></i>
                            </div>
                            <h4 class="font-bold text-gray-700 text-lg mb-1">Semua Stok Aman!</h4>
                            <p class="text-gray-500 text-sm">Tidak ada produk skincare dengan stok kritis saat ini.</p>
                        </div>
                    @else
                        <div class="divide-y divide-pink-50">
                            @foreach($produksHampirHabis as $p)
                                @php
                                    $firstFoto = null;
                                    if ($p->foto) {
                                        foreach (explode('|', $p->foto) as $item) {
                                            if (str_contains($item, ':')) {
                                                [, $firstFoto] = explode(':', $item);
                                                break;
                                            }
                                        }
                                    }
                                    $stokColor = $p->stok == 0 ? 'bg-red-500' : ($p->stok <= 2 ? 'bg-red-400' : 'bg-yellow-400');
                                    $stokPersen = $p->stok == 0 ? 0 : min(100, ($p->stok / 5) * 100);
                                    $barColor = $p->stok == 0 ? 'bg-red-500' : ($p->stok <= 2 ? 'bg-red-400' : 'bg-yellow-400');
                                @endphp

                                <div class="p-4 hover:bg-pink-50/30 transition-colors">
                                    <div class="flex items-center gap-4">
                                        <!-- FOTO -->
                                        <div class="w-14 h-14 rounded-xl overflow-hidden bg-gray-100 flex-shrink-0">
                                            @if($firstFoto && file_exists(public_path('storage/'.$firstFoto)))
                                                <img src="{{ asset('storage/'.$firstFoto) }}" class="w-full h-full object-cover" alt="{{ $p->nama_produk }}">
                                            @else
                                                <div class="w-full h-full flex items-center justify-center">
                                                    <i class="fas fa-image text-gray-400 text-xl"></i>
                                                </div>
                                            @endif
                                        </div>

                                        <!-- INFO -->
                                        <div class="flex-1 min-w-0">
                                            <div class="flex items-start justify-between gap-2">
                                                <div class="min-w-0">
                                                    <h4 class="font-semibold text-gray-800 truncate">{{ $p->nama_produk }}</h4>
                                                    <p class="text-xs text-gray-500 mt-0.5">
                                                        {{ $p->kategori->nama ?? 'Tanpa Kategori' }} &bull;
                                                        Rp {{ number_format($p->harga, 0, ',', '.') }}
                                                    </p>
                                                </div>
                                                <div class="flex items-center gap-2 flex-shrink-0">
                                                    @if($p->stok == 0)
                                                        <span class="inline-flex items-center gap-1 bg-red-500 text-white text-xs font-bold px-3 py-1.5 rounded-full stok-habis-warning">
                                                            <i class="fas fa-times-circle"></i>
                                                            HABIS!
                                                        </span>
                                                    @else
                                                        <span class="inline-flex items-center gap-1 {{ $stokColor }} text-white text-xs font-bold px-3 py-1.5 rounded-full">
                                                            <i class="fas fa-layer-group"></i>
                                                            {{ $p->stok }} unit
                                                        </span>
                                                    @endif
                                                </div>
                                            </div>

                                            <div class="mt-2">
                                                <div class="h-2 bg-gray-100 rounded-full overflow-hidden">
                                                    <div @style(['width: ' . $stokPersen . '%']) 
                                                        @class(['h-full stok-bar rounded-full', $barColor])>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="mt-3 flex gap-2">
                                                <button type="button"
                                                    data-id="{{ $p->id }}"
                                                    data-nama="{{ $p->nama_produk }}"
                                                    data-stok="{{ $p->stok }}"
                                                    onclick="openModal(this.dataset.id, this.dataset.nama, this.dataset.stok)"
                                                    class="flex-1 {{ $p->stok == 0 ? 'bg-red-500 hover:bg-red-600' : 'btn-primary' }} text-white text-xs font-semibold py-2 px-3 rounded-lg flex items-center justify-center gap-1">
                                                    <i class="fas fa-plus"></i>
                                                    Tambah Stok
                                                </button>

                                                <a href="{{ route('restok.show', $p->id) }}"
                                                    class="flex-1 bg-gray-100 text-gray-700 text-xs font-semibold py-2 px-3 rounded-lg flex items-center justify-center gap-1 hover:bg-gray-200 transition">
                                                    <i class="fas fa-history"></i>
                                                    Riwayat
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>

                <!-- SEMUA PRODUK (untuk restok produk yang tidak hampir habis) -->
                <div class="bg-white rounded-xl shadow-lg border border-pink-100 overflow-hidden mt-6">
              <div class="p-5 border-b border-pink-100 flex items-center gap-3">
    <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center">
        <i class="fas fa-cubes text-blue-500"></i>
    </div>
    <div>
        <h3 class="font-bold text-gray-800">Semua Produk Skincare</h3>
        <p class="text-xs text-gray-500">Restok produk skincare mana saja kapan saja</p>
    </div>
</div>

                    <div class="divide-y divide-pink-50 max-h-96 overflow-y-auto">
                        @foreach($semuaProduk as $p)
                            @php
                                $firstFoto = null;
                                if ($p->foto) {
                                    foreach (explode('|', $p->foto) as $item) {
                                        if (str_contains($item, ':')) {
                                            [, $firstFoto] = explode(':', $item);
                                            break;
                                        }
                                    }
                                }
                                $stokClass = $p->stok == 0 ? 'bg-red-100 text-red-700 font-bold' : ($p->stok > 10 ? 'bg-green-100 text-green-700' : ($p->stok > 5 ? 'bg-yellow-100 text-yellow-700' : 'bg-red-100 text-red-700'));
                            @endphp

                            <div class="p-3 hover:bg-pink-50 transition flex items-center gap-3">
                                <div class="w-10 h-10 rounded-lg overflow-hidden bg-gray-100 flex-shrink-0">
                                    @if($firstFoto && file_exists(public_path('storage/'.$firstFoto)))
                                        <img src="{{ asset('storage/'.$firstFoto) }}" class="w-full h-full object-cover" alt="{{ $p->nama_produk }}">
                                    @else
                                        <div class="w-full h-full flex items-center justify-center">
                                            <i class="fas fa-image text-gray-300 text-sm"></i>
                                        </div>
                                    @endif
                                </div>
                                <div class="flex-1 min-w-0">
                                    <p class="font-medium text-gray-800 text-sm truncate">{{ $p->nama_produk }}</p>
                                    <p class="text-xs text-gray-500">{{ $p->kategori->nama ?? '-' }}</p>
                                </div>
                                <span class="text-xs font-semibold px-2 py-1 rounded-full {{ $stokClass }}">
                                    @if($p->stok == 0)
                                        <i class="fas fa-exclamation-circle mr-1"></i> HABIS!
                                    @else
                                        {{ $p->stok }} unit
                                    @endif
                                </span>
                                <button type="button"
                                    data-id="{{ $p->id }}"
                                    data-nama="{{ $p->nama_produk }}"
                                    data-stok="{{ $p->stok }}"
                                    onclick="openModal(this.dataset.id, this.dataset.nama, this.dataset.stok)"
                                    class="{{ $p->stok == 0 ? 'bg-red-500 hover:bg-red-600' : 'btn-primary' }} text-white text-xs font-semibold py-1.5 px-3 rounded-lg flex items-center gap-1 flex-shrink-0">
                                    <i class="fas fa-plus"></i>
                                    Restok
                                </button>
                            </div>
                        @endforeach
                    </div>
                </div>
            </div>

            <!-- ===== KOLOM KANAN: RIWAYAT RESTOK ===== -->
            <div>
                <div class="bg-white rounded-xl shadow-lg border border-pink-100 overflow-hidden sticky top-24">
                  <div class="p-5 border-b border-pink-100 flex items-center gap-3">
                        <div class="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-history text-purple-500"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800">Riwayat Restok</h3>
                            <p class="text-xs text-gray-500">10 aktivitas terakhir</p>
                        </div>
                    </div>
                    @if($riwayatRestok->isEmpty())
                        <div class="p-8 text-center">
                            <i class="fas fa-history text-gray-300 text-4xl mb-3"></i>
                            <p class="text-gray-500 text-sm">Belum ada riwayat restok.</p>
                        </div>
                    @else
                        <div class="divide-y divide-pink-50 max-h-[600px] overflow-y-auto">
                            @foreach($riwayatRestok as $log)
                                <div class="p-4">
                                    <div class="flex items-start gap-3">
                                        <div class="w-9 h-9 bg-green-100 rounded-full flex items-center justify-center flex-shrink-0 mt-0.5">
                                            <i class="fas fa-plus text-green-600 text-xs"></i>
                                        </div>
                                        <div class="flex-1 min-w-0">
                                            <p class="font-medium text-gray-800 text-sm truncate">{{ $log->produk->nama_produk ?? 'Produk dihapus' }}</p>
                                            <div class="flex items-center gap-2 mt-1">
                                                <span class="text-xs text-gray-500">{{ $log->stok_sebelum }}</span>
                                                <i class="fas fa-arrow-right text-gray-400 text-xs"></i>
                                                <span class="text-xs font-bold text-green-600">{{ $log->stok_sesudah }}</span>
                                                <span class="text-xs bg-green-100 text-green-700 px-1.5 py-0.5 rounded-full font-semibold">
                                                    +{{ $log->jumlah_tambah }}
                                                </span>
                                            </div>
                                            @if($log->catatan)
                                                <p class="text-xs text-gray-400 mt-1 italic truncate">{{ $log->catatan }}</p>
                                            @endif
                                            <p class="text-xs text-gray-400 mt-1">
                                                {{ $log->created_at->diffForHumans() }}
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- ===== MODAL TAMBAH STOK ===== -->
    <div id="modalRestok" class="fixed inset-0 bg-black/50 z-50 hidden flex items-center justify-center p-4">
        <div class="modal-content bg-white rounded-2xl shadow-2xl w-full max-w-md">

           {{-- MODAL HEADER --}}
<div class="relative bg-gradient-to-r from-pink-400 via-pink-500 to-pink-600 p-5 rounded-t-2xl overflow-hidden">
    {{-- Background Decoration --}}
    <div class="absolute top-0 right-0 w-32 h-32 opacity-10">
        <i class="fas fa-boxes text-7xl absolute top-2 right-2"></i>
    </div>
    
    <div class="relative flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="w-10 h-10 bg-white/20 backdrop-blur-sm rounded-xl flex items-center justify-center shadow-inner border border-white/30">
                <i class="fas fa-boxes text-white text-lg"></i>
            </div>
            <div>
                <h3 class="text-white font-bold text-xl">Tambah Stok</h3>
                <p class="text-pink-100 text-xs mt-0.5">Isi jumlah stok skincare yang akan ditambahkan</p>
            </div>
        </div>
        <button onclick="closeModal()" 
            class="text-white/70 hover:text-white transition-all duration-200 p-2 hover:bg-white/20 rounded-xl">
            <i class="fas fa-times text-lg"></i>
        </button>
    </div>
</div>

            <!-- MODAL BODY -->
            <form action="{{ route('restok.store') }}" method="POST" id="formRestok">
                @csrf
                <div class="p-5 space-y-4">

                    <!-- PILIH PRODUK -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">
                            <i class="fas fa-box text-pink-500 mr-1"></i>
                            Pilih Produk Skincare
                        </label>
                        <select name="produk_id" id="modalProdukId"
                            onchange="updateStokInfo(this)"
                            class="w-full border border-pink-200 rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-pink-500 focus:border-transparent" required>
                            <option value="">-- Pilih Produk --</option>
                            @foreach($semuaProduk as $p)
                                <option value="{{ $p->id }}" data-stok="{{ $p->stok }}">
                                    {{ $p->nama_produk }} (Stok: {{ $p->stok }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- INFO STOK SAAT INI -->
                    <div id="stokInfoBox" class="hidden">
                        <div class="bg-pink-50 border border-pink-200 rounded-lg p-4">
                            <div class="flex items-center justify-between">
                                <div>
                                    <p class="text-xs text-gray-500 mb-1">Stok Saat Ini</p>
                                    <p class="text-2xl font-bold text-pink-600" id="stokSaatIni">0</p>
                                    <p class="text-xs text-gray-500">unit tersedia</p>
                                </div>
                                <div class="w-14 h-14 bg-pink-100 rounded-xl flex items-center justify-center">
                                    <i class="fas fa-layer-group text-pink-500 text-2xl"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- JUMLAH TAMBAH -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">
                            <i class="fas fa-plus-circle text-green-500 mr-1"></i>
                            Jumlah Tambah Stok
                        </label>
                        <div class="flex items-center gap-3">
                            <button type="button" onclick="decrementStok()"
                                class="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center text-gray-700 hover:bg-gray-200 transition text-lg font-bold flex-shrink-0">
                                −
                            </button>
                            <input type="number" name="jumlah_tambah" id="jumlahTambah"
                                value="1" min="1" max="10000"
                                oninput="updatePreview()"
                                class="flex-1 text-center border border-pink-200 rounded-lg px-3 py-2.5 text-sm font-bold text-lg focus:outline-none focus:ring-2 focus:ring-pink-500" required>
                            <button type="button" onclick="incrementStok()"
                                class="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center text-gray-700 hover:bg-gray-200 transition text-lg font-bold flex-shrink-0">
                                +
                            </button>
                        </div>

                        <!-- QUICK BUTTONS -->
                        <div class="flex gap-2 mt-2">
                            @foreach([5, 10, 25, 50, 100] as $qty)
                            <button type="button" 
                                data-qty="{{ $qty }}"
                                onclick="setJumlah(this.dataset.qty)"
                                class="flex-1 text-xs bg-gray-100 text-gray-600 py-1.5 rounded-lg hover:bg-pink-100 hover:text-pink-600 transition font-medium">
                                +{{ $qty }}
                            </button>
                            @endforeach
                        </div>
                    </div>

                    <!-- PREVIEW STOK SETELAH -->
                    <div id="previewBox" class="hidden bg-green-50 border border-green-200 rounded-lg p-3">
                        <div class="flex items-center justify-between">
                            <div class="text-sm text-gray-600">
                                <span id="previewSebelum" class="font-medium">0</span>
                                <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                                <span id="previewSesudah" class="font-bold text-green-600 text-lg">0</span>
                                <span class="text-xs text-gray-500 ml-1">unit</span>
                            </div>
                            <span class="bg-green-100 text-green-700 text-xs font-bold px-2 py-1 rounded-full" id="previewTambah">+0</span>
                        </div>
                    </div>

                    <!-- CATATAN -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">
                            <i class="fas fa-sticky-note text-blue-500 mr-1"></i>
                            Catatan <span class="text-gray-400 font-normal">(opsional)</span>
                        </label>
                        <textarea name="catatan" rows="2"
                            placeholder="Contoh: Restock dari supplier utama..."
                            class="w-full border border-pink-200 rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-pink-500 resize-none"></textarea>
                    </div>
                </div>

                <!-- MODAL FOOTER -->
                <div class="p-5 pt-0 flex gap-3">
                    <button type="submit"
                        class="flex-1 btn-primary text-white py-3 rounded-xl font-semibold flex items-center justify-center gap-2 shadow-lg">
                                <i class="fas fa-save"></i>
                        Simpan Restok
                    </button>
                    <button type="button" onclick="closeModal()"
                        class="flex-1 bg-gray-100 text-gray-700 py-3 rounded-xl font-semibold hover:bg-gray-200 transition">
                        Batal
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // ===== DATA PRODUK (untuk preview) =====
        const produkData = {
            @foreach($semuaProduk as $p)
            {{ $p->id }}: { nama: '{{ addslashes($p->nama_produk) }}', stok: {{ $p->stok }} },
            @endforeach
        };

        // ===== MODAL =====
        function openModal(produkId = null, namaProduk = null, stokSaatIni = null) {
            document.getElementById('modalRestok').classList.remove('hidden');
            document.getElementById('modalRestok').style.display = 'flex';
            document.body.style.overflow = 'hidden';

            if (produkId) {
                document.getElementById('modalProdukId').value = produkId;
                showStokInfo(stokSaatIni || produkData[produkId]?.stok || 0);
            } else {
                document.getElementById('stokInfoBox').classList.add('hidden');
                document.getElementById('previewBox').classList.add('hidden');
            }

            document.getElementById('jumlahTambah').value = 1;
            updatePreview();
        }

        function closeModal() {
            document.getElementById('modalRestok').classList.add('hidden');
            document.getElementById('modalRestok').style.display = '';
            document.body.style.overflow = '';
        }

        // Close modal on backdrop click
        document.getElementById('modalRestok').addEventListener('click', function(e) {
            if (e.target === this) closeModal();
        });

        // ===== STOK INFO =====
        function showStokInfo(stok) {
            document.getElementById('stokInfoBox').classList.remove('hidden');
            document.getElementById('stokSaatIni').textContent = stok;
            updatePreview();
        }

        function updateStokInfo(select) {
            const option = select.options[select.selectedIndex];
            const stok = option ? parseInt(option.getAttribute('data-stok')) || 0 : 0;
            if (select.value) {
                showStokInfo(stok);
            } else {
                document.getElementById('stokInfoBox').classList.add('hidden');
                document.getElementById('previewBox').classList.add('hidden');
            }
        }

        function updatePreview() {
            const stokEl = document.getElementById('stokSaatIni');
            const jumlahEl = document.getElementById('jumlahTambah');

            if (!stokEl || document.getElementById('stokInfoBox').classList.contains('hidden')) return;

            const stokSebelum = parseInt(stokEl.textContent) || 0;
            const jumlah = parseInt(jumlahEl.value) || 0;
            const stokSesudah = stokSebelum + jumlah;

            document.getElementById('previewBox').classList.remove('hidden');
            document.getElementById('previewSebelum').textContent = stokSebelum;
            document.getElementById('previewSesudah').textContent = stokSesudah;
            document.getElementById('previewTambah').textContent = '+' + jumlah;
        }

        function incrementStok() {
            const el = document.getElementById('jumlahTambah');
            el.value = (parseInt(el.value) || 0) + 1;
            updatePreview();
        }

        function decrementStok() {
            const el = document.getElementById('jumlahTambah');
            const val = (parseInt(el.value) || 0) - 1;
            el.value = Math.max(1, val);
            updatePreview();
        }

        function setJumlah(n) {
            document.getElementById('jumlahTambah').value = n;
            updatePreview();
        }

        // ===== NOTIFIKASI DROPDOWN =====
        async function loadNotifikasi() {
            try {
                const res = await fetch('{{ route("restok.list") }}');
                const data = await res.json();
                const list = document.getElementById('notifList');
                const badge = document.getElementById('notifBadge');

                if (data.length === 0) {
                    list.innerHTML = `
                        <div class="px-4 py-6 text-center">
                            <i class="fas fa-check-circle text-green-400 text-2xl mb-2 block"></i>
                            <p class="text-gray-500 text-sm">Semua stok skincare aman!</p>
                        </div>`;
                    badge.style.display = 'none';
                } else {
                    badge.style.display = 'flex';
                    badge.textContent = data.length;

                    list.innerHTML = data.map(p => `
                        <a href="{{ route('restok.index') }}" class="flex items-center gap-3 px-4 py-3 hover:bg-pink-50 transition">
                            <div class="w-8 h-8 ${p.stok === 0 ? 'bg-red-100' : 'bg-yellow-100'} rounded-full flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-exclamation ${p.stok === 0 ? 'text-red-500' : 'text-yellow-500'} text-xs"></i>
                            </div>
                            <div class="flex-1 min-w-0">
                                <p class="text-sm font-medium text-gray-800 truncate">${p.nama_produk}</p>
                                <p class="text-xs ${p.stok === 0 ? 'text-red-500 font-semibold' : 'text-yellow-600'}">
                                    ${p.stok === 0 ? '⚠️ Stok habis! Segera restok!' : 'Sisa ' + p.stok + ' unit — segera restok!'}
                                </p>
                            </div>
                        </a>
                    `).join('');
                }
            } catch (e) {
                console.error('Gagal memuat notifikasi:', e);
            }
        }

        function toggleNotif() {
            const dropdown = document.getElementById('notifDropdown');
            const isHidden = dropdown.classList.contains('hidden');
            dropdown.classList.toggle('hidden');
            if (isHidden) loadNotifikasi();
        }

        // Close notif when clicking outside
        window.addEventListener('click', function(e) {
            if (!e.target.closest('#notifWrapper')) {
                document.getElementById('notifDropdown').classList.add('hidden');
            }
            if (!e.target.closest('#userDropdown') && !e.target.closest('#dropdownMenu')) {
                document.getElementById('dropdownMenu')?.classList.add('hidden');
                const icon = document.getElementById('dropdownIcon');
                if (icon) icon.style.transform = 'rotate(0deg)';
            }
        });

        // ===== USER DROPDOWN =====
        function toggleDropdown() {
            const menu = document.getElementById('dropdownMenu');
            const icon = document.getElementById('dropdownIcon');
            menu.classList.toggle('hidden');
            icon.style.transform = menu.classList.contains('hidden') ? 'rotate(0deg)' : 'rotate(180deg)';
        }

        // ===== AUTO LOAD BADGE ON PAGE LOAD =====
        window.addEventListener('DOMContentLoaded', () => {
            loadNotifikasi();
        });

        // Auto-hide success alert
        // @if(session('success'))
        setTimeout(() => {
            const alert = document.querySelector('.bg-green-50');
            if (alert) {
                alert.style.transition = 'opacity 0.5s';
                alert.style.opacity = '0';
                setTimeout(() => alert.remove(), 500);
            }
        }, 5000);
        // @endif
    </script>
</body>
</html>