@extends('layouts.admin')

@section('title', 'Dashboard')

@section('content')
{{-- Stats Cards --}}
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <div>
            <p class="text-sm text-gray-500">Total Lowongan</p>
            <p class="text-3xl font-bold text-gray-800">{{ $totalJobs }}</p>
        </div>
        <p class="text-xs text-green-600 mt-2">{{ $activeJobs }} aktif</p>
    </div>

    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <div>
            <p class="text-sm text-gray-500">Total Pelamar</p>
            <p class="text-3xl font-bold text-gray-800">{{ $totalApplicants }}</p>
        </div>
    </div>

    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <div>
            <p class="text-sm text-gray-500">Total Lamaran</p>
            <p class="text-3xl font-bold text-gray-800">{{ $totalApplications }}</p>
        </div>
    </div>

    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <div>
            <p class="text-sm text-gray-500">Notifikasi Terkirim</p>
            <p class="text-3xl font-bold text-gray-800">{{ $notificationStats['sent'] }}</p>
        </div>
        @if($notificationStats['failed'] > 0)
        <p class="text-xs text-red-600 mt-2">{{ $notificationStats['failed'] }} gagal</p>
        @endif
    </div>
</div>

{{-- Recent Jobs --}}
<div class="bg-white rounded-xl border border-gray-100 p-6 mb-8">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 mb-4">
        <div>
            <h3 class="text-lg font-semibold text-gray-800">Lowongan Terbaru</h3>
        </div>
        <a href="{{ route('admin.jobs.index') }}" class="text-sm font-medium text-indigo-600 hover:text-indigo-800">Lihat semua lowongan</a>
    </div>

    @if($recentJobs->count() > 0)
    <div class="divide-y divide-gray-100">
        @foreach($recentJobs as $job)
        <div class="py-3 flex flex-col md:flex-row md:items-center md:justify-between gap-3">
            <div>
                <p class="text-sm font-medium text-gray-800">{{ $job->title }}</p>
                <p class="text-xs text-gray-500">{{ $job->company }} • {{ $job->location }}</p>
            </div>

            <div class="flex items-center flex-wrap gap-3">
                <span class="text-xs px-2 py-1 rounded-full {{ $job->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500' }}">
                    {{ $job->is_active ? 'Aktif' : 'Nonaktif' }}
                </span>

                <a href="{{ route('admin.jobs.edit', $job) }}" class="text-sm font-medium text-indigo-600 hover:text-indigo-800">Edit</a>
            </div>
        </div>
        @endforeach
    </div>
    @else
    <p class="text-sm text-gray-400 text-center py-4">Belum ada lowongan.</p>
    @endif
</div>

{{-- Status Overview --}}
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <h3 class="text-lg font-semibold text-gray-800 mb-4">Status Lamaran</h3>
        <div class="space-y-3">
            @php
                $statusColors = [
                    'applied' => 'blue',
                    'accepted' => 'green',
                    'rejected' => 'red',
                ];
            @endphp
            @forelse($statusCounts as $status => $count)
            @php
                if (isset($statusColors[$status])) {
                    $color = $statusColors[$status];
                } elseif (str_contains($status, '_passed')) {
                    $color = 'yellow';
                } elseif (str_contains($status, '_form_filled')) {
                    $color = 'indigo';
                } else {
                    $color = 'gray';
                }
                $label = match(true) {
                    $status === 'applied' => 'Lamaran Masuk',
                    $status === 'accepted' => 'Diterima',
                    $status === 'rejected' => 'Ditolak',
                    str_contains($status, '_passed') => 'Lolos ' . ucfirst(str_replace(['stage', '_passed'], ['Tahap '], $status)),
                    str_contains($status, '_form_filled') => 'Form Terisi ' . ucfirst(str_replace(['stage', '_form_filled'], ['Tahap '], $status)),
                    default => ucfirst(str_replace('_', ' ', $status)),
                };
            @endphp
            <div class="flex items-center justify-between">
                <span class="text-sm text-gray-600">{{ $label }}</span>
                <div class="flex items-center">
                    <div class="w-32 bg-gray-100 rounded-full h-2 mr-3">
                        <div class="bg-{{ $color }}-500 h-2 rounded-full" style="width: {{ $totalApplications > 0 ? ($count / $totalApplications * 100) : 0 }}%"></div>
                    </div>
                    <span class="text-sm font-medium text-gray-700 w-8 text-right">{{ $count }}</span>
                </div>
            </div>
            @empty
            <p class="text-gray-400 text-sm text-center py-2">Belum ada data</p>
            @endforelse
        </div>
    </div>

    <div class="bg-white rounded-xl border border-gray-100 p-6">
        <h3 class="text-lg font-semibold text-gray-800 mb-4">Lamaran Terbaru</h3>
        <div class="space-y-3">
            @forelse($recentApplications as $app)
            <a href="{{ route('admin.applications.show', $app) }}" class="flex items-center justify-between p-3 hover:bg-gray-50 rounded-lg">
                <div>
                    <p class="text-sm font-medium text-gray-800">{{ $app->applicant->name ?? 'N/A' }}</p>
                    <p class="text-xs text-gray-500">{{ $app->jobListing->title ?? 'N/A' }}</p>
                </div>
                <span class="inline-block px-2 py-1 text-xs font-medium rounded-full {{ $app->status_badge_class }}">
                    {{ $app->status_label }}
                </span>
            </a>
            @empty
            <p class="text-gray-400 text-sm text-center py-4">Belum ada lamaran</p>
            @endforelse
        </div>
    </div>
</div>
@endsection
