<x-app-layout>

<x-module-hero
    title="Registration Reports"
    description="Analyze registration trends and patterns"
    icon="fa-users">

    <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 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-users text-sm"></i> Registrations
        </a>
        <a href="{{ route('training.reports.completion') }}"
           class="module-hero-tab {{ request()->routeIs('training.reports.completion') ? '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-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', 'registrations') }}"
           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>

    {{-- Registration Trends --}}
    <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-chart-line text-green-600"></i>
            <h3 class="font-semibold text-gray-800 text-sm">Registration Trends</h3>
        </div>
        <div class="p-5">
            @if($registrationTrends->count() > 0)
                <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">Month</th>
                                <th class="th-col-2 px-4 py-3 text-center">Total</th>
                                <th class="th-col-3 px-4 py-3 text-center">Approved</th>
                                <th class="th-col-4 px-4 py-3 text-center">Pending</th>
                                <th class="th-col-5 px-4 py-3 text-center">Rejected</th>
                                <th class="th-col-6 px-4 py-3 text-center">Cancelled</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($registrationTrends as $trend)
                                <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">{{ $trend->month }}</td>
                                    <td class="px-4 py-3 text-center">
                                        <span class="px-2 py-1 bg-blue-100 text-blue-800 rounded-full text-xs font-medium">{{ $trend->total }}</span>
                                    </td>
                                    <td class="px-4 py-3 text-center text-green-600 font-medium">{{ $trend->approved }}</td>
                                    <td class="px-4 py-3 text-center text-yellow-600 font-medium">{{ $trend->pending }}</td>
                                    <td class="px-4 py-3 text-center text-red-600 font-medium">{{ $trend->rejected }}</td>
                                    <td class="px-4 py-3 text-center text-gray-600 font-medium">{{ $trend->cancelled }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <div class="text-center py-8 text-gray-500">
                    <i class="fas fa-chart-bar text-3xl mb-3 text-gray-300 block"></i>
                    <p class="text-sm">No registration data available for the selected period.</p>
                </div>
            @endif
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        {{-- Top Packages --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-5 py-4 border-b border-gray-200 flex items-center gap-2">
                <i class="fas fa-trophy text-yellow-500"></i>
                <h3 class="font-semibold text-gray-800 text-sm">Top Packages by Registration</h3>
            </div>
            <div class="p-5">
                @if($topPackages->count() > 0)
                    <div class="space-y-4">
                        @foreach($topPackages as $index => $package)
                            <div class="flex items-center gap-3">
                                <div class="w-7 h-7 rounded-full flex items-center justify-center text-white font-bold text-xs
                                            {{ $index === 0 ? 'bg-yellow-500' : ($index === 1 ? 'bg-gray-400' : ($index === 2 ? 'bg-amber-600' : 'bg-gray-300')) }}">
                                    {{ $index + 1 }}
                                </div>
                                <div class="flex-1">
                                    <p class="font-medium text-gray-800 text-sm">{{ $package->name }}</p>
                                    <div class="mt-1 h-1.5 bg-gray-200 rounded-full overflow-hidden">
                                        <div class="h-full bg-green-500 rounded-full"
                                             style="width: {{ $topPackages->first()->registrations_count > 0 ? ($package->registrations_count / $topPackages->first()->registrations_count) * 100 : 0 }}%"></div>
                                    </div>
                                </div>
                                <span class="font-bold text-gray-700 text-sm">{{ $package->registrations_count }}</span>
                            </div>
                        @endforeach
                    </div>
                @else
                    <p class="text-center text-sm text-gray-500 py-4">No package data available.</p>
                @endif
            </div>
        </div>

        {{-- Registration by Category --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-5 py-4 border-b border-gray-200 flex items-center gap-2">
                <i class="fas fa-folder text-purple-500"></i>
                <h3 class="font-semibold text-gray-800 text-sm">Registration by Category</h3>
            </div>
            <div class="p-5">
                @if($registrationByCategory->count() > 0)
                    <div class="space-y-3">
                        @foreach($registrationByCategory as $category)
                            <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg text-sm">
                                <span class="font-medium text-gray-700">{{ $category->category_name }}</span>
                                <span class="px-2 py-1 bg-green-100 text-green-800 rounded-full text-xs font-medium">
                                    {{ $category->registration_count }} registrations
                                </span>
                            </div>
                        @endforeach
                    </div>
                @else
                    <p class="text-center text-sm text-gray-500 py-4">No category data available.</p>
                @endif
            </div>
        </div>
    </div>
</div>

</x-app-layout>
