<x-admin-layout title="Permission Matrix" subtitle="Atur hak akses modul untuk role tetap aplikasi. Owner selalu memiliki akses penuh.">
    <form method="post" action="{{ route('permissions.update') }}" class="space-y-6">
        @csrf
        <div class="overflow-hidden rounded-lg bg-white shadow-sm">
            <div class="overflow-x-auto">
                <table class="w-full min-w-[760px] text-left text-sm">
                    <thead class="border-b bg-slate-50 text-xs uppercase text-slate-500">
                        <tr>
                            <th class="p-4">Permission</th>
                            @foreach ($roles as $role => $label)
                                <th class="p-4 text-center">{{ $label }}</th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody class="divide-y">
                        @foreach ($permissions as $group => $items)
                            <tr class="bg-slate-50">
                                <td colspan="{{ count($roles) + 1 }}" class="px-4 py-3 text-xs font-semibold uppercase text-slate-500">{{ $group }}</td>
                            </tr>
                            @foreach ($items as $permission => $meta)
                                <tr>
                                    <td class="p-4 font-medium">{{ $meta['label'] }}</td>
                                    @foreach ($roles as $role => $label)
                                        <td class="p-4 text-center">
                                            <input
                                                type="checkbox"
                                                name="matrix[{{ $role }}][]"
                                                value="{{ $permission }}"
                                                class="h-5 w-5 rounded border-slate-300"
                                                @checked(in_array($permission, $matrix[$role] ?? [], true) || $role === 'owner')
                                                @disabled($role === 'owner')
                                            >
                                            @if ($role === 'owner')
                                                <input type="hidden" name="matrix[{{ $role }}][]" value="{{ $permission }}">
                                            @endif
                                        </td>
                                    @endforeach
                                </tr>
                            @endforeach
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        <div class="sticky bottom-4 flex justify-end">
            <button class="rounded-md bg-slate-900 px-5 py-3 text-sm font-medium text-white shadow-lg">Simpan Permission</button>
        </div>
    </form>
</x-admin-layout>
