<x-app-layout>

<x-module-hero
    title="Revenue Reports"
    description="Financial analysis and payment tracking"
    icon="fa-dollar-sign">

    <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 {{ 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 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-dollar-sign text-sm"></i> Revenue
        </a>
    </x-slot>

    <x-slot name="actions">
        <a href="{{ route('training.reports.export', 'revenue') }}"
           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>

    {{-- Revenue Summary --}}
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
        <div class="bg-white rounded-xl border-l-4 border-green-500 border border-gray-200 p-5 text-center">
            <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-wallet text-green-600"></i>
            </div>
            <p class="text-2xl font-bold text-gray-800">{{ number_format($totalRevenue, 0) }}</p>
            <p class="text-xs text-gray-500 mt-1">UGX Total Revenue</p>
        </div>
        <div class="bg-white rounded-xl border-l-4 border-yellow-500 border border-gray-200 p-5 text-center">
            <div class="w-10 h-10 bg-yellow-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-clock text-yellow-600"></i>
            </div>
            <p class="text-2xl font-bold text-yellow-600">{{ number_format($pendingPayments, 0) }}</p>
            <p class="text-xs text-gray-500 mt-1">UGX Pending</p>
        </div>
        <div class="bg-white rounded-xl border-l-4 border-red-500 border border-gray-200 p-5 text-center">
            <div class="w-10 h-10 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-exclamation-triangle text-red-600"></i>
            </div>
            <p class="text-2xl font-bold text-red-600">{{ $failedPayments }}</p>
            <p class="text-xs text-gray-500 mt-1">Failed Payments</p>
        </div>
        <div class="bg-white rounded-xl border-l-4 border-blue-500 border border-gray-200 p-5 text-center">
            <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-3">
                <i class="fas fa-credit-card text-blue-600"></i>
            </div>
            <p class="text-lg font-bold text-gray-800">MTN MoMo</p>
            <p class="text-xs text-gray-500 mt-1">Payment Method</p>
        </div>
    </div>

    {{-- Revenue 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">Revenue Trends</h3>
        </div>
        <div class="p-5">
            @if($revenueTrends->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">Revenue (UGX)</th>
                                <th class="th-col-3 px-4 py-3 text-center">Payments</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($revenueTrends 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 text-green-600 font-bold">{{ number_format($trend->revenue, 0) }}</td>
                                    <td class="px-4 py-3 text-center text-gray-600">{{ $trend->payments }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot class="bg-green-50">
                            <tr>
                                <td class="px-4 py-3 font-bold text-gray-800">Total</td>
                                <td class="px-4 py-3 text-center font-bold text-green-700">{{ number_format($revenueTrends->sum('revenue'), 0) }}</td>
                                <td class="px-4 py-3 text-center font-bold text-gray-700">{{ $revenueTrends->sum('payments') }}</td>
                            </tr>
                        </tfoot>
                    </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 revenue data available for the selected period.</p>
                </div>
            @endif
        </div>
    </div>

    {{-- Revenue by Package --}}
    <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-box text-purple-600"></i>
            <h3 class="font-semibold text-gray-800 text-sm">Revenue 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">Fee</th>
                        <th class="th-col-3 px-4 py-3 text-center">Registrations</th>
                        <th class="th-col-4 px-4 py-3 text-center">Total Revenue</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($revenueByPackage 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">{{ number_format($item->fee_amount, 0) }} UGX</td>
                            <td class="px-4 py-3 text-center text-gray-600">{{ $item->registrations }}</td>
                            <td class="px-4 py-3 text-center text-green-600 font-bold">{{ number_format($item->total_revenue ?? 0, 0) }} UGX</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="px-4 py-8 text-center text-gray-500 text-sm">
                                No revenue data available for the selected period.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

</x-app-layout>
