@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('recruitment.offer_letters.title')],
    ];
    $appFilter = ['' => __('recruitment.filters.all')] + ($applicationOptions ?? []);
@endphp

<x-ui.layout.page
    :title="__('recruitment.offer_letters.title')"
    :subtitle="__('recruitment.offer_letters.subtitle')"
    page-icon="fe fe-mail"
    :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.recruitment.offer-letters.create')"
                        :label="__('recruitment.offer_letters.actions.add')"
                        permission="recruitment.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <form method="GET" class="row g-3 align-items-end">
                    <div class="col-md-3">
                        <x-ui.form.input name="q" :label="__('recruitment.filters.search')" :value="request('q')" />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.select name="status" :label="__('recruitment.offer_letters.fields.status')" :options="$statusOptions" :selected="request('status')" />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.select name="application_id" :label="__('recruitment.offer_letters.fields.application')" :options="$appFilter" :selected="request('application_id')" />
                    </div>
                    <div class="col-md-2">
                        <x-ui.buttons.primary type="submit" icon="fe fe-filter">{{ __('recruitment.filters.apply') }}</x-ui.buttons.primary>
                    </div>
                </form>
            </div>
        </div>

        <form method="POST" action="{{ route('hr.recruitment.offer-letters.bulk-destroy') }}">
            @csrf
            @can('recruitment.manage')
                <div class="mb-2">
                    <x-ui.buttons.danger outline size="sm" type="submit">{{ __('recruitment.offer_letters.actions.bulk_delete') }}</x-ui.buttons.danger>
                </div>
            @endcan

            <x-ui.table.table :columns="[
                ['label' => '', 'width' => '36px'],
                ['label' => __('recruitment.offer_letters.fields.reference')],
                ['label' => __('recruitment.offer_letters.fields.candidate')],
                ['label' => __('recruitment.offer_letters.fields.vacancy')],
                ['label' => __('recruitment.offer_letters.fields.salary')],
                ['label' => __('recruitment.offer_letters.fields.status')],
                ['label' => __('common.actions'), 'align' => 'end', 'width' => '220px'],
            ]">
                @forelse ($offers as $row)
                    <x-ui.table.row>
                        <td>
                            @can('delete', $row)
                                @if ($row->status === 'draft')
                                    <input type="checkbox" name="ids[]" value="{{ $row->id }}" class="form-check-input">
                                @endif
                            @endcan
                        </td>
                        <td><a href="{{ route('hr.recruitment.offer-letters.show', $row) }}"><code>{{ $row->reference_code }}</code></a></td>
                        <td><strong>{{ $row->candidate?->full_name ?? '—' }}</strong></td>
                        <td>{{ $row->vacancy?->title ?? '—' }}</td>
                        <td>{{ number_format((float) $row->salary, 2) }} {{ $row->currency }}</td>
                        <td>
                            <x-ui.operational.status-badge :label="__('recruitment.offer_letters.status.'.$row->status)" variant="neutral" />
                        </td>
                        <td class="text-end text-nowrap">
                            <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.offer-letters.show', $row)">{{ __('common.view') }}</x-ui.buttons.secondary>
                            @can('update', $row)
                                @if ($row->status === 'draft')
                                    <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.offer-letters.edit', $row)">{{ __('common.edit') }}</x-ui.buttons.secondary>
                                @endif
                            @endcan
                        </td>
                    </x-ui.table.row>
                @empty
                    <x-ui.table.row>
                        <td colspan="7" class="p-0"><x-ui.operational.empty-state :title="__('recruitment.offer_letters.empty')" /></td>
                    </x-ui.table.row>
                @endforelse
            </x-ui.table.table>
        </form>

        <x-slot:pagination>
            @if ($offers->hasPages())
                <div class="d-flex justify-content-center">{{ $offers->links() }}</div>
            @endif
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
