@extends('layouts.app')

@section('title', 'Applicant Tracking')
@section('heading', 'Applicant Tracking')

@section('content')
@include('recruitment.top-submenu')

    @php
        $stageKpis = [
            'applied' => ['title' => 'Applied', 'accent' => 'slate', 'tone' => 'positive'],
            'screening' => ['title' => 'Screening', 'accent' => 'amber', 'tone' => 'warning'],
            'interview' => ['title' => 'Interview', 'accent' => 'sky', 'tone' => 'info'],
            'offer' => ['title' => 'Offer', 'accent' => 'emerald', 'tone' => 'positive'],
            'rejected' => ['title' => 'Rejected', 'accent' => 'rose', 'tone' => 'critical'],
        ];
        $activeStage = $stage ?? '';
    @endphp

    <div class="mb-5 grid gap-3 sm:grid-cols-2 md:grid-cols-3 xl:grid-cols-6">
        <a href="{{ route('applicants.index', request()->except(['page', 'stage'])) }}" class="block">
            <x-kpi-card
                title="Total"
                :value="$totalCount"
                accent="teal"
                subtitle="All applicants"
                @class([
                    'h-full transition hover:-translate-y-0.5 hover:shadow-md',
                    'ring-2 ring-teal-600' => $activeStage === '',
                ])
            />
        </a>
        @foreach($stageKpis as $stageKey => $meta)
            <a href="{{ route('applicants.index', array_merge(request()->except('page'), ['stage' => $stageKey])) }}" class="block">
                <x-kpi-card
                    :title="$meta['title']"
                    :value="$stageCounts[$stageKey] ?? 0"
                    :accent="$meta['accent']"
                    :tone="$meta['tone']"
                    subtitle="Pipeline stage"
                    @class([
                        'h-full transition hover:-translate-y-0.5 hover:shadow-md',
                        'ring-2 ring-teal-600' => $activeStage === $stageKey,
                    ])
                />
            </a>
        @endforeach
    </div>

    <x-card>
        <form method="GET" action="{{ route('applicants.index') }}" class="mb-4 grid gap-3 lg:grid-cols-6">
            <input type="text" name="search" value="{{ $search }}" placeholder="Search name or email..." class="rounded-lg border border-slate-300 px-3 py-2 text-sm lg:col-span-2">
            <select name="source" class="rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                <option value="">All sources</option>
                <option value="referral" @selected(($source ?? '') === 'referral')>Employee referral</option>
                <option value="direct" @selected(($source ?? '') === 'direct')>Direct / HR</option>
                <option value="careers" @selected(($source ?? '') === 'careers')>Careers page</option>
            </select>
            <select name="referrer_id" class="rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                <option value="">All referrers</option>
                @foreach($referrers as $referrer)
                    <option value="{{ $referrer->id }}" @selected((int) ($referrerId ?? 0) === (int) $referrer->id)>{{ $referrer->name }}</option>
                @endforeach
            </select>
            <select name="job_posting_id" class="rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                <option value="">All roles</option>
                @foreach($jobPostings as $job)
                    <option value="{{ $job->id }}" @selected((int) ($jobPostingId ?? 0) === (int) $job->id)>{{ $job->title }}</option>
                @endforeach
            </select>
            <button type="submit" class="rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white">Filter</button>
        </form>

        <div class="mb-4 flex flex-wrap gap-2">
            @if(app(\App\Services\Recruitment\RecruitmentNavService::class)->canRefer(auth()->user()))
                <a href="{{ route('applicants.refer') }}" class="rounded-lg border border-teal-700 px-4 py-2 text-sm font-medium text-teal-800 hover:bg-teal-50">Refer a candidate</a>
            @endif
            @if(auth()->user()->hasPermission('recruitment_create'))
                <a href="{{ route('applicants.create') }}" class="rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white"><i class="bi bi-plus-lg me-1"></i>Add applicant</a>
            @endif
            <a href="{{ route('applicants.index', ['view' => 'board']) }}" class="rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-200">Board view</a>
        </div>

        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Applicant</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Position</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Source</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Stage</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Applied</th>
                    <th class="px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Actions</th>
                </tr>
            </x-slot>
            @forelse($applicants as $applicant)
                <tr>
                    <td class="px-4 py-3 text-sm">
                        <p class="font-medium text-slate-900">{{ $applicant->name }}</p>
                        <p class="text-xs text-slate-500">{{ $applicant->email ?: '—' }}</p>
                    </td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $applicant->jobPosting?->title ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm">
                        @if($applicant->isReferral())
                            <span class="rounded-full bg-amber-100 px-2 py-0.5 text-xs font-medium text-amber-800">Referral</span>
                            <p class="mt-1 text-xs text-slate-500">{{ $applicant->referredBy?->name ?? 'Staff' }}</p>
                        @elseif($applicant->isExternalApplication())
                            <span class="rounded-full bg-sky-100 px-2 py-0.5 text-xs font-medium text-sky-800">Careers</span>
                        @else
                            <span class="rounded-full bg-slate-100 px-2 py-0.5 text-xs text-slate-600">Direct</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 text-sm">
                        <span class="rounded-full px-2 py-0.5 text-xs
                            @if($applicant->stage === 'interview') bg-blue-100 text-blue-700
                            @elseif($applicant->stage === 'offer') bg-emerald-100 text-emerald-700
                            @elseif($applicant->stage === 'screening') bg-amber-100 text-amber-700
                            @elseif($applicant->stage === 'rejected') bg-red-100 text-red-700
                            @else bg-slate-100 text-slate-600 @endif">
                            {{ ucfirst($applicant->stage) }}
                        </span>
                    </td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $applicant->applied_at?->format('d M Y') ?? '—' }}</td>
                    <td class="px-4 py-3 text-right text-sm whitespace-nowrap">
                        <a href="{{ route('applicants.show', $applicant) }}" class="btn-icon-action btn-icon-action--view" title="View" aria-label="View"><i class="bi bi-eye"></i></a>
                        @if(auth()->user()->hasPermission('recruitment_edit')) <a href="{{ route('applicants.edit', $applicant) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        @endif
                    </td>
                </tr>
            @empty
                <tr><td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">No applicants found.</td></tr>
            @endforelse
        </x-table>

        @if($applicants->hasPages())<div class="mt-4">{{ $applicants->links() }}</div>@endif
    </x-card>
@endsection
