@extends('layouts.app')

@section('title', 'Applicant board')
@section('heading', 'Applicant board')

@section('content')
@include('recruitment.top-submenu')

<div class="mb-4 flex flex-wrap items-center justify-between gap-3">
    <p class="text-sm text-slate-500">Drag-free board — use the stage menu on each card to move candidates.</p>
    <a href="{{ route('applicants.index') }}" class="text-sm font-medium text-primary hover:underline">Table view</a>
</div>

@php
    $stageLabels = ['applied' => 'Applied', 'screening' => 'Screening', 'interview' => 'Interview', 'offer' => 'Offer', 'rejected' => 'Rejected'];
    $sla = app(\App\Services\Recruitment\RecruitmentSlaService::class);
@endphp

<div class="flex gap-4 overflow-x-auto pb-4">
    @foreach($stageLabels as $stage => $label)
        <div class="min-w-[17rem] flex-1 rounded-xl border border-slate-200 bg-slate-50/80">
            <div class="border-b border-slate-200 px-3 py-2">
                <h3 class="text-sm font-semibold text-slate-800">{{ $label }}</h3>
                <p class="text-xs text-slate-500">{{ ($boardGroups[$stage] ?? collect())->count() }} candidates</p>
            </div>
            <div class="max-h-[70vh] space-y-2 overflow-y-auto p-2">
                @foreach($boardGroups[$stage] ?? [] as $applicant)
                    <div class="rounded-lg border border-slate-200 bg-white p-3 shadow-sm">
                        <a href="{{ route('applicants.show', $applicant) }}" class="font-medium text-slate-900 hover:text-primary">{{ $applicant->name }}</a>
                        <p class="mt-1 text-xs text-slate-500">{{ $applicant->jobPosting?->title ?? '—' }}</p>
                        @if($applicant->isReferral())
                            <p class="mt-1 text-[10px] font-medium text-amber-700">Referral · {{ $applicant->referredBy?->name }}</p>
                        @endif
                        @if($sla->isOverdue($applicant))
                            <p class="mt-1 text-[10px] font-medium text-rose-600">SLA overdue</p>
                        @endif
                        @if(auth()->user()->hasPermission('recruitment_edit'))
                            <form action="{{ route('applicants.update-stage', $applicant) }}" method="POST" class="mt-2">
                                @csrf
                                @method('PATCH')
                                <select name="stage" class="w-full rounded border border-slate-200 px-2 py-1 text-xs js-select2" onchange="this.form.submit()">
                                    @foreach($stageLabels as $value => $stageLabel)
                                        <option value="{{ $value }}" @selected($applicant->stage === $value)>{{ $stageLabel }}</option>
                                    @endforeach
                                </select>
                            </form>
                        @endif
                    </div>
                @endforeach
            </div>
        </div>
    @endforeach
</div>
@endsection
