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

<x-ui.layout.page
    :title="__('rbac.users.title')"
    :subtitle="__('rbac.users.subtitle')"
    page-icon="fe fe-users"
    :breadcrumbs="$breadcrumbs"
>
    @if (session('invite_link'))
        <div class="alert alert-info border-0 shadow-sm mb-3" role="alert">
            <div class="fw-semibold mb-1">{{ __('rbac.users.invite_link_hint') }}</div>
            <code class="small user-select-all d-block text-break">{{ session('invite_link') }}</code>
        </div>
    @endif

    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('settings.users.create')"
                        :label="__('rbac.users.create')"
                        permission="settings.users.invite"
                    />
                    @can('settings.users.invite')
                        <x-ui.buttons.secondary :href="route('settings.users.invite')" icon="fe fe-send">
                            {{ __('rbac.users.invite') }}
                        </x-ui.buttons.secondary>
                    @endcan
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('rbac.users.columns.name')],
            ['label' => __('rbac.users.columns.email')],
            ['label' => __('rbac.users.columns.employee')],
            ['label' => __('rbac.users.columns.roles')],
            ['label' => __('rbac.users.status')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '160px'],
        ]">
            @forelse ($users as $u)
                <x-ui.table.row>
                    <td><strong>{{ $u->name }}</strong></td>
                    <td>{{ $u->email }}</td>
                    <td>{{ $u->employee?->full_name ?? '—' }}</td>
                    <td>
                        @foreach ($u->roles as $r)
                            <x-ui.operational.status-badge :label="$r->name" variant="neutral" class="me-1" />
                        @endforeach
                    </td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$u->is_active ? __('common.active') : __('common.inactive')"
                            :variant="$u->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        @can('settings.users.assign_roles')
                            <x-ui.buttons.secondary size="sm" :href="route('settings.users.edit', $u)" icon="fe fe-edit-2">
                                {{ __('common.edit') }}
                            </x-ui.buttons.secondary>
                        @endcan
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="6" class="p-0">
                        <x-ui.operational.empty-state :title="__('operational_ui.index.empty_default')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
