<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">Create Role</h1>
                <p class="text-xs text-gray-400 mt-0.5">Register a custom role and assign permissions</p>
            </div>
            <a href="{{ route('business-roles.index') }}" class="inline-flex items-center gap-1.5 text-sm text-gray-500 hover:text-indigo-600 transition">
                <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 19l-7-7 7-7"/></svg>
                Back to Roles
            </a>
        </div>
    </x-slot>

    <div class="max-w-3xl mx-auto py-6">
        <form method="POST" action="{{ route('business-roles.store') }}" class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden p-6 space-y-6">
            @csrf

            <div class="space-y-4">
                <div>
                    <label class="block text-xs font-medium text-gray-500 mb-1.5">Role Name</label>
                    <input type="text" name="name" value="{{ old('name') }}" required placeholder="e.g. Sales Agent" class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                    @error('name') <p class="text-red-500 text-[10px] mt-1">{{ $message }}</p> @enderror
                </div>

                <div>
                    <label class="block text-xs font-medium text-gray-500 mb-3">Permissions Map</label>
                    @php
                        $groupedPermissions = $permissions->groupBy(function($perm) {
                            if (str_contains($perm->name, 'invoice')) return 'Invoices';
                            if (str_contains($perm->name, 'note') || str_contains($perm->name, 'credit-debit')) return 'Credit / Debit Notes';
                            if (str_contains($perm->name, 'customer')) return 'Customers';
                            if (str_contains($perm->name, 'item')) return 'Items';
                            return 'System & Settings';
                        });
                    @endphp
                    <div class="space-y-4">
                        @foreach($groupedPermissions as $groupName => $groupPerms)
                            <div class="bg-gray-50 p-4 rounded-xl border border-gray-100/80 space-y-2.5">
                                <h4 class="text-[10px] font-bold text-indigo-600 uppercase tracking-wider border-b border-gray-200/60 pb-1">{{ $groupName }}</h4>
                                <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-3">
                                    @foreach($groupPerms as $perm)
                                        <label class="flex items-center gap-2 text-xs text-gray-600 cursor-pointer select-none">
                                            <input type="checkbox" name="permissions[]" value="{{ $perm->name }}" class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                                            <span class="font-medium text-gray-800">{{ $perm->name }}</span>
                                        </label>
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>
            </div>

            <div class="flex justify-end gap-3 border-t border-gray-50 pt-4">
                <a href="{{ route('business-roles.index') }}" class="px-4 py-2 border border-gray-200 rounded-xl text-xs font-semibold text-gray-600 hover:bg-gray-50 transition">Cancel</a>
                <button type="submit" class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-xl text-xs font-semibold transition">Save Role</button>
            </div>
        </form>
    </div>
</x-app-layout>
