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

<x-ui.layout.page
    :title="__('recruitment.applications.title')"
    :subtitle="__('recruitment.applications.subtitle')"
    page-icon="fe fe-inbox"
    :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.applications.create')"
                        :label="__('recruitment.applications.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.applications.fields.status')" :options="$statusOptions" :selected="request('status')" />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.select name="vacancy_id" :label="__('recruitment.applications.fields.vacancy')" :options="$vacancyFilter" :selected="request('vacancy_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>

        <x-ui.table.table :columns="[
            ['label' => __('recruitment.applications.fields.reference')],
            ['label' => __('recruitment.applications.fields.candidate')],
            ['label' => __('recruitment.applications.fields.vacancy')],
            ['label' => __('recruitment.applications.fields.status')],
            ['label' => __('recruitment.applications.fields.applied_at')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '220px'],
        ]">
            @forelse ($applications as $row)
                <x-ui.table.row>
                    <td><code>{{ $row->reference_code }}</code></td>
                    <td><strong>{{ $row->candidate?->full_name ?? '—' }}</strong></td>
                    <td>{{ $row->vacancy?->title ?? '—' }}</td>
                    <td>
                        <x-ui.operational.status-badge :label="__('recruitment.applications.status.'.$row->status)" variant="neutral" />
                    </td>
                    <td>{{ $row->applied_at?->format('Y-m-d') ?? '—' }}</td>
                    <td class="text-end">
                        @can('update', $row)
                            <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.applications.edit', $row)">{{ __('common.edit') }}</x-ui.buttons.secondary>
                            <form method="POST" action="{{ route('hr.recruitment.applications.status', $row) }}" class="d-inline-flex gap-1 align-items-center">
                                @csrf
                                <select name="status" class="form-select form-select-sm" style="width: auto;">
                                    @foreach ($statusOptions as $value => $label)
                                        @continue($value === '')
                                        <option value="{{ $value }}" @selected($row->status === $value)>{{ $label }}</option>
                                    @endforeach
                                </select>
                                <x-ui.buttons.primary size="sm" type="submit">{{ __('recruitment.applications.actions.update_status') }}</x-ui.buttons.primary>
                            </form>
                        @endcan
                        @can('delete', $row)
                            <form method="POST" action="{{ route('hr.recruitment.applications.destroy', $row) }}" class="d-inline">
                                @csrf @method('DELETE')
                                <x-ui.buttons.danger outline size="sm" type="submit">{{ __('common.delete') }}</x-ui.buttons.danger>
                            </form>
                        @endcan
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="6" class="p-0"><x-ui.operational.empty-state :title="__('recruitment.applications.empty')" /></td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

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