@extends('layouts.admin')

@section('title', 'مراجعة المدفوعات')
@section('heading', 'مراجعة المدفوعات')
@section('subheading', 'وافق أو ارفض طلبات الدفع الواردة من التطبيق')

@section('content')
    <div class="mb-6 flex flex-wrap items-center gap-3">
        <a href="{{ route('admin.payments.settings') }}" class="rounded-xl border border-zinc-300 px-4 py-2.5 text-sm font-medium text-zinc-700 hover:bg-zinc-50">
            إعدادات الدفع
        </a>
        @foreach (['pending' => 'معلّق', 'approved' => 'موافق', 'rejected' => 'مرفوض'] as $key => $label)
            <a href="{{ route('admin.payments.reviews', ['status' => $key]) }}"
               class="rounded-xl px-4 py-2 text-sm font-semibold {{ $statusFilter === $key ? 'bg-madar-950 text-white' : 'border border-zinc-300 text-zinc-700 hover:bg-zinc-50' }}">
                {{ $label }} ({{ $counts[$key] ?? 0 }})
            </a>
        @endforeach
    </div>

    <div class="overflow-hidden rounded-2xl border border-zinc-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-zinc-200 text-sm">
                <thead class="bg-zinc-50 text-right text-xs font-semibold uppercase text-zinc-500">
                    <tr>
                        <th class="px-4 py-3">#</th>
                        <th class="px-4 py-3">المستخدم</th>
                        <th class="px-4 py-3">الوسيلة</th>
                        <th class="px-4 py-3">الغرض</th>
                        <th class="px-4 py-3">المبلغ</th>
                        <th class="px-4 py-3">المرجع</th>
                        <th class="px-4 py-3">الحالة</th>
                        <th class="px-4 py-3">التاريخ</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-zinc-100">
                    @forelse ($submissions as $s)
                        <tr class="hover:bg-zinc-50/80">
                            <td class="px-4 py-3 text-zinc-500">{{ $s->id }}</td>
                            <td class="px-4 py-3">
                                @if ($s->user)
                                    <a href="{{ route('admin.users.show', $s->user) }}" class="font-medium text-teal-700 hover:underline">{{ $s->user->username }}</a>
                                @endif
                            </td>
                            <td class="px-4 py-3">{{ $s->paymentMethod?->name_ar }}</td>
                            <td class="px-4 py-3">
                                @if ($s->purpose === 'subscription')
                                    اشتراك {{ $s->subscriptionPlan?->name }}
                                @else
                                    شحن محفظة
                                @endif
                            </td>
                            <td class="px-4 py-3 font-mono font-semibold">{{ number_format((float) $s->amount, 2) }}</td>
                            <td class="px-4 py-3 font-mono text-xs">{{ $s->payer_reference }}</td>
                            <td class="px-4 py-3">
                                @php
                                    $badge = match($s->status) {
                                        'pending' => 'bg-amber-100 text-amber-800',
                                        'approved' => 'bg-emerald-100 text-emerald-800',
                                        'rejected' => 'bg-red-100 text-red-800',
                                        default => 'bg-zinc-100 text-zinc-700',
                                    };
                                @endphp
                                <span class="rounded-full px-2 py-0.5 text-xs font-bold {{ $badge }}">{{ $s->status }}</span>
                            </td>
                            <td class="px-4 py-3 text-zinc-500 whitespace-nowrap">{{ $s->created_at?->format('Y-m-d H:i') }}</td>
                            <td class="px-4 py-3">
                                <a href="{{ route('admin.payments.reviews.show', $s) }}" class="text-teal-700 font-medium hover:underline">عرض</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="px-4 py-12 text-center text-zinc-500">لا توجد طلبات في هذا التصنيف.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if ($submissions->hasPages())
            <div class="border-t border-zinc-100 px-4 py-3">{{ $submissions->links() }}</div>
        @endif
    </div>
@endsection
