<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Data Admin & User - Raja Futsal</title>
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            font-family: 'Inter', sans-serif;
        }

        .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: #fff7ed;
            color: #ea580c;
        }

        .active-nav {
            background-color: #fff7ed;
            color: #ea580c;
            font-weight: 600;
        }

        .table-row-hover:hover {
            background-color: #fef3c7;
            transform: translateY(-1px);
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: linear-gradient(135deg, #f97316 0%, #ea580c 100%);
            transition: all 0.3s ease;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(249, 115, 22, 0.3);
        }

        .action-btn {
            transition: all 0.2s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
        }

        .role-badge {
            font-size: 0.75rem;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            display: inline-flex;
            align-items: center;
            gap: 0.25rem;
        }

        .role-admin {
            background: linear-gradient(135deg, #f97316 0%, #ea580c 100%);
            color: white;
        }

        .role-user {
            background-color: #e5e7eb;
            color: #374151;
        }

        .status-active {
            background-color: #d1fae5;
            color: #065f46;
        }

        .status-inactive {
            background-color: #fee2e2;
            color: #991b1b;
        }

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

        /* Notification badge */
        .notification-badge {
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.1); }
            100% { transform: scale(1); }
        }

        /* Shake animation */
        @keyframes shake {
            0%, 100% { transform: rotate(0); }
            25% { transform: rotate(15deg); }
            75% { transform: rotate(-15deg); }
        }
        .group:hover .group-hover\:animate-shake {
            animation: shake 0.3s ease-in-out;
        }

        .stat-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }

        @keyframes bounce {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.2); }
        }

        .animate-bounce-custom {
            animation: bounce 0.5s ease-in-out infinite;
        }
    </style>
</head>

<body class="bg-gray-50 min-h-screen">
    <!-- ========== NAVBAR ATAS (MENU UTAMA) FULL WIDTH ========== -->
    <nav class="bg-white border-b shadow-sm sticky top-0 z-50 w-full">
        <div class="px-4 sm:px-6 lg:px-8 py-3">
            <div class="relative flex flex-col md:flex-row md:items-center md:justify-between">
                <!-- LOGO & TITLE -->
                <div class="flex items-center gap-3 mb-3 md:mb-0">
                    <div class="w-10 h-10 bg-gradient-to-br from-orange-500 to-orange-600 rounded-lg flex items-center justify-center shadow-md">
                        <span class="text-white font-bold text-lg">R</span>
                    </div>
                    <div>
                        <h1 class="font-bold text-lg text-gray-800">Raja Futsal</h1>
                        <p class="text-xs text-gray-500">Manajemen Pengguna</p>
                    </div>
                </div>

                <!-- MENU NAVIGASI DENGAN ICON -->
                <div class="hidden md:flex flex-wrap items-center gap-3 absolute left-1/2 transform -translate-x-1/2">
                    @php
                        $uid = auth()->user()->getAttribute('ID');

                        // Badge Pesanan Masuk (pending + dibayar)
                        $pendingPesananCount = \App\Models\Pesanan::whereIn('status', ['pending', 'dibayar'])
                            ->whereHas('detail', function($q) use ($uid) {
                                $q->whereHas('produk', function($q2) use ($uid) {
                                    $q2->where('user_id', $uid);
                                });
                            })->count();
                    @endphp

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


                    <a href="{{ route('kategori.index') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-xl font-medium transition
                        {{ request()->routeIs('kategori.*') ? 'bg-orange-50 text-orange-600 border border-orange-100' : 'text-gray-600 hover:bg-orange-50 hover:text-orange-600' }}">
                        <i class="fas fa-tags {{ request()->routeIs('kategori.*') ? 'text-orange-500' : '' }}"></i> Kategori
                    </a>

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

                    <a href="{{ route('pesanan.masuk.index') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-xl font-medium transition
                        {{ request()->routeIs('pesanan.masuk.*') ? 'bg-orange-50 text-orange-600 border border-orange-100' : 'text-gray-600 hover:bg-orange-50 hover:text-orange-600' }}">
                        <i class="fas fa-truck {{ request()->routeIs('pesanan.masuk.*') ? 'text-orange-500' : '' }}"></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 notification-badge">
                                {{ $pendingPesananCount > 99 ? '99+' : $pendingPesananCount }}
                            </span>
                        @endif
                    </a>

                    <a href="{{ route('user.index') }}"
                        class="flex items-center gap-2 px-4 py-2 rounded-xl font-medium transition
                        {{ request()->routeIs('user.*') ? 'bg-orange-50 text-orange-600 border border-orange-100' : 'text-gray-600 hover:bg-orange-50 hover:text-orange-600' }}">
                        <i class="fas fa-users {{ request()->routeIs('user.*') ? 'text-orange-500' : '' }}"></i> User
                    </a>
                </div>

                <!-- USER MENU -->
                <div class="flex items-center gap-4 mt-3 md:mt-0">
                    <!-- NOTIF ICONS FOR SELLER -->
                    <!-- Restok Icon -->
                    <a href="{{ route('restok.index') }}" class="relative p-2 text-gray-500 hover:text-orange-600 transition-all duration-200 focus:outline-none bg-gray-50 hover:bg-orange-50 rounded-xl border border-transparent hover:border-orange-100 flex items-center justify-center" title="Restok">
                        <i class="fas fa-boxes text-xl"></i>
                        @if(isset($sellerRestokCount) && $sellerRestokCount > 0)
                            <span class="absolute top-1.5 right-1.5 flex h-4 w-4">
                                <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
                                <span class="relative inline-flex rounded-full h-4 w-4 bg-red-500 text-white text-[10px] items-center justify-center font-bold">
                                    {{ $sellerRestokCount > 99 ? '99+' : $sellerRestokCount }}
                                </span>
                            </span>
                        @endif
                    </a>

                    <!-- Chat Icon -->
                    <a href="{{ route('chat.index') }}" class="relative p-2 text-gray-500 hover:text-orange-600 transition-all duration-200 focus:outline-none bg-gray-50 hover:bg-orange-50 rounded-xl border border-transparent hover:border-orange-100 flex items-center justify-center" title="Chat">
                        <i class="fas fa-comments text-xl"></i>
                        @if(isset($sellerUnreadChatCount) && $sellerUnreadChatCount > 0)
                            <span class="absolute top-1.5 right-1.5 flex h-4 w-4">
                                <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
                                <span class="relative inline-flex rounded-full h-4 w-4 bg-red-500 text-white text-[10px] items-center justify-center font-bold">
                                    {{ $sellerUnreadChatCount > 99 ? '99+' : $sellerUnreadChatCount }}
                                </span>
                            </span>
                        @endif
                    </a>

                    <!-- Ulasan Icon -->
                    <a href="{{ route('ulasan.penjual.index') }}" class="relative p-2 text-gray-500 hover:text-orange-600 transition-all duration-200 focus:outline-none bg-gray-50 hover:bg-orange-50 rounded-xl border border-transparent hover:border-orange-100 flex items-center justify-center" title="Ulasan">
                        <i class="fas fa-star text-xl"></i>
                        @if(isset($sellerUnreadUlasanCount) && $sellerUnreadUlasanCount > 0)
                            <span class="absolute top-1.5 right-1.5 flex h-4 w-4">
                                <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
                                <span class="relative inline-flex rounded-full h-4 w-4 bg-red-500 text-white text-[10px] items-center justify-center font-bold">
                                    {{ $sellerUnreadUlasanCount > 99 ? '99+' : $sellerUnreadUlasanCount }}
                                </span>
                            </span>
                        @endif
                    </a>

                    <!-- USER DROPDOWN -->
                    <div class="relative" id="userDropdown">
                        <button onclick="toggleUserDropdown(event)" 
                        class="flex items-center gap-3 px-3 py-2 hover:bg-orange-50 rounded-xl transition-all duration-200 border border-transparent hover:border-orange-200 group">

                                <div class="w-9 h-9 bg-gradient-to-br from-orange-500 to-orange-600 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-orange-600">
                                        {{ Auth::user()->name }}
                                    </p>
                                   <p class="text-xs text-gray-500 capitalize flex items-center gap-1">
                                        <i class="fas fa-store text-orange-500 mr-1"></i>
                                        Penjual
                                    </p>
                                </div>

                                <i id="dropdownIcon"
                                class="fas fa-chevron-down text-sm text-gray-400 group-hover:text-orange-600 
                                        transition-transform duration-200"></i>
                            </button>

                            <!-- DROPDOWN -->
                            <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">

                                <!-- USER INFO -->
                                <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-orange-500 to-orange-600 
                                                    rounded-full flex items-center justify-center 
                                                    text-white font-bold text-xl">
                                            {{ 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-flex items-center gap-1 mt-1 px-2 py-0.5 
                                                        bg-orange-100 text-orange-600 rounded-full 
                                                        text-xs font-medium">
                                                <i class="fas fa-store text-orange-500 mr-1"></i>
                                                Penjual
                                            </span>
                                        </div>
                                    </div>
                                </div>

                                <!-- PROFILE -->
                                <a href="{{ route('profile.edit') }}"
                                class="flex items-center gap-3 px-4 py-3 hover:bg-orange-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-orange-100">
                                        <i class="fas fa-user text-gray-500 group-hover:text-orange-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>

                                <!-- LOGOUT -->
                                <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>
        </div>
    </nav>

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

        <div class="relative mb-8 overflow-hidden rounded-2xl bg-gradient-to-r from-orange-500 via-orange-600 to-orange-700 p-6 text-white shadow-xl w-full">
            {{-- Background Decorations --}}
            <div class="absolute top-0 right-0 w-64 h-64 opacity-10">
                <i class="fas fa-users text-8xl absolute top-10 right-10"></i>
                <i class="fas fa-user-shield text-7xl absolute bottom-0 right-32"></i>
                <i class="fas fa-user-plus text-6xl absolute top-20 right-48"></i>
            </div>
            <div class="absolute bottom-0 left-0 w-48 h-48 bg-white opacity-5 rounded-full -ml-16 -mb-16"></div>
            
            <div class="relative">
                {{-- Top Section --}}
                <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-4">
                    <div class="flex items-center gap-3">
                        <div class="w-12 h-12 bg-white/20 backdrop-blur-sm rounded-xl flex items-center justify-center shadow-inner border border-white/30">
                            <i class="fas fa-users text-white text-2xl"></i>
                        </div>
                        <div>
                            <h2 class="text-2xl font-bold text-white">Manajemen Pengguna</h2>
                            <p class="text-orange-100 text-sm">Kelola semua akun penjual, kurir, dan pengguna sistem</p>
                        </div>
                    </div>
                    
                    <div class="flex-shrink-0">
                        <a href="{{ route('user.create') }}"
                        class="inline-flex items-center gap-2 border-2 border-gray-300 text-gray-600 bg-white px-6 py-2.5 rounded-xl font-semibold hover:border-gray-400 transition-all action-btn">
                            <i class="fas fa-plus-circle"></i>
                            <span>Tambah User</span>
                        </a>
                    </div>
                </div>
                
                {{-- Bottom Section - Stats --}}
                <div class="flex flex-wrap items-center gap-3 pt-4 border-t border-white/20">
                    <div class="flex items-center gap-2 bg-white/20 backdrop-blur-sm px-3 py-1.5 rounded-xl">
                        <i class="fas fa-store text-orange-200 text-xs"></i>
                        <span class="text-orange-100 text-sm">Total Penjual:</span>
                        <span class="font-bold text-white">{{ $totalPenjual ?? 0 }}</span>
                    </div>
                    <div class="flex items-center gap-2 bg-white/20 backdrop-blur-sm px-3 py-1.5 rounded-xl">
                        <i class="fas fa-truck text-orange-200 text-xs"></i>
                        <span class="text-orange-100 text-sm">Total Kurir:</span>
                        <span class="font-bold text-white">{{ $totalKurir ?? 0 }}</span>
                    </div>
                    <div class="flex items-center gap-2 bg-white/20 backdrop-blur-sm px-3 py-1.5 rounded-xl">
                        <i class="fas fa-users text-orange-200 text-xs"></i>
                        <span class="text-orange-100 text-sm">Total User:</span>
                        <span class="font-bold text-white">{{ $totalUser ?? 0 }}</span>
                    </div>
                </div>
            </div>
        </div>
                <!-- MAIN CONTENT CARD -->
                <div class="bg-white rounded-xl shadow-lg border border-gray-100 overflow-hidden w-full">
                        <!-- HEADER CARD -->
                        <div class="px-6 py-4 border-b border-gray-100 bg-gradient-to-r from-orange-50 to-white flex flex-col md:flex-row md:justify-between md:items-center gap-4">
                        <div>
                    <h3 class="font-bold text-gray-800 text-lg flex items-center gap-2">
                        <i class="fas fa-list text-orange-500"></i>
                        Daftar Pengguna
                    </h3>
                    <p class="text-sm text-gray-500 mt-1">Kelola semua akun penjual, kurir, dan user sistem</p>
                </div>

                <div class="flex flex-col sm:flex-row items-stretch sm:items-center gap-3">
                    <!-- SEARCH -->
                    <form method="GET" action="{{ route('user.index') }}" id="searchForm">
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fas fa-search text-gray-400"></i>
                            </div>
                            <input type="text"
                                name="search"
                                id="searchInput"
                                value="{{ request('search') }}"
                                placeholder="Cari user..."
                                onchange="this.form.submit()"
                                class="pl-10 pr-4 py-2.5 border border-gray-900 rounded-lg focus:outline-none focus:ring-2 focus:ring-orange-500 focus:border-transparent text-sm w-full sm:w-64">
                        </div>
                    </form>

                    <!-- RESET -->
                    <a href="{{ route('user.index') }}" title="Reset Filter"
                        class="p-2.5 bg-gray-100 text-gray-700 border border-gray-900 rounded-lg hover:bg-gray-200 transition flex items-center justify-center">
                        <i class="fas fa-undo-alt"></i>
                    </a>
                </div>
            </div>

            <!-- TOAST NOTIFICATION -->
            @if (session('success') || session('error'))
            <div id="toast-notification"
                class="fixed bottom-6 right-6 z-50 flex items-center gap-3 bg-white border {{ session('success') ? 'border-green-200' : 'border-red-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 {{ session('success') ? 'bg-green-500' : 'bg-red-500' }} rounded-xl flex items-center justify-center flex-shrink-0">
                    <i class="fas {{ session('success') ? 'fa-check' : 'fa-exclamation' }} text-white text-sm"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="toast-title font-semibold text-gray-800 text-sm">{{ session('success') ? 'Berhasil!' : 'Gagal!' }}</p>
                    <p class="toast-message text-gray-500 text-xs mt-0.5 truncate">{{ session('success') ?? session('error') }}</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 {{ session('success') ? 'bg-green-500' : 'bg-red-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-notification');
                    if (toast) {
                        toast.style.animation = 'slideOutRight 0.3s ease forwards';
                        setTimeout(() => toast.remove(), 300);
                    }
                }
            </script>
            @endif

            <!-- TABLE -->
            <div class="overflow-x-auto">
                @if($users->isEmpty())
                    <!-- EMPTY STATE -->
                    <div class="py-16 text-center">
                        <div class="mx-auto w-24 h-24 bg-orange-50 rounded-full flex items-center justify-center mb-6">
                            <i class="fas fa-user-friends text-orange-400 text-3xl"></i>
                        </div>
                        <h4 class="text-lg font-medium text-gray-700 mb-2">Belum ada data admin</h4>
                        <p class="text-gray-500 mb-6">Mulai dengan menambahkan admin pertama Anda</p>
                        <a href="{{ route('user.create') }}"
                           class="btn-primary text-white px-5 py-2.5 rounded-lg font-medium inline-flex items-center gap-2">
                            <i class="fas fa-user-plus"></i>
                            Tambah Admin Pertama
                        </a>
                    </div>
                @else
                    <div class="overflow-x-auto">
                        @php
                            $hasManageableUsers = $users->contains(fn($u) => in_array($u->role, ['penjual', 'kurir']));
                        @endphp
                        <table class="w-full">
                            <thead class="bg-orange-50">
                                <tr>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-orange-800 uppercase tracking-wider w-20">
                                        <i class="fas fa-hashtag mr-1"></i>
                                        No
                                    </th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-orange-800 uppercase tracking-wider">
                                        <i class="fas fa-address-book mr-1"></i>
                                        Kontak
                                    </th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-orange-800 uppercase tracking-wider">
                                        <i class="fas fa-tag mr-1"></i>
                                        Role & Status
                                    </th>
                                    @if($hasManageableUsers)
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-orange-800 uppercase tracking-wider w-48">
                                        <i class="fas fa-cog mr-1"></i>
                                        Aksi
                                    </th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100" id="userTableBody">
                                @foreach($users as $index => $u)
                                    <tr class="table-row-hover">
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="text-sm font-medium text-gray-600">
                                            {{ ($users->currentPage() - 1) * $users->perPage() + $loop->iteration }}
                                            </div>
                                        </td>
                                        <td class="px-6 py-4">
                                            <div class="space-y-1">
                                                <div class="flex items-center gap-2">
                                                    <i class="fas fa-envelope text-orange-400 text-xs w-4"></i>
                                                    <span class="text-sm text-gray-700 break-all">{{ $u->email }}</span>
                                                </div>
                                                @if($u->no_hp)
                                                    <div class="flex items-center gap-2">
                                                        <i class="fas fa-phone text-orange-400 text-xs w-4"></i>
                                                        <span class="text-sm text-gray-700">{{ $u->no_hp }}</span>
                                                    </div>
                                                @endif
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="flex flex-col gap-2">
                                                <span class="role-badge {{ $u->role === 'admin' ? 'role-admin' : 'role-user' }}">
                                                    <i class="fas {{ $u->role === 'admin' ? 'fa-user-shield' : 'fa-user' }}"></i>
                                                    {{ ucfirst($u->role) }}
                                                </span>

                                                <!-- Status Active/Inactive -->
                                                @php
                                                    $status = isset($u->status) ? $u->status : 'active';
                                                @endphp
                                                <span class="role-badge {{ $status === 'active' ? 'status-active' : 'status-inactive' }}">
                                                    <i class="fas fa-circle" style="font-size: 6px;"></i>
                                                    {{ ucfirst($status) }}
                                                </span>
                                            </div>
                                        </td>
                                         @if($hasManageableUsers)
                                         <td class="px-6 py-4 whitespace-nowrap">
                                            @if(in_array($u->role, ['penjual', 'kurir']))
                                            <div class="flex items-center gap-2">
                                                <!-- EDIT BUTTON -->
                                                <a href="{{ route('user.edit', $u->ID) }}"
                                                   class="action-btn px-4 py-2 bg-blue-100 text-blue-700 rounded-lg font-medium text-sm hover:bg-blue-200 flex items-center gap-2">
                                                    <i class="fas fa-edit"></i>
                                                    Edit
                                                </a>

                                                <!-- DELETE BUTTON -->
                                                <form action="{{ route('user.destroy', $u->ID) }}" method="POST" class="inline" id="deleteForm{{ $u->ID }}">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="button"
                                                        data-id="{{ $u->ID }}"
                                                        data-nama="{{ $u->nama }}"
                                                        onclick="confirmDelete(this.dataset.id, this.dataset.nama)"
                                                        class="action-btn px-4 py-2 bg-red-100 text-red-700 rounded-lg font-medium text-sm hover:bg-red-200 flex items-center gap-2">
                                                        <i class="fas fa-trash"></i>
                                                        Hapus
                                                    </button>
                                                </form>
                                            </div>
                                            @endif
                                        </td>
                                        @endif
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <!-- PAGINATION -->
                    @if(method_exists($users, 'hasPages') && $users->hasPages())
                        <div class="px-6 py-4 border-t border-gray-100">
                            {{ $users->links() }}
                        </div>
                    @endif
                @endif
            </div>
        </div>
    </div>

    <!-- DELETE CONFIRMATION MODAL -->
    <div id="deleteModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
        <div class="bg-white rounded-2xl p-6 max-w-md w-full mx-4 shadow-2xl transform transition-all">
            <div class="flex items-center gap-4 mb-4">
                <div class="w-14 h-14 bg-red-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-exclamation-triangle text-red-600 text-2xl"></i>
                </div>
                <div>
                    <h3 class="font-bold text-gray-800 text-lg" id="deleteModalTitle">Hapus User?</h3>
                    <p class="text-gray-600 text-sm" id="deleteModalSubtitle"></p>
                </div>
                <button onclick="closeDeleteModal()" class="ml-auto text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>

            <div class="p-4 bg-red-50 rounded-lg mb-6 border border-red-100">
                <p class="text-red-700 text-sm flex items-start gap-2">
                    <i class="fas fa-exclamation-circle mt-0.5"></i>
                    <span>
                        Aksi ini akan menghapus user <strong id="userNameToDelete" class="font-bold"></strong> secara permanen.
                        <br>Semua data terkait user ini juga akan ikut terhapus.
                    </span>
                </p>
            </div>

            <div class="flex justify-end gap-3">
                <button onclick="closeDeleteModal()"
                        class="px-5 py-2.5 text-gray-700 font-medium hover:bg-gray-100 rounded-lg transition border border-gray-300 flex items-center gap-2">
                    <i class="fas fa-times"></i>
                    Batal
                </button>
                <form id="deleteForm" method="POST" class="inline">
                    @csrf
                    @method('DELETE')
                    <button type="submit"
                            class="px-5 py-2.5 bg-red-600 text-white font-medium hover:bg-red-700 rounded-lg transition flex items-center gap-2 shadow-lg hover:shadow-xl">
                        <i class="fas fa-trash"></i>
                        Ya, Hapus
                    </button>
                </form>
            </div>
        </div>
    </div>
<script>
    // Dropdown User
    function toggleUserDropdown(event) { 
        if (event) event.stopPropagation();
        const dropdown = document.getElementById('dropdownMenu');
        const icon = document.getElementById('dropdownIcon');

        if (dropdown.classList.contains('hidden')) {
            dropdown.classList.remove('hidden');
            dropdown.classList.add('dropdown-enter');
            setTimeout(() => {
                dropdown.classList.remove('dropdown-enter');
                dropdown.classList.add('dropdown-enter-active');
            }, 10);
            if(icon) icon.style.transform = 'rotate(180deg)';
        } else {
            dropdown.classList.remove('dropdown-enter-active');
            dropdown.classList.add('dropdown-leave');
            setTimeout(() => {
                dropdown.classList.add('hidden');
                dropdown.classList.remove('dropdown-leave');
            }, 200);
            if(icon) icon.style.transform = 'rotate(0deg)';
        }
    }

    // Close dropdown when clicking outside
    document.addEventListener('click', function(event) {
        const dropdown = document.getElementById('dropdownMenu');
        const userDropdown = document.getElementById('userDropdown');
        if (dropdown && userDropdown && !userDropdown.contains(event.target)) {
            if (!dropdown.classList.contains('hidden')) {
                dropdown.classList.add('hidden');
                const icon = document.getElementById('dropdownIcon');
                if (icon) icon.style.transform = 'rotate(0deg)';
            }
        }
    });

    // Delete Confirmation
    function confirmDelete(userId, userName) {
        document.getElementById('userNameToDelete').innerHTML = userName;
        document.getElementById('deleteModalTitle').innerHTML = 'Hapus ' + userName + '?';
        document.getElementById('deleteModalSubtitle').innerHTML = 'User ID: #' + userId;
        document.getElementById('deleteForm').action = '/user/' + userId;
        document.getElementById('deleteModal').classList.remove('hidden');
        document.getElementById('deleteModal').classList.add('flex');
        document.body.style.overflow = 'hidden';
    }

    function closeDeleteModal() {
        document.getElementById('deleteModal').classList.add('hidden');
        document.getElementById('deleteModal').classList.remove('flex');
        document.body.style.overflow = 'auto';
    }

    document.getElementById('deleteModal').addEventListener('click', function(e) {
        if (e.target.id === 'deleteModal') closeDeleteModal();
    });

    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && !document.getElementById('deleteModal').classList.contains('hidden')) {
            closeDeleteModal();
        }
    });

    // Search server-side dengan debounce
    document.addEventListener('DOMContentLoaded', function() {
        const searchInput = document.getElementById('searchInput');
        if (searchInput) {
            let timeout;
            searchInput.addEventListener('input', function() {
                clearTimeout(timeout);
                timeout = setTimeout(() => {
                    document.getElementById('searchForm').submit();
                }, 500);
            });
        }
    });
</script>
</body>
</html>
