<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">Roles & Permissions</h1>
                <p class="text-xs text-gray-400 mt-0.5">Manage permissions and roles for your team</p>
            </div>
            <a href="{{ route('business-roles.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">
                Create Role
            </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

    <div x-data="{ openModal: false, selectedRole: '', selectedPermissions: [] }">
        <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">Role Name</th>
                        <th class="px-6 py-3.5">Permissions Count</th>
                        <th class="px-6 py-3.5 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse($roles as $role)
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-6 py-3.5 font-medium text-gray-800">
                                {{ $role->name }}
                                @if(is_null($role->business_id))
                                    <span class="ml-1.5 px-1.5 py-0.5 text-[9px] bg-gray-100 text-gray-500 rounded font-semibold">System</span>
                                @else
                                    <span class="ml-1.5 px-1.5 py-0.5 text-[9px] bg-indigo-50 text-indigo-600 rounded font-semibold">Custom</span>
                                @endif
                            </td>
                            <td class="px-6 py-3.5 text-gray-500">
                                <span class="px-2.5 py-1 rounded-full bg-gray-100 text-gray-700 text-[10px] font-semibold">
                                    {{ $role->permissions->count() }} Permissions
                                </span>
                            </td>
                            <td class="px-6 py-3.5 text-right">
                                <div class="flex items-center justify-end gap-3">
                                    <button @click="selectedRole = '{{ addslashes($role->name) }}'; selectedPermissions = {{ json_encode($role->permissions->pluck('name')) }}; openModal = true" class="text-indigo-600 hover:underline font-semibold">
                                        View Permissions
                                    </button>
                                    @if(in_array(strtolower($role->name), ['admin', 'super admin']))
                                        <span class="text-gray-400 text-[10px] font-semibold italic">System Protected</span>
                                    @else
                                        <a href="{{ route('business-roles.edit', $role->id) }}" class="text-indigo-600 hover:underline font-semibold">Edit</a>
                                        <form method="POST" action="{{ route('business-roles.destroy', $role->id) }}" onsubmit="return confirm('Are you sure you want to delete this role?');" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="text-red-600 hover:underline font-semibold">Delete</button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="px-6 py-10 text-center text-gray-400">
                                No roles found. Create one now to assign specific permission sets!
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
            @if($roles->hasPages())
                <div class="p-4 border-t border-gray-100">
                    {{ $roles->links() }}
                </div>
            @endif
        </div>

        <!-- Modal -->
        <div x-show="openModal" class="fixed inset-0 z-50 overflow-y-auto" x-cloak>
            <div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0">
                <!-- Backdrop -->
                <div x-show="openModal" 
                     x-transition:enter="ease-out duration-300"
                     x-transition:enter-start="opacity-0"
                     x-transition:enter-end="opacity-100"
                     x-transition:leave="ease-in duration-200"
                     x-transition:leave-start="opacity-100"
                     x-transition:leave-end="opacity-0"
                     class="fixed inset-0 transition-opacity bg-gray-500 bg-opacity-75" @click="openModal = false"></div>

                <!-- Modal panel -->
                <div x-show="openModal" 
                     x-transition:enter="ease-out duration-300"
                     x-transition:enter-start="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                     x-transition:enter-end="opacity-100 translate-y-0 sm:scale-100"
                     x-transition:leave="ease-in duration-200"
                     x-transition:leave-start="opacity-100 translate-y-0 sm:scale-100"
                     x-transition:leave-end="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                     class="inline-block align-bottom bg-white rounded-2xl text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full p-6">
                    <div class="flex items-center justify-between border-b border-gray-100 pb-3 mb-4">
                        <h3 class="text-sm font-bold text-gray-800">
                            Permissions for <span class="text-indigo-600" x-text="selectedRole"></span>
                        </h3>
                        <button @click="openModal = false" class="text-gray-400 hover:text-gray-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>
                        </button>
                    </div>
                    <div class="flex flex-wrap gap-1.5 max-h-60 overflow-y-auto p-1">
                        <template x-for="perm in selectedPermissions" :key="perm">
                            <span class="px-2.5 py-1 rounded-lg bg-indigo-50 text-indigo-600 text-[10px] font-semibold" x-text="perm"></span>
                        </template>
                        <template x-if="selectedPermissions.length === 0">
                            <span class="text-gray-400 text-xs">No permissions assigned.</span>
                        </template>
                    </div>
                    <div class="flex justify-end gap-3 border-t border-gray-100 pt-4 mt-6">
                        <button @click="openModal = false" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-800 rounded-xl text-xs font-semibold transition">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
