@extends('layouts.app')

@section('title', 'Kelola Pengguna')

@section('content')
<div x-data="{
    showModal: false,
    isEditing: false,
    user: { id: null, name: '', email: '', is_admin: 1 },
    formAction: '{{ route('admin.users.store') }}',
    formMethod: 'POST'
}" class="bg-gray-50 min-h-screen">

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

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

        <!-- HEADER -->
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
            <div class="text-white">
                <h1 class="text-2xl md:text-3xl font-bold">👤 Kelola Pengguna</h1>
                <p class="text-blue-100 text-sm">Manajemen admin sistem</p>
            </div>

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

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

                <button
                    @click="isEditing = false; user = { id: null, name: '', email: '', is_admin: 1 }; formAction = '{{ route('admin.users.store') }}'; formMethod = 'POST'; showModal = true;"
                    class="px-4 py-2 rounded-xl bg-white text-blue-700 font-bold shadow hover:shadow-lg transition text-center">
                    + Tambah Admin
                </button>

            </div>
        </div>

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

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

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

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

                            <!-- USER -->
                            <td class="px-5 py-4 whitespace-nowrap">
                                <div class="flex items-center gap-3">
                                    <div class="w-10 h-10 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center font-bold">
                                        {{ substr($user->name, 0, 2) }}
                                    </div>
                                    <div>
                                        <p class="font-bold text-gray-800">{{ $user->name }}</p>
                                        <p class="text-xs text-gray-500">{{ $user->email }}</p>
                                    </div>
                                </div>
                            </td>

                            <!-- ROLE -->
                            <td class="px-5 py-4 text-center">
                                @if ($user->is_admin)
                                    <span class="bg-green-500 text-white px-3 py-1 rounded-full text-xs">Admin</span>
                                @else
                                    <span class="bg-gray-500 text-white px-3 py-1 rounded-full text-xs">User</span>
                                @endif
                            </td>

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

                                    <button
                                        @click="isEditing = true; user = {{ $user->toJson() }}; formAction = '{{ url('admin/users') }}/' + {{ $user->id }}; formMethod = 'PUT'; showModal = true;"
                                        class="bg-blue-500 text-white px-3 py-1.5 rounded-full text-xs hover:bg-blue-600">
                                        Edit
                                    </button>

                                    <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST"
                                        onsubmit="return confirm('Anda yakin ingin menghapus pengguna ini?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                            class="bg-red-500 text-white px-3 py-1.5 rounded-full text-xs hover:bg-red-600">
                                            Hapus
                                        </button>
                                    </form>

                                </div>
                            </td>

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

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

                    <div class="flex items-center gap-3 mb-3">
                        <div class="w-10 h-10 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center font-bold">
                            {{ substr($user->name, 0, 2) }}
                        </div>
                        <div>
                            <p class="font-bold text-gray-800">{{ $user->name }}</p>
                            <p class="text-xs text-gray-500">{{ $user->email }}</p>
                        </div>
                    </div>

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

                        @if ($user->is_admin)
                            <span class="bg-green-500 text-white px-3 py-1 rounded-full text-xs">Admin</span>
                        @else
                            <span class="bg-gray-500 text-white px-3 py-1 rounded-full text-xs">User</span>
                        @endif

                        <div class="flex gap-2">
                            <button
                                @click="isEditing = true; user = {{ $user->toJson() }}; formAction = '{{ url('admin/users') }}/' + {{ $user->id }}; formMethod = 'PUT'; showModal = true;"
                                class="bg-blue-500 text-white px-2 py-1 rounded-full text-xs">
                                Edit
                            </button>

                            <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST"
                                onsubmit="return confirm('Anda yakin ingin menghapus pengguna ini?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit"
                                    class="bg-red-500 text-white px-2 py-1 rounded-full text-xs">
                                    Hapus
                                </button>
                            </form>
                        </div>

                    </div>

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

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

        </div>
    </div>

        <div x-show="showModal" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0"
            x-transition:enter-end="opacity-100" x-transition:leave="transition ease-in duration-200"
            x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0"
            class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center p-4 z-50"
            style="display: none;">
            <div @click.outside="showModal = false" x-show="showModal" x-transition:enter="transition ease-out duration-300"
                x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100"
                x-transition:leave="transition ease-in duration-200" x-transition:leave-start="opacity-100 scale-100"
                x-transition:leave-end="opacity-0 scale-95"
                class="bg-white rounded-xl shadow-xl w-full max-w-lg overflow-hidden">
                <form :action="formAction" method="POST" class="flex flex-col">
                    @csrf
                    <template x-if="isEditing"><input type="hidden" name="_method" value="PUT"></template>


                    <div class="p-6 flex justify-between items-center border-b">
                        <h2 class="text-xl font-bold text-gray-800"
                            x-text="isEditing ? 'Edit Admin' : 'Tambah Admin Baru'"></h2>
                        <button type="button" @click="showModal = false"
                            class="text-gray-400 hover:text-gray-600 p-2 -mr-2">&times;</button>
                    </div>

                    <div class="p-6 space-y-4">
                        <div>
                            <label for="name" class="block text-sm font-medium text-gray-700 mb-1">Nama
                                Lengkap</label>
                            <input type="text" name="name" id="name" x-model="user.name"
                                class="w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500"
                                required>
                        </div>
                        <div>
                            <label for="email" class="block text-sm font-medium text-gray-700 mb-1">Alamat
                                Email</label>
                            <input type="email" name="email" id="email" x-model="user.email"
                                class="w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500"
                                required>
                        </div>
                        <input type="hidden" name="is_admin" value="1">
                        <div class="pt-4">
                            <p class="text-sm text-gray-600 font-medium"
                                x-text="isEditing ? 'Ubah Password (opsional)' : 'Buat Password'"></p>
                            <p class="text-xs text-gray-500"
                                x-text="isEditing ? 'Isi hanya jika ingin mengubah password.' : 'Password wajib diisi untuk akun baru.'">
                            </p>
                        </div>
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="password" class="block text-sm font-medium text-gray-700 mb-1">Password
                                    Baru</label>
                                <input type="password" name="password" id="password"
                                    class="w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500"
                                    :required="!isEditing">
                            </div>
                            <div>
                                <label for="password_confirmation"
                                    class="block text-sm font-medium text-gray-700 mb-1">Konfirmasi Password</label>
                                <input type="password" name="password_confirmation" id="password_confirmation"
                                    class="w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500"
                                    :required="!isEditing">
                            </div>
                        </div>
                    </div>

                    <div class="bg-gray-50 px-6 py-4 flex flex-col-reverse sm:flex-row sm:justify-end gap-3 rounded-b-xl border-t">
                        <button type="button" @click="showModal = false"
                            class="w-full sm:w-auto px-4 py-2 bg-white border border-gray-300 rounded-lg text-sm font-semibold text-gray-700 hover:bg-gray-50 transition">Batal</button>
                        <button type="submit"
                            class="w-full sm:w-auto px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 transition focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                            x-text="isEditing ? 'Simpan Perubahan' : 'Simpan Admin'"></button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

