@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('settings.title')],
        ['label' => __('rbac.roles.title')],
    ];
@endphp

<x-ui.layout.page
    :title="__('rbac.roles.title')"
    :subtitle="__('rbac.roles.subtitle')"
    page-icon="fe fe-shield"
    :breadcrumbs="$breadcrumbs"
>
    @error('role')
        <div class="alert alert-danger">{{ $message }}</div>
    @enderror

    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('settings.roles.create')"
                        :label="__('rbac.roles.create')"
                        permission="settings.roles.manage"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('rbac.users.columns.name')],
            ['label' => __('rbac.roles.description')],
            ['label' => __('rbac.roles.status')],
            ['label' => __('rbac.roles.permissions')],
            ['label' => __('rbac.roles.assigned_users')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '260px'],
        ]">
            @forelse ($roles as $role)
                <x-ui.table.row>
                    <td><strong>{{ $role->name }}</strong></td>
                    <td>{{ $role->description ?: '—' }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$role->is_active ? __('common.active') : __('common.inactive')"
                            :variant="$role->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td>{{ $role->permissions_count }}</td>
                    <td>{{ $role->users_count }}</td>
                    <td class="text-end">
                        <x-ui.buttons.secondary size="sm" :href="route('settings.roles.show', $role)" icon="fe fe-eye">
                            {{ __('common.view') }}
                        </x-ui.buttons.secondary>
                        @can('settings.roles.manage')
                            <x-ui.buttons.secondary size="sm" :href="route('settings.roles.edit', $role->id)" icon="fe fe-edit-2">
                                {{ __('common.edit') }}
                            </x-ui.buttons.secondary>
                            @if ($role->slug !== 'owner' && $role->users_count === 0)
                                <form method="POST" action="{{ route('settings.roles.destroy', $role) }}" class="d-inline"
                                    onsubmit="return confirm(@js(__('rbac.roles.delete_confirm')))">
                                    @csrf
                                    @method('DELETE')
                                    <x-ui.buttons.danger type="submit" size="sm" icon="fe fe-trash-2">
                                        {{ __('common.delete') }}
                                    </x-ui.buttons.danger>
                                </form>
                            @endif
                        @endcan
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="6" class="p-0">
                        <x-ui.operational.empty-state :title="__('rbac.roles.empty')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
