@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('settings.title')],
        ['label' => __('rbac.roles.title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('rbac.roles.empty');
@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:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search',
                            'name' => 'q',
                            'label' => __('common.search'),
                            'width' => '16rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('rbac.roles.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('common.active'),
                                '0' => __('common.inactive'),
                            ],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <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'), 'sort' => 'name'],
            ['label' => __('rbac.roles.description')],
            ['label' => __('rbac.roles.status'), 'sort' => 'is_active'],
            ['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.action-modal.trigger
                            :entity="__('rbac.roles.title')"
                            :record="$role->name"
                            :view-url="route('settings.roles.show', $role)"
                            :edit-url="auth()->user()?->can('settings.roles.manage') ? route('settings.roles.edit', $role) : null"
                            :delete-url="auth()->user()?->can('settings.roles.manage') && $role->slug !== 'owner' && $role->users_count === 0 ? route('settings.roles.destroy', $role) : null"
                            :delete-message="__('rbac.roles.delete_confirm')"
                        />
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="6" class="p-0">
                        <x-ui.operational.empty-state :title="$emptyTitle" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

        <x-slot:pagination>
            <x-ui.operational.pagination :paginator="$roles" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
    <x-ui.action-modal />
</x-ui.layout.page>
