@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('departments.title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('departments.empty');
@endphp

<x-ui.layout.page
    :title="__('departments.title')"
    :subtitle="__('departments.subtitle')"
    page-icon="fe fe-grid"
    :breadcrumbs="$breadcrumbs"
>
    <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' => __('departments.fields.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('departments.status.active'),
                                '0' => __('departments.status.inactive'),
                            ],
                            'width' => '10rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.departments.create')"
                        :label="__('departments.actions.add')"
                        permission="departments.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('departments.fields.name'), 'sort' => 'name'],
            ['label' => __('departments.fields.parent')],
            ['label' => __('departments.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '80px'],
        ]">
            @forelse ($departments as $department)
                <x-ui.table.row>
                    <td><strong>{{ $department->name }}</strong></td>
                    <td>{{ $department->parent?->name ?? '—' }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$department->is_active ? __('departments.status.active') : __('departments.status.inactive')"
                            :variant="$department->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('departments.singular')"
                            :record="$department->name"
                            :edit-url="auth()->user()?->can('departments.update', $department) ? route('hr.departments.edit', $department) : null"
                            :delete-url="auth()->user()?->can('departments.delete', $department) ? route('hr.departments.destroy', $department) : null"
                        />
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="4" 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="$departments" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

    <x-ui.action-modal />
</x-ui.layout.page>
