@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('recruitment.cvs.title')],
    ];
@endphp

<x-ui.layout.page
    :title="__('recruitment.cvs.title')"
    :subtitle="__('recruitment.cvs.subtitle')"
    page-icon="fe fe-folder"
    :breadcrumbs="$breadcrumbs"
>
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('hr.recruitment.cvs.index') }}" class="row g-3 align-items-end mb-4">
                <div class="col-md-4">
                    <x-ui.form.input name="q" :label="__('recruitment.filters.search')" :value="request('q')" />
                </div>
                <div class="col-md-4">
                    <x-ui.form.select
                        name="candidate_id"
                        :label="__('recruitment.cvs.fields.candidate')"
                        :options="['' => __('recruitment.filters.all')] + $candidateOptions"
                        :selected="request('candidate_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>

            @can('create', App\Modules\HR\Recruitment\Models\RecruitmentCandidateCv::class)
                <form method="POST" action="{{ route('hr.recruitment.cvs.store') }}" enctype="multipart/form-data" class="row g-3 border-top pt-4">
                    @csrf
                    <div class="col-md-4">
                        <x-ui.form.search-select
                            name="candidate_id"
                            lookup="candidates"
                            :label="__('recruitment.cvs.fields.candidate')"
                            :options="$candidateOptions"
                            :selected="old('candidate_id')"
                            :placeholder="__('common.select')"
                            required
                        />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.input name="file" type="file" :label="__('recruitment.cvs.fields.file')" required />
                    </div>
                    <div class="col-md-2">
                        <x-ui.form.input name="notes" :label="__('recruitment.cvs.fields.notes')" :value="old('notes')" />
                    </div>
                    <div class="col-md-1 d-flex align-items-end">
                        <label class="form-check">
                            <input type="checkbox" name="is_primary" value="1" class="form-check-input" @checked(old('is_primary'))>
                            <span class="form-check-label">{{ __('recruitment.cvs.fields.is_primary') }}</span>
                        </label>
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <x-ui.buttons.primary type="submit">{{ __('recruitment.cvs.upload') }}</x-ui.buttons.primary>
                    </div>
                </form>
            @endcan
        </div>
    </div>

    <x-ui.operational.index-shell>
        <x-ui.table.table :columns="[
            ['label' => __('recruitment.cvs.fields.reference')],
            ['label' => __('recruitment.cvs.fields.candidate')],
            ['label' => __('recruitment.cvs.fields.original_name')],
            ['label' => __('recruitment.cvs.fields.is_primary')],
            ['label' => __('common.actions'), 'align' => 'end'],
        ]">
            @forelse ($cvs as $cv)
                <x-ui.table.row>
                    <td><code>{{ $cv->reference_code }}</code></td>
                    <td>{{ $cv->candidate?->full_name ?? '—' }}</td>
                    <td>{{ $cv->original_name }}</td>
                    <td>{{ $cv->is_primary ? __('common.yes') : __('common.no') }}</td>
                    <td class="text-end">
                        <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.cvs.download', $cv)" icon="fe fe-download">
                            {{ __('recruitment.cvs.actions.download') }}
                        </x-ui.buttons.secondary>
                        @can('delete', $cv)
                            <form method="POST" action="{{ route('hr.recruitment.cvs.destroy', $cv) }}" class="d-inline">
                                @csrf
                                @method('DELETE')
                                <x-ui.buttons.danger outline type="submit" size="sm">{{ __('common.delete') }}</x-ui.buttons.danger>
                            </form>
                        @endcan
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="5" class="p-0">
                        <x-ui.operational.empty-state :title="__('recruitment.cvs.empty')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

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