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

<x-ui.layout.page
    :title="__('contracts.title')"
    :subtitle="__('contracts.subtitle')"
    page-icon="fe fe-file"
    :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' => '14rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'contract_type',
                            'label' => __('contracts.fields.contract_type'),
                            'options' => ['' => __('common.all')] + collect(['permanent', 'temporary', 'part_time', 'internship', 'freelancer', 'consultant', 'seasonal'])
                                ->mapWithKeys(fn ($type) => [$type => __('contracts.types.'.$type)])->all(),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'status',
                            'label' => __('contracts.fields.status'),
                            'options' => ['' => __('common.all')] + collect(['active', 'inactive', 'draft', 'expired', 'terminated', 'renewed'])
                                ->mapWithKeys(fn ($status) => [$status => __('contracts.status.'.$status)])->all(),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'start_date_from',
                            'label' => __('common.from'),
                            'width' => '9rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'start_date_to',
                            'label' => __('common.to'),
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.contracts.create')"
                        :label="__('contracts.actions.add')"
                        permission="contracts.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('contracts.fields.employee')],
            ['label' => __('contracts.fields.contract_type')],
            ['label' => __('contracts.fields.start_date'), 'sort' => 'start_date'],
            ['label' => __('contracts.fields.end_date'), 'sort' => 'end_date'],
            ['label' => __('contracts.fields.status'), 'sort' => 'status'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '160px'],
        ]">
            @forelse ($contracts as $contract)
                <x-ui.table.row>
                    <td><strong>{{ $contract->employee?->full_name ?? '—' }}</strong></td>
                    <td>{{ __('contracts.types.'.$contract->contract_type) }}</td>
                    <td>{{ $contract->start_date?->format('Y-m-d') }}</td>
                    <td>{{ $contract->end_date?->format('Y-m-d') ?? '—' }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="__('contracts.status.'.$contract->status)"
                            :variant="$contract->status === 'active' ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('contracts.singular')"
                            :record="$contract->employee?->full_name ?? ''"
                            :edit-url="auth()->user()?->can('update', $contract) ? route('hr.contracts.edit', $contract) : null"
                            :deactivate-url="$contract->is_active && auth()->user()?->can('deactivate', $contract) ? route('hr.contracts.deactivate', $contract) : 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="$contracts" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
    <x-ui.action-modal />
</x-ui.layout.page>
