@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('departments.title')],
    ];
@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: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')],
            ['label' => __('departments.fields.parent')],
            ['label' => __('departments.fields.status')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '120px'],
        ]">
            @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>
                    @php
                        $user = auth()->user();
                        $canEdit = $user?->can('departments.update', $department);
                        $canDelete = $user?->can('departments.delete', $department);
                    @endphp
                    <td class="text-end">
                        <x-ui.table.actions-trigger
                            :type="__('departments.singular')"
                            :id="$department->id"
                            :view-href="$canEdit ? route('hr.departments.edit', $department) : null"
                            :delete-url="$canDelete ? 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="__('departments.empty')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>
    </x-ui.operational.index-shell>

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