<x-app-layout>
    @section('title', 'Kelola Pengguna')

    <div class="space-y-6">
        <!-- Header -->
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-2xl font-bold text-[#2F347A]">Kelola Pengguna</h1>
                <p class="text-sm text-[#7A7FAE] mt-1">Kelola akun dokter dan apoteker</p>
            </div>
            <a href="{{ route('user-management.create') }}" 
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-[#4A538F] text-white rounded-lg hover:bg-[#424B84] transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
                </svg>
                Tambah Pengguna
            </a>
        </div>

        <!-- Filters -->
        <x-card class="p-6">
            <form method="GET" action="{{ route('user-management.index') }}" class="flex gap-4 items-end">
                <div class="flex-1">
                    <label class="block text-sm font-medium text-[#2F347A] mb-1">Cari</label>
                    <input type="text" name="search" value="{{ request('search') }}" placeholder="Cari nama, email, atau NIP..."
                           class="w-full px-4 py-2.5 border border-[#E5E7F2] rounded-lg focus:ring-2 focus:ring-[#4A538F] focus:border-[#4A538F] text-[#2F347A]">
                </div>
                <div>
                    <label class="block text-sm font-medium text-[#2F347A] mb-1">Role</label>
                    <select name="role" class="px-4 py-2.5 border border-[#E5E7F2] rounded-lg focus:ring-2 focus:ring-[#4A538F] focus:border-[#4A538F] bg-white text-[#2F347A]">
                        <option value="">Semua</option>
                        <option value="dokter" {{ request('role') == 'dokter' ? 'selected' : '' }}>Dokter</option>
                        <option value="apoteker" {{ request('role') == 'apoteker' ? 'selected' : '' }}>Apoteker</option>
                    </select>
                </div>
                <button type="submit" class="px-6 py-2.5 bg-[#4A538F] text-white rounded-lg hover:bg-[#424B84] transition-colors">
                    Filter
                </button>
            </form>
        </x-card>

        <!-- Success/Error Messages -->
        @if(session('success'))
            <div class="p-4 bg-[#C9F7E3] border border-[#1F9254] rounded-lg text-[#1F9254]">
                {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="p-4 bg-[#FFD6D6] border border-[#C0392B] rounded-lg text-[#C0392B]">
                {{ session('error') }}
            </div>
        @endif

        <!-- Table -->
        <x-card class="overflow-hidden">
            <div class="px-6 py-4 table-header-custom">
                <h2 class="text-lg font-semibold">Daftar Pengguna</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-[#F4F6FF] border-b border-[#E5E7F2]">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Nama</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Email</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">NIP</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Role</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-[#E5E7F2]">
                        @forelse($users as $user)
                            <tr class="hover:bg-[#F4F6FF]">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="flex items-center gap-3">
                                        @if($user->profile_photo)
                                            <img src="{{ asset('storage/' . $user->profile_photo) }}" alt="Foto" class="w-8 h-8 rounded-full object-cover">
                                        @else
                                            <div class="w-8 h-8 rounded-full bg-[#4A538F] flex items-center justify-center">
                                                <span class="text-xs font-semibold text-white">{{ substr($user->name, 0, 1) }}</span>
                                            </div>
                                        @endif
                                        <span class="font-medium text-[#2F347A]">{{ $user->name }}</span>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-[#7A7FAE]">{{ $user->email }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-[#7A7FAE]">{{ $user->nip ?? '-' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <x-badge :type="$user->role">{{ ucfirst($user->role) }}</x-badge>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="flex items-center gap-2">
                                        <a href="{{ route('user-management.edit', $user) }}" 
                                           class="inline-flex items-center gap-1 px-3 py-1.5 text-sm bg-[#E9EBF5] text-[#4A538F] rounded-lg hover:bg-[#d8dce8] transition-colors">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                            </svg>
                                            Edit
                                        </a>
                                        <form method="POST" action="{{ route('user-management.destroy', $user) }}" 
                                              onsubmit="return confirm('Yakin ingin menghapus akun {{ $user->name }}?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" 
                                                    class="inline-flex items-center gap-1 px-3 py-1.5 text-sm bg-[#FFD6D6] text-[#C0392B] rounded-lg hover:bg-[#ffc4c4] transition-colors">
                                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                                </svg>
                                                Hapus
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="px-6 py-12 text-center text-[#7A7FAE]">
                                    <svg class="w-12 h-12 mx-auto mb-4 text-[#E5E7F2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"/>
                                    </svg>
                                    Tidak ada pengguna ditemukan
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="px-6 py-4 border-t border-[#E5E7F2]">
                {{ $users->withQueryString()->links() }}
            </div>
        </x-card>
    </div>
</x-app-layout>
