@extends('layouts.admin', ['title' => 'طلبات الشحن اليدوي | RaseedNow'])

@php
    $fmt = fn ($minor) => number_format(($minor ?? 0) / 100, 2).' ج.م';
    $statusLabels = [
        'pending' => 'قيد المراجعة',
        'approved' => 'معتمد',
        'rejected' => 'مرفوض',
        'info_requested' => 'مطلوب معلومات',
    ];
@endphp

@section('content')
    <section class="lc-page-shell">
        <div class="section-heading">
            <div><span>Manual Review Center</span><h1>مراجعة المدفوعات اليدوية</h1></div>
            <a class="secondary-button" href="{{ route('admin.manual-payments.gateways') }}">بوابات الدفع</a>
        </div>

        @if($errors->any())<div class="mt-4 rounded-xl border border-red-200 bg-red-50 p-3 text-sm font-bold text-red-700">{{ $errors->first() }}</div>@endif

        <div class="mt-6 grid gap-4 md:grid-cols-4">
            <div class="ac-kpi" style="--accent:#f97316"><span>طلبات معلقة</span><strong>{{ $summary['pending'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#16a34a"><span>اعتمدت اليوم</span><strong>{{ $summary['approvedToday'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#ef4444"><span>مخاطر عالية</span><strong>{{ $summary['highRisk'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#2563eb"><span>مبلغ معلق</span><strong>{{ $fmt($summary['amountPending']) }}</strong></div>
        </div>

        <form method="GET" class="surface mt-6 grid gap-3 p-4 md:grid-cols-4">
            <label class="form-field"><span>بحث</span><input name="q" value="{{ $query }}" placeholder="مرجع / رقم / اسم"></label>
            <label class="form-field"><span>الحالة</span><select name="status"><option value="">كل الحالات</option>@foreach($statusLabels as $value => $label)<option value="{{ $value }}" @selected($status === $value)>{{ $label }}</option>@endforeach</select></label>
            <label class="form-field"><span>المخاطر</span><select name="risk"><option value="">كل الطلبات</option><option value="high" @selected($risk === 'high')>مخاطر عالية فقط</option></select></label>
            <div class="flex items-end"><button class="primary-button w-full" type="submit">تصفية</button></div>
        </form>

        <div class="mt-6 grid gap-4">
            @forelse($submissions as $submission)
                <article class="surface p-5">
                    <div class="flex flex-wrap items-start justify-between gap-4">
                        <div>
                            <span class="text-xs font-black uppercase tracking-[0.18em] text-[#14b8a6]">{{ $submission->gateway?->name_ar }}</span>
                            <h2 class="mt-1 text-xl font-black text-[#0f172a]">{{ $submission->payer_name }} — {{ $fmt($submission->amount_minor) }}</h2>
                            <p class="mt-1 text-sm font-bold text-[#64748b]">{{ $submission->user?->email }} · {{ $submission->created_at->format('Y-m-d H:i') }}</p>
                        </div>
                        <div class="flex flex-wrap gap-2">
                            <span class="ac-badge">{{ $statusLabels[$submission->status] ?? $submission->status }}</span>
                            <span class="ac-badge">مخاطر {{ $submission->risk_score }}/100</span>
                            @if($submission->order)<a class="secondary-button" href="{{ route('admin.orders.show', $submission->order) }}">الطلب</a>@endif
                            @if($submission->proof_path)<a class="secondary-button" href="{{ asset($submission->proof_path) }}" target="_blank">فتح الإثبات</a>@endif
                        </div>
                    </div>

                    <div class="mt-4 grid gap-3 md:grid-cols-4">
                        <div class="rounded-2xl border border-[#e5edf0] bg-[#f8fafc] p-3"><span class="block text-xs text-[#64748b]">رقم الهاتف</span><strong>{{ $submission->payer_phone }}</strong></div>
                        <div class="rounded-2xl border border-[#e5edf0] bg-[#f8fafc] p-3"><span class="block text-xs text-[#64748b]">مرجع التحويل</span><strong>{{ $submission->reference_number ?: '-' }}</strong></div>
                        <div class="rounded-2xl border border-[#e5edf0] bg-[#f8fafc] p-3"><span class="block text-xs text-[#64748b]">معرّف المحوّل</span><strong>{{ $submission->payer_identifier ?: '-' }}</strong></div>
                        <div class="rounded-2xl border border-[#e5edf0] bg-[#f8fafc] p-3"><span class="block text-xs text-[#64748b]">ينتهي</span><strong>{{ $submission->expires_at?->diffForHumans() ?: '-' }}</strong></div>
                    </div>

                    @if($submission->dynamic_fields)
                        <div class="mt-4 rounded-2xl border border-[#e5edf0] bg-white p-4">
                            <h3 class="font-black text-[#003d36]">بيانات إضافية من العميل</h3>
                            <div class="mt-3 grid gap-2 md:grid-cols-3">
                                @foreach($submission->dynamic_fields as $entry)
                                    <div class="rounded-xl bg-[#f8fafc] p-3 text-sm"><span class="block text-[#64748b]">{{ $entry['label'] ?? 'حقل' }}</span><strong>{{ $entry['value'] ?? '-' }}</strong></div>
                                @endforeach
                            </div>
                        </div>
                    @endif

                    @if($submission->risk_flags)
                        <div class="mt-4 rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm font-bold text-amber-800">
                            <strong>تنبيهات المخاطر:</strong> {{ implode('، ', $submission->risk_flags) }}
                        </div>
                    @endif

                    @if($submission->notes || $submission->review_note || $submission->rejection_reason)
                        <div class="mt-4 grid gap-3 md:grid-cols-3">
                            @if($submission->notes)<p class="rounded-2xl bg-[#f8fafc] p-3 text-sm"><b>ملاحظات العميل:</b> {{ $submission->notes }}</p>@endif
                            @if($submission->review_note)<p class="rounded-2xl bg-[#f8fafc] p-3 text-sm"><b>ملاحظة المراجعة:</b> {{ $submission->review_note }}</p>@endif
                            @if($submission->rejection_reason)<p class="rounded-2xl bg-red-50 p-3 text-sm text-red-700"><b>سبب الرفض:</b> {{ $submission->rejection_reason }}</p>@endif
                        </div>
                    @endif

                    @if($submission->status === 'pending')
                        <div class="mt-4 grid gap-3 md:grid-cols-3">
                            <form method="POST" action="{{ route('admin.manual-payments.submissions.approve', $submission) }}" class="rounded-2xl border border-emerald-200 bg-emerald-50 p-3">
                                @csrf
                                <textarea name="review_note" rows="2" placeholder="ملاحظة اختيارية" class="w-full"></textarea>
                                <button class="primary-button mt-2 w-full" type="submit">اعتماد وإضافة الرصيد</button>
                            </form>
                            <form method="POST" action="{{ route('admin.manual-payments.submissions.request-info', $submission) }}" class="rounded-2xl border border-blue-200 bg-blue-50 p-3">
                                @csrf
                                <textarea name="review_note" rows="2" required placeholder="ما المطلوب من العميل؟" class="w-full"></textarea>
                                <button class="secondary-button mt-2 w-full" type="submit">طلب معلومات</button>
                            </form>
                            <form method="POST" action="{{ route('admin.manual-payments.submissions.reject', $submission) }}" class="rounded-2xl border border-red-200 bg-red-50 p-3">
                                @csrf
                                <textarea name="rejection_reason" rows="2" placeholder="سبب الرفض" class="w-full"></textarea>
                                <button class="secondary-button mt-2 w-full" type="submit">رفض الطلب</button>
                            </form>
                        </div>
                    @endif
                </article>
            @empty
                <div class="surface p-8 text-center text-sm font-bold text-[#64748b]">لا توجد طلبات دفع مطابقة.</div>
            @endforelse
        </div>

        <div class="mt-6">{{ $submissions->links() }}</div>
    </section>
@endsection
