@extends('layouts.self-paced-dashboard')

@section('title', 'Payment Verifications')
@section('page-title', 'Payment Verifications')
@section('page-subtitle', 'Review and approve cash payment proofs')

@section('hero-actions')
    <a href="{{ route('self-paced.dashboard.index') }}"
       class="inline-flex items-center gap-2 px-4 py-2 bg-white/10 hover:bg-white/20 text-white text-sm font-medium rounded-lg transition border border-white/30">
        <i class="fas fa-arrow-left"></i> Back
    </a>
@endsection

@section('content')
<div class="max-w-6xl mx-auto">

    {{-- Filters & Stats Bar --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 mb-5">
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-5">
            <h2 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                <i class="fas fa-filter text-indigo-600"></i> Filters
            </h2>
            <div class="flex items-center gap-2 text-xs text-gray-500">
                <span>Total: {{ $totalCount }}</span>
                <i class="fas fa-circle text-gray-300 text-[6px]"></i>
                <span class="text-amber-600">Pending: {{ $pendingCount }}</span>
                <i class="fas fa-circle text-gray-300 text-[6px]"></i>
                <span class="text-green-600">Approved: {{ $approvedCount }}</span>
                <i class="fas fa-circle text-gray-300 text-[6px]"></i>
                <span class="text-red-600">Rejected: {{ $rejectedCount }}</span>
            </div>
        </div>

        <form action="{{ route('self-paced.dashboard.payment-proofs.index') }}" method="GET" class="space-y-4">
            <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-4">

                {{-- Status Filter --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-2">Status</label>
                    <select name="status"
                            class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition">
                        <option value="">All Statuses</option>
                        <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</option>
                        <option value="approved" {{ request('status') === 'approved' ? 'selected' : '' }}>Approved</option>
                        <option value="rejected" {{ request('status') === 'rejected' ? 'selected' : '' }}>Rejected</option>
                    </select>
                </div>

                {{-- Fee Type Filter --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-2">Fee Type</label>
                    <select name="fee_type"
                            class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition">
                        <option value="">All Types</option>
                        <option value="enrollment" {{ request('fee_type') === 'enrollment' ? 'selected' : '' }}>Enrollment</option>
                        <option value="certificate" {{ request('fee_type') === 'certificate' ? 'selected' : '' }}>Certificate</option>
                        <option value="book" {{ request('fee_type') === 'book' ? 'selected' : '' }}>Course Book</option>
                        <option value="resit" {{ request('fee_type') === 'resit' ? 'selected' : '' }}>Exam Re-sit</option>
                    </select>
                </div>

                {{-- Proof Type Filter --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-2">Proof Type</label>
                    <select name="proof_type"
                            class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition">
                        <option value="">All Types</option>
                        <option value="m_pesa" {{ request('proof_type') === 'm_pesa' ? 'selected' : '' }}>M-Pesa</option>
                        <option value="bank" {{ request('proof_type') === 'bank' ? 'selected' : '' }}>Bank Transfer</option>
                        <option value="receipt" {{ request('proof_type') === 'receipt' ? 'selected' : '' }}>Receipt</option>
                    </select>
                </div>

                {{-- Search by Learner/Course --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-2">Search</label>
                    <input type="text"
                           name="search"
                           value="{{ request('search') }}"
                           placeholder="Learner name or course..."
                           class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition">
                </div>
            </div>

            <div class="flex gap-2">
                <button type="submit"
                        class="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white text-sm font-medium rounded-lg transition">
                    <i class="fas fa-search text-xs"></i> Apply Filters
                </button>
                <a href="{{ route('self-paced.dashboard.payment-proofs.index') }}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 text-sm font-medium rounded-lg transition">
                    <i class="fas fa-times text-xs"></i> Clear
                </a>
            </div>
        </form>
    </div>

    {{-- Proofs Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        @if($paymentProofs->isEmpty())
            <div class="px-6 py-12 text-center">
                <i class="fas fa-inbox text-4xl text-gray-200 mb-3 block"></i>
                <p class="text-gray-400 text-sm">No payment proofs found.</p>
                <p class="text-gray-300 text-xs mt-1">
                    {{ request('status') || request('fee_type') || request('proof_type') ? 'Try adjusting your filters.' : 'Payment proofs will appear here when learners submit cash payment receipts.' }}
                </p>
            </div>
        @else
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr>
                            <th class="bg-indigo-50 text-indigo-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">#</th>
                            <th class="bg-purple-50 text-purple-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Learner & Course</th>
                            <th class="bg-blue-50 text-blue-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Fee Type</th>
                            <th class="bg-green-50 text-green-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Proof Type</th>
                            <th class="bg-amber-50 text-amber-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Amount</th>
                            <th class="bg-rose-50 text-rose-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Status</th>
                            <th class="bg-cyan-50 text-cyan-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Submitted</th>
                            <th class="bg-gray-50 text-gray-700 font-semibold text-center px-4 py-3 text-xs uppercase tracking-wide">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50">
                        @foreach($paymentProofs as $i => $proof)
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-indigo-50/30 transition-colors">
                            <td class="px-4 py-3 text-gray-400 text-xs">{{ ($paymentProofs->currentPage() - 1) * $paymentProofs->perPage() + $i + 1 }}</td>
                            <td class="px-4 py-3">
                                <div class="space-y-1">
                                    <p class="font-medium text-gray-800 text-xs">{{ $proof->enrollment->user->name ?? 'Unknown' }}</p>
                                    <p class="text-gray-500 text-xs">{{ $proof->enrollment->course->title ?? 'Unknown' }}</p>
                                </div>
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $feeTypeIcons = [
                                        'enrollment' => ['Enrollment', 'fa-user-check', 'indigo'],
                                        'certificate' => ['Certificate', 'fa-certificate', 'amber'],
                                        'book' => ['Course Book', 'fa-book', 'green'],
                                        'resit' => ['Exam Re-sit', 'fa-redo', 'purple'],
                                    ];
                                    $feeInfo = $feeTypeIcons[$proof->fee_type] ?? ['Unknown', 'fa-question', 'gray'];
                                @endphp
                                <div class="flex items-center gap-2">
                                    <i class="fas {{ $feeInfo[1] }} text-{{ $feeInfo[2] }}-500"></i>
                                    <span class="text-gray-700">{{ $feeInfo[0] }}</span>
                                </div>
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $proofTypeLabels = [
                                        'm_pesa' => ['M-Pesa', 'fa-mobile', 'orange'],
                                        'bank' => ['Bank Transfer', 'fa-university', 'blue'],
                                        'receipt' => ['Receipt', 'fa-receipt', 'gray'],
                                    ];
                                    $proofLabel = $proofTypeLabels[$proof->proof_type] ?? ['Unknown', 'fa-question', 'gray'];
                                @endphp
                                <div class="flex items-center gap-2">
                                    <i class="fas {{ $proofLabel[1] }} text-{{ $proofLabel[2] }}-500"></i>
                                    <span class="text-gray-700">{{ $proofLabel[0] }}</span>
                                </div>
                            </td>
                            <td class="px-4 py-3 text-gray-700 font-medium text-xs">
                                {{ 'KES ' . number_format($proof->amount, 2) }}
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $statusColors = [
                                        'pending' => 'bg-amber-100 text-amber-700',
                                        'approved' => 'bg-green-100 text-green-700',
                                        'rejected' => 'bg-red-100 text-red-700',
                                    ];
                                    $color = $statusColors[$proof->status] ?? 'bg-gray-100 text-gray-600';
                                @endphp
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium {{ $color }}">
                                    {{ ucfirst($proof->status) }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-gray-500 text-xs">
                                {{ $proof->created_at?->diffForHumans() ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                @can('verify self-paced payments')
                                <a href="{{ route('self-paced.dashboard.payment-proofs.show', $proof) }}"
                                   class="inline-flex items-center gap-1 px-3 py-1.5 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 text-xs font-medium rounded-lg transition">
                                    <i class="fas fa-eye text-xs"></i> Review
                                </a>
                                @else
                                <span class="text-gray-400 text-xs">—</span>
                                @endcan
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            {{-- Pagination --}}
            <div class="px-6 py-4 border-t border-gray-100">
                {{ $paymentProofs->links('pagination::tailwind') }}
            </div>
        @endif
    </div>

</div>
@endsection
