@extends('admin.layouts.app')

@section('title', 'طلبات شحن المحفظة')

@section('content')
<div class="container-fluid px-0">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <h1 class="h3 mb-0" style="color: var(--text-light);">
                    <i class="fas fa-wallet" style="color: var(--primary-yellow);"></i>
                    طلبات شحن المحفظة
                </h1>
                <a href="{{ route('admin.dashboard') }}" class="btn btn-outline-primary">
                    <i class="fas fa-arrow-right"></i> العودة
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="row mb-3">
        <div class="col-12">
            <div class="btn-group">
                <a href="{{ route('admin.wallet-topup.index', ['status' => 'pending']) }}" class="btn {{ ($status ?? 'pending') == 'pending' ? 'btn-warning' : 'btn-outline-warning' }}">
                    قيد الانتظار
                </a>
                <a href="{{ route('admin.wallet-topup.index', ['status' => 'approved']) }}" class="btn {{ ($status ?? '') == 'approved' ? 'btn-success' : 'btn-outline-success' }}">
                    مفعّلة
                </a>
                <a href="{{ route('admin.wallet-topup.index', ['status' => 'rejected']) }}" class="btn {{ ($status ?? '') == 'rejected' ? 'btn-danger' : 'btn-outline-danger' }}">
                    مرفوضة
                </a>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    @if($requests->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th style="color: var(--primary-yellow);">التاريخ</th>
                                        <th style="color: var(--primary-yellow);">النوع</th>
                                        <th style="color: var(--primary-yellow);">الاسم</th>
                                        <th style="color: var(--primary-yellow);">المبلغ</th>
                                        <th style="color: var(--primary-yellow);">كود التحويل</th>
                                        <th style="color: var(--primary-yellow);">رقم المحوّل منه</th>
                                        <th style="color: var(--primary-yellow);">صورة</th>
                                        <th style="color: var(--primary-yellow);">الحالة</th>
                                        @if(($status ?? 'pending') == 'pending')
                                        <th style="color: var(--primary-yellow);">إجراء</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($requests as $r)
                                    <tr>
                                        <td>{{ $r->created_at->format('Y-m-d H:i') }}</td>
                                        <td>
                                            @php
                                                $ownerType = $r->partnerWallet?->owner_type ?? $r->owner_type ?? '';
                                            @endphp
                                            @if($ownerType == 'restaurant')
                                                مطعم
                                            @elseif($ownerType == 'gas_office')
                                                مكتب غاز
                                            @elseif($ownerType == 'driver')
                                                سائق
                                            @else
                                                —
                                            @endif
                                        </td>
                                        <td>
                                            @if($r->partnerWallet && $r->partnerWallet->owner)
                                                {{ $r->partnerWallet->owner->name ?? '—' }}
                                            @else
                                                —
                                            @endif
                                        </td>
                                        <td>{{ number_format($r->amount, 0) }} د.ع</td>
                                        <td>{{ $r->transfer_code ?? '—' }}</td>
                                        <td>{{ $r->transfer_from_phone ?? '—' }}</td>
                                        <td>
                                            @if($r->receipt_image_path)
                                                <a href="{{ asset('storage/' . $r->receipt_image_path) }}" target="_blank" class="btn btn-sm btn-outline-info">عرض</a>
                                            @else
                                                —
                                            @endif
                                        </td>
                                        <td>
                                            @if($r->status == 'pending')
                                                <span class="badge bg-warning">قيد الانتظار</span>
                                            @elseif($r->status == 'approved')
                                                <span class="badge bg-success">مفعّل</span>
                                            @else
                                                <span class="badge bg-danger">مرفوض</span>
                                            @endif
                                        </td>
                                        @if(($status ?? 'pending') == 'pending')
                                        <td>
                                            <form action="{{ route('admin.wallet-topup.approve', $r->id) }}" method="POST" class="d-inline">
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-success" onclick="return confirm('تأكيد تفعيل الشحن وإضافة المبلغ للمحفظة؟')">
                                                    تفعيل
                                                </button>
                                            </form>
                                            <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#rejectModal{{ $r->id }}">
                                                رفض
                                            </button>
                                            <div class="modal fade" id="rejectModal{{ $r->id }}" tabindex="-1">
                                                <div class="modal-dialog">
                                                    <div class="modal-content" style="background: var(--card-bg); color: var(--text-light);">
                                                        <div class="modal-header">
                                                            <h5 class="modal-title">رفض طلب الشحن</h5>
                                                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                                        </div>
                                                        <form action="{{ route('admin.wallet-topup.reject', $r->id) }}" method="POST">
                                                            @csrf
                                                            <div class="modal-body">
                                                                <label class="form-label">سبب الرفض (اختياري)</label>
                                                                <textarea name="admin_notes" class="form-control" rows="2"></textarea>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                                                                <button type="submit" class="btn btn-danger">رفض</button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                        @endif
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="d-flex justify-content-center mt-3">
                            {{ $requests->links() }}
                        </div>
                    @else
                        <p class="text-muted mb-0">لا توجد طلبات.</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
