<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between w-full">
            <div>
                <h1 class="text-lg font-semibold text-gray-800">Manage Users & Staff</h1>
                <p class="text-xs text-gray-400 mt-0.5">Add sub-users and assign roles to your business team</p>
            </div>
            <a href="{{ route('business-users.create') }}" class="inline-flex items-center gap-1.5 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-xl text-xs font-semibold transition shadow-sm">
                Add User
            </a>
        </div>
    </x-slot>

    @if(session('success'))
        <div class="bg-emerald-50 border border-emerald-100 rounded-xl p-4 mb-6 flex items-center gap-3">
            <span class="w-8 h-8 rounded-full bg-emerald-100 flex items-center justify-center text-emerald-600">
                <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="M9 12l2 2 4-4" /></svg>
            </span>
            <p class="text-xs font-semibold text-emerald-800">{{ session('success') }}</p>
        </div>
    @endif

    @if(session('error'))
        <div class="bg-red-50 border border-red-100 rounded-xl p-4 mb-6 flex items-center gap-3">
            <span class="w-8 h-8 rounded-full bg-red-100 flex items-center justify-center text-red-600">
                <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="M6 18L18 6M6 6l12 12" /></svg>
            </span>
            <p class="text-xs font-semibold text-red-800">{{ session('error') }}</p>
        </div>
    @endif

    <div class="bg-white rounded-xl border border-gray-100 shadow-sm overflow-hidden">
        <table class="w-full text-left border-collapse text-xs">
            <thead>
                <tr class="bg-gray-50 border-b border-gray-100 text-gray-400 uppercase tracking-wider font-semibold">
                    <th class="px-6 py-3.5">Name</th>
                    <th class="px-6 py-3.5">Email</th>
                    <th class="px-6 py-3.5">Role</th>
                    <th class="px-6 py-3.5 text-right">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-50">
                @forelse($users as $u)
                    <tr class="hover:bg-gray-50 transition">
                        <td class="px-6 py-3.5 font-medium text-gray-800">{{ $u->name }}</td>
                        <td class="px-6 py-3.5 text-gray-500 font-mono">{{ $u->email }}</td>
                        <td class="px-6 py-3.5">
                            <span class="px-2 py-0.5 rounded-lg bg-gray-100 text-gray-600 font-medium">
                                {{ $u->roles->pluck('name')->first() ?: 'No Role Assigned' }}
                            </span>
                        </td>
                        <td class="px-6 py-3.5 text-right flex items-center justify-end gap-3">
                            <a href="{{ route('business-users.edit', $u->id) }}" class="text-indigo-600 hover:underline font-semibold">Edit</a>
                            @if(Auth::id() !== $u->id)
                                <form method="POST" action="{{ route('business-users.destroy', $u->id) }}" onsubmit="return confirm('Are you sure you want to delete this user?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:underline font-semibold">Delete</button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4" class="px-6 py-10 text-center text-gray-400">
                            No users found. Create your first team member!
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
        @if($users->hasPages())
            <div class="p-4 border-t border-gray-100">
                {{ $users->links() }}
            </div>
        @endif
    </div>
</x-app-layout>
