@extends('admin.layouts.app')
@section('title', 'Users')
@section('page-title', 'Users')

@section('content')
<div x-data="usersManager()" x-init="init()">
    <div class="flex flex-col sm:flex-row sm:items-center gap-3 mb-6">
        <div class="relative flex-1">
            <svg class="absolute left-3 top-2.5 w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
            </svg>
            <input type="text" x-model="search" @input="onSearch()" placeholder="Search users..."
                   class="pl-9 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-indigo-500 outline-none w-full">
        </div>
        <a href="{{ route('admin.users.create') }}"
           class="flex items-center justify-center gap-2 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-sm font-medium flex-shrink-0">
            <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="M12 4v16m8-8H4"/></svg>
            New User
        </a>
    </div>

    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 dark:bg-gray-700/50">
                <tr>
                    <th class="text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Name</th>
                    <th class="hidden sm:table-cell text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Email</th>
                    <th class="text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Role</th>
                    <th class="hidden md:table-cell text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Joined</th>
                    <th class="px-4 py-3"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                <template x-if="loading">
                    <tr><td colspan="5" class="px-6 py-12 text-center">
                        <div class="flex items-center justify-center gap-2 text-gray-400">
                            <svg class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24">
                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
                            </svg>
                            Loading...
                        </div>
                    </td></tr>
                </template>
                <template x-if="!loading">
                    <template x-for="user in items" :key="user.id">
                        <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/30 transition-colors">
                            <td class="px-4 py-3">
                                <div class="flex items-center gap-3">
                                    <div class="w-8 h-8 rounded-full bg-indigo-100 dark:bg-indigo-900/30 flex items-center justify-center text-indigo-700 dark:text-indigo-400 font-medium text-sm flex-shrink-0"
                                         x-text="user.name.charAt(0).toUpperCase()"></div>
                                    <span class="font-medium text-gray-900 dark:text-white truncate" x-text="user.name"></span>
                                </div>
                            </td>
                            <td class="hidden sm:table-cell px-4 py-3 text-gray-600 dark:text-gray-300 truncate max-w-xs" x-text="user.email"></td>
                            <td class="px-4 py-3">
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300 whitespace-nowrap"
                                      x-text="user.role"></span>
                            </td>
                            <td class="hidden md:table-cell px-4 py-3 text-gray-500 dark:text-gray-400 whitespace-nowrap" x-text="user.created_at"></td>
                            <td class="px-4 py-3">
                                <div class="flex items-center justify-end gap-1">
                                <a :href="user.edit_url" class="p-1.5 text-gray-400 hover:text-indigo-600 dark:hover:text-indigo-400 rounded">
                                    <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="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
                                </a>
                                <button x-show="!user.is_self" type="button" @click="deleteUser(user.id)" :disabled="deletingId === user.id"
                                        class="p-1.5 text-gray-400 hover:text-red-600 dark:hover:text-red-400 rounded disabled:opacity-50">
                                    <svg x-show="deletingId === user.id" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                                    <svg x-show="deletingId !== user.id" 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>
                                </button>
                                </div>
                            </td>
                        </tr>
                    </template>
                </template>
                <template x-if="!loading && items.length === 0">
                    <tr><td colspan="5" class="px-6 py-12 text-center text-gray-400">No users found.</td></tr>
                </template>
            </tbody>
        </table>

        <div x-show="meta.last_page > 1" class="px-4 py-4 border-t border-gray-200 dark:border-gray-700 flex flex-wrap items-center justify-between gap-3">
            <p class="text-sm text-gray-500 dark:text-gray-400">Total: <span x-text="meta.total"></span></p>
            <div class="flex items-center gap-1">
                <button @click="fetch(meta.current_page - 1)" :disabled="meta.current_page === 1"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">←</button>
                <template x-for="p in pages()" :key="p">
                    <button @click="fetch(p)"
                            :class="p === meta.current_page ? 'bg-indigo-600 text-white border-indigo-600' : 'border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300'"
                            class="px-3 py-1.5 text-sm rounded border" x-text="p"></button>
                </template>
                <button @click="fetch(meta.current_page + 1)" :disabled="meta.current_page === meta.last_page"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">→</button>
            </div>
        </div>
    </div>
</div>
@endsection
