<x-app-layout>

<x-module-hero
    title="Completion Reports"
    description="Training completion rates and performance metrics"
    icon="fa-graduation-cap">

    <x-slot name="tabs">
        <a href="{{ route('training.reports.index') }}"
           class="module-hero-tab {{ request()->routeIs('training.reports.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-tachometer-alt text-sm"></i> Overview
        </a>
        <a href="{{ route('training.reports.registrations') }}"
           class="module-hero-tab {{ request()->routeIs('training.reports.registrations') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-users text-sm"></i> Registrations
        </a>
        <a href="{{ route('training.reports.completion') }}"
           class="module-hero-tab border-b-2 border-[#1e293b]
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-graduation-cap text-sm"></i> Completion
        </a>
        <a href="{{ route('training.reports.revenue') }}"
           class="module-hero-tab {{ request()->routeIs('training.reports.revenue') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-dollar-sign text-sm"></i> Revenue
        </a>
    </x-slot>

    <x-slot name="actions">
        <a href="{{ route('training.reports.export', 'completion') }}"
           class="inline-flex items-center gap-2 px-4 py-2 bg-white/20 text-white text-sm font-medium rounded-lg hover:bg-white/30 transition">
            <i class="fas fa-download"></i> Export
        </a>
    </x-slot>

</x-module-hero>

<div class="p-6">

    {{-- Date Filter --}}
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 mb-6">
        <form method="GET" class="flex flex-wrap gap-3 items-center">
            <label class="text-sm font-medium text-gray-700">Date Range:</label>
            <input type="date" name="from" value="{{ $dateFrom }}"
                   class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
            <span class="text-gray-500 text-sm">to</span>
            <input type="date" name="to" value="{{ $dateTo }}"
                   class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
            <button type="submit" class="px-4 py-2 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700 transition">
                <i class="fas fa-filter mr-1"></i> Apply Filter
            </button>
        </form>
    </div>

    {{-- Summary Cards --}}
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
        <div class="bg-white rounded-xl border border-gray-200 p-5 text-center">
            <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-percentage text-green-600 text-lg"></i>
            </div>
            <p class="text-3xl font-bold text-green-600">{{ $completionRate }}%</p>
            <p class="text-xs text-gray-500 mt-1">Overall Completion Rate</p>
        </div>
        <div class="bg-white rounded-xl border border-gray-200 p-5 text-center">
            <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-certificate text-purple-600 text-lg"></i>
            </div>
            <p class="text-3xl font-bold text-gray-800">{{ $certificatesIssued }}</p>
            <p class="text-xs text-gray-500 mt-1">Certificates Issued</p>
        </div>
        <div class="bg-white rounded-xl border border-gray-200 p-5 text-center">
            <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-user-check text-blue-600 text-lg"></i>
            </div>
            <p class="text-3xl font-bold text-gray-800">{{ number_format($averageAttendance ?? 0, 1) }}%</p>
            <p class="text-xs text-gray-500 mt-1">Average Attendance</p>
        </div>
        <div class="bg-white rounded-xl border border-gray-200 p-5 text-center">
            <div class="w-12 h-12 bg-yellow-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-chart-line text-yellow-600 text-lg"></i>
            </div>
            <p class="text-3xl font-bold text-gray-800">{{ number_format($averageScore ?? 0, 1) }}</p>
            <p class="text-xs text-gray-500 mt-1">Average Score</p>
        </div>
    </div>

    {{-- Completion by Package --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-6 overflow-hidden">
        <div class="px-5 py-4 border-b border-gray-200 flex items-center gap-2">
            <i class="fas fa-box text-green-600"></i>
            <h3 class="font-semibold text-gray-800 text-sm">Completion by Training Package</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full table-zebra text-sm">
                <thead>
                    <tr>
                        <th class="th-col-1 px-4 py-3 text-left">Package</th>
                        <th class="th-col-2 px-4 py-3 text-center">Registrations</th>
                        <th class="th-col-3 px-4 py-3 text-center">Completed</th>
                        <th class="th-col-4 px-4 py-3 text-center">Completion Rate</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($completionByPackage as $item)
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td class="px-4 py-3 font-medium text-gray-900">{{ $item->name }}</td>
                            <td class="px-4 py-3 text-center text-gray-600">{{ $item->total_registrations }}</td>
                            <td class="px-4 py-3 text-center text-green-600 font-medium">{{ $item->completed }}</td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $rate = $item->total_registrations > 0 ? round(($item->completed / $item->total_registrations) * 100, 1) : 0;
                                @endphp
                                <div class="flex items-center justify-center gap-2">
                                    <div class="w-20 h-1.5 bg-gray-200 rounded-full overflow-hidden">
                                        <div class="h-full {{ $rate >= 70 ? 'bg-green-500' : ($rate >= 40 ? 'bg-yellow-500' : 'bg-red-500') }} rounded-full"
                                             style="width: {{ $rate }}%"></div>
                                    </div>
                                    <span class="text-xs font-medium">{{ $rate }}%</span>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="px-4 py-8 text-center text-gray-500 text-sm">
                                No completion data available for the selected period.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- Certificate & Performance --}}
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <div class="bg-white rounded-xl border border-gray-200 p-5">
            <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                <i class="fas fa-certificate text-green-600"></i> Certificate Statistics
            </h3>
            <div class="space-y-3">
                <div class="flex justify-between items-center p-3 bg-green-50 rounded-lg text-sm">
                    <div class="flex items-center gap-2">
                        <i class="fas fa-check-circle text-green-600"></i>
                        <span class="text-gray-700">Certificates Issued</span>
                    </div>
                    <span class="font-bold text-green-600 text-lg">{{ $certificatesIssued }}</span>
                </div>
                <div class="flex justify-between items-center p-3 bg-red-50 rounded-lg text-sm">
                    <div class="flex items-center gap-2">
                        <i class="fas fa-times-circle text-red-600"></i>
                        <span class="text-gray-700">Certificates Revoked</span>
                    </div>
                    <span class="font-bold text-red-600 text-lg">{{ $certificatesRevoked }}</span>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl border border-gray-200 p-5">
            <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                <i class="fas fa-chart-bar text-blue-600"></i> Performance Metrics
            </h3>
            <div class="space-y-4">
                <div>
                    <div class="flex justify-between mb-1 text-sm">
                        <span class="text-gray-600">Average Attendance</span>
                        <span class="font-medium text-gray-800">{{ number_format($averageAttendance ?? 0, 1) }}%</span>
                    </div>
                    <div class="h-2 bg-gray-200 rounded-full overflow-hidden">
                        <div class="h-full bg-blue-500 rounded-full" style="width: {{ min(100, $averageAttendance ?? 0) }}%"></div>
                    </div>
                </div>
                <div>
                    <div class="flex justify-between mb-1 text-sm">
                        <span class="text-gray-600">Average Score</span>
                        <span class="font-medium text-gray-800">{{ number_format($averageScore ?? 0, 1) }}</span>
                    </div>
                    <div class="h-2 bg-gray-200 rounded-full overflow-hidden">
                        <div class="h-full bg-yellow-500 rounded-full" style="width: {{ min(100, $averageScore ?? 0) }}%"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</x-app-layout>
