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

<x-ui.layout.page
    :title="__('recruitment.interviews.title')"
    :subtitle="__('recruitment.interviews.subtitle')"
    page-icon="fe fe-users"
    :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.interviews.create')"
                        :label="__('recruitment.interviews.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.interviews.fields.status')" :options="$statusOptions" :selected="request('status')" />
                    </div>
                    <div class="col-md-3">
                        <x-ui.form.select name="application_id" :label="__('recruitment.interviews.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.interviews.bulk-destroy') }}">
            @csrf
            @can('recruitment.manage')
                <div class="mb-2">
                    <x-ui.buttons.danger outline size="sm" type="submit">{{ __('recruitment.interviews.actions.bulk_delete') }}</x-ui.buttons.danger>
                </div>
            @endcan

            <x-ui.table.table :columns="[
                ['label' => '', 'width' => '36px'],
                ['label' => __('recruitment.interviews.fields.reference')],
                ['label' => __('recruitment.interviews.fields.candidate')],
                ['label' => __('recruitment.interviews.fields.scheduled_at')],
                ['label' => __('recruitment.interviews.fields.mode')],
                ['label' => __('recruitment.interviews.fields.status')],
                ['label' => __('common.actions'), 'align' => 'end', 'width' => '280px'],
            ]">
                @forelse ($interviews as $row)
                    <x-ui.table.row>
                        <td>
                            @can('delete', $row)
                                <input type="checkbox" name="ids[]" value="{{ $row->id }}" class="form-check-input">
                            @endcan
                        </td>
                        <td><code>{{ $row->reference_code }}</code></td>
                        <td><strong>{{ $row->application?->candidate?->full_name ?? '—' }}</strong></td>
                        <td>{{ $row->scheduled_at?->format('Y-m-d H:i') ?? '—' }}</td>
                        <td>{{ __('recruitment.interviews.mode.'.$row->mode) }}</td>
                        <td>
                            <x-ui.operational.status-badge :label="__('recruitment.interviews.status.'.$row->status)" variant="neutral" />
                        </td>
                        <td class="text-end text-nowrap">
                            @can('update', $row)
                                @if ($row->status === 'scheduled')
                                    <x-ui.buttons.secondary size="sm" :href="route('hr.recruitment.interviews.edit', $row)">{{ __('common.edit') }}</x-ui.buttons.secondary>
                                    <form method="POST" action="{{ route('hr.recruitment.interviews.complete', $row) }}" class="d-inline">@csrf
                                        <x-ui.buttons.primary size="sm" type="submit">{{ __('recruitment.interviews.actions.complete') }}</x-ui.buttons.primary>
                                    </form>
                                    <form method="POST" action="{{ route('hr.recruitment.interviews.cancel', $row) }}" class="d-inline">@csrf
                                        <x-ui.buttons.secondary size="sm" type="submit">{{ __('recruitment.interviews.actions.cancel') }}</x-ui.buttons.secondary>
                                    </form>
                                    <form method="POST" action="{{ route('hr.recruitment.interviews.no-show', $row) }}" class="d-inline">@csrf
                                        <x-ui.buttons.secondary size="sm" type="submit">{{ __('recruitment.interviews.actions.no_show') }}</x-ui.buttons.secondary>
                                    </form>
                                @endif
                            @endcan
                            @can('delete', $row)
                                <form method="POST" action="{{ route('hr.recruitment.interviews.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="7" class="p-0"><x-ui.operational.empty-state :title="__('recruitment.interviews.empty')" /></td>
                    </x-ui.table.row>
                @endforelse
            </x-ui.table.table>
        </form>

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