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

<x-ui.layout.page
    :title="__('positions.title')"
    :subtitle="__('positions.subtitle')"
    page-icon="fe fe-layers"
    :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' => 'search-select',
                            'name' => 'department_id',
                            'label' => __('positions.fields.department'),
                            'lookup' => 'departments',
                            'width' => '12rem',
                        ],
                        [
                            'type' => 'search-select',
                            'name' => 'job_grade_id',
                            'label' => __('positions.fields.job_grade'),
                            'lookup' => 'job_grades',
                            'width' => '12rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('positions.fields.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('positions.status.active'),
                                '0' => __('positions.status.inactive'),
                            ],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.positions.create')"
                        :label="__('positions.actions.add')"
                        permission="positions.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('positions.fields.title'), 'sort' => 'title'],
            ['label' => __('positions.fields.department')],
            ['label' => __('positions.fields.job_grade')],
            ['label' => __('positions.fields.salary')],
            ['label' => __('positions.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '200px'],
        ]">
            @forelse ($positions as $position)
                <x-ui.table.row>
                    <td><strong>{{ $position->title }}</strong></td>
                    <td>{{ $position->department?->name ?? '—' }}</td>
                    <td>{{ $position->jobGrade?->name ?? '—' }}</td>
                    <td>{{ number_format((float) $position->salary, 2) }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$position->is_active ? __('positions.status.active') : __('positions.status.inactive')"
                            :variant="$position->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('positions.singular')"
                            :record="$position->title"
                            :edit-url="auth()->user()?->can('positions.update', $position) ? route('hr.positions.edit', $position) : null"
                            :deactivate-url="($position->is_active && auth()->user()?->can('positions.deactivate', $position)) ? route('hr.positions.deactivate', $position) : null"
                            :deactivate-message="__('common.actions_modal.status_message')"
                            :deactivate-hint="__('positions.actions.deactivate_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="$positions" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

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