@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('settings.title')],
        ['label' => __('rbac.users.title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('operational_ui.index.empty_default');
@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:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search',
                            'name' => 'q',
                            'label' => __('common.search'),
                            'width' => '16rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('rbac.users.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.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'), 'sort' => 'name'],
            ['label' => __('rbac.users.columns.email'), 'sort' => 'email'],
            ['label' => __('rbac.users.columns.employee')],
            ['label' => __('rbac.users.columns.roles')],
            ['label' => __('rbac.users.status'), 'sort' => 'is_active'],
            ['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">
                        <x-ui.action-modal.trigger
                            :entity="__('rbac.users.title')"
                            :record="$u->name"
                            :edit-url="auth()->user()?->can('settings.users.assign_roles') ? route('settings.users.edit', $u) : null"
                        />
                    </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="$users" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
    <x-ui.action-modal />
</x-ui.layout.page>
