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

<x-ui.layout.page
    :title="__('employees.title')"
    :subtitle="__('employees.subtitle')"
    page-icon="fe fe-users"
    :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'),
                            'placeholder' => __('common.search'),
                            'width' => '16rem',
                        ],
                        [
                            'type' => 'search-select',
                            'name' => 'department_id',
                            'label' => __('employees.fields.department'),
                            'lookup' => 'departments',
                            'width' => '12rem',
                        ],
                        [
                            'type' => 'search-select',
                            'name' => 'position_id',
                            'label' => __('employees.fields.position'),
                            'lookup' => 'positions',
                            'width' => '12rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('employees.fields.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('employees.status.active'),
                                '0' => __('employees.status.inactive'),
                            ],
                            'width' => '9rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'hire_date_from',
                            'label' => __('common.from'),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'hire_date_to',
                            'label' => __('common.to'),
                            'width' => '10rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:secondary>
                    @can('exports.access')
                        <form method="POST" action="{{ route('exports.store') }}" class="d-inline">
                            @csrf
                            <input type="hidden" name="type" value="employees_report">
                            <x-ui.buttons.secondary type="submit" icon="fe fe-download">
                                {{ __('exports.actions.export_csv') }}
                            </x-ui.buttons.secondary>
                        </form>
                    @endcan
                </x-slot:secondary>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.employees.create')"
                        :label="__('employees.actions.add')"
                        permission="employees.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('employees.fields.employee_code'), 'sort' => 'employee_code'],
            ['label' => __('employees.fields.full_name'), 'sort' => 'full_name'],
            ['label' => __('employees.fields.department')],
            ['label' => __('employees.fields.position')],
            ['label' => __('employees.fields.hire_date'), 'sort' => 'hire_date'],
            ['label' => __('employees.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '200px'],
        ]">
            @forelse ($employees as $employee)
                <x-ui.table.row>
                    <td><code>{{ $employee->employee_code }}</code></td>
                    <td>
                        <a href="{{ route('hr.employees.show', $employee) }}">
                            <strong>{{ $employee->full_name }}</strong>
                        </a>
                    </td>
                    <td>{{ $employee->department?->name ?? '—' }}</td>
                    <td>{{ $employee->position?->title ?? '—' }}</td>
                    <td>{{ $employee->hire_date?->format('Y-m-d') }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$employee->is_active ? __('employees.status.active') : __('employees.status.inactive')"
                            :variant="$employee->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('employees.singular')"
                            :record="$employee->full_name"
                            :view-url="auth()->user()?->can('view', $employee) ? route('hr.employees.show', $employee) : null"
                            :edit-url="auth()->user()?->can('employees.update', $employee) ? route('hr.employees.edit', $employee) : null"
                            :deactivate-url="($employee->is_active && auth()->user()?->can('employees.deactivate', $employee)) ? route('hr.employees.deactivate', $employee) : null"
                            :deactivate-message="__('common.actions_modal.status_message')"
                            :deactivate-hint="__('employees.actions.deactivate_confirm')"
                        />
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="7" 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="$employees" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

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