@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('recruitment.job_requests.title')],
    ];
    $deptOptions = ['' => __('recruitment.filters.all')] + $departments->mapWithKeys(fn ($d) => [$d->id => $d->name])->all();
@endphp

<x-ui.layout.page
    :title="__('recruitment.job_requests.title')"
    :subtitle="__('recruitment.job_requests.subtitle')"
    page-icon="fe fe-file-text"
    :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.job-requests.create')"
                        :label="__('recruitment.job_requests.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.job_requests.fields.status')" :options="$statusOptions" :selected="request('status')" />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.select name="department_id" :label="__('recruitment.job_requests.fields.department')" :options="$deptOptions" :selected="request('department_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.job_requests.fields.reference')],
            ['label' => __('recruitment.job_requests.fields.title')],
            ['label' => __('recruitment.job_requests.fields.department')],
            ['label' => __('recruitment.job_requests.fields.openings')],
            ['label' => __('recruitment.job_requests.fields.status')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '260px'],
        ]">
            @forelse ($jobRequests as $row)
                <x-ui.table.row>
                    <td><code>{{ $row->reference_code }}</code></td>
                    <td><strong>{{ $row->title }}</strong></td>
                    <td>{{ $row->department?->name ?? '—' }}</td>
                    <td>{{ $row->openings }}</td>
                    <td>
                        <x-ui.operational.status-badge :label="__('recruitment.job_requests.status.'.$row->status)" variant="neutral" />
                    </td>
                    <td class="text-end">
                        @can('update', $row)
                            <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.job-requests.edit', $row)">{{ __('common.edit') }}</x-ui.buttons.secondary>
                        @endcan
                        @can('submit', $row)
                            <form method="POST" action="{{ route('hr.recruitment.job-requests.submit', $row) }}" class="d-inline">@csrf
                                <x-ui.buttons.primary size="sm" type="submit">{{ __('recruitment.job_requests.actions.submit') }}</x-ui.buttons.primary>
                            </form>
                        @endcan
                        @can('approve', $row)
                            <form method="POST" action="{{ route('hr.recruitment.job-requests.approve', $row) }}" class="d-inline">@csrf
                                <x-ui.buttons.primary size="sm" type="submit">{{ __('recruitment.job_requests.actions.approve') }}</x-ui.buttons.primary>
                            </form>
                            <form method="POST" action="{{ route('hr.recruitment.job-requests.reject', $row) }}" class="d-inline">@csrf
                                <x-ui.buttons.danger outline size="sm" type="submit">{{ __('recruitment.job_requests.actions.reject') }}</x-ui.buttons.danger>
                            </form>
                        @endcan
                        @can('delete', $row)
                            <form method="POST" action="{{ route('hr.recruitment.job-requests.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.job_requests.empty')" /></td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

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