@extends('admin.layouts.app')
@section('title', 'Withdrawal Requests')
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col-auto"><a href="{{ route('admin.wallet.index') }}" class="btn btn-outline-secondary btn-sm">← Back</a></div>
            <div class="col"><h2 class="page-title">Withdrawal Requests</h2></div>
        </div>
    </div>
</div>
<div class="page-body">
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-4">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['pending','completed','cancelled'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.wallet.withdrawals') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h3 class="card-title">Withdrawal Requests ({{ $withdrawals->total() }})</h3></div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr><th>Reference</th><th>Owner</th><th>Amount</th><th>Bank Details</th><th>Status</th><th>Requested</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    @forelse($withdrawals as $txn)
                    <tr>
                        <td class="text-muted small">{{ $txn->reference }}</td>
                        <td>
                            @if($txn->wallet?->owner)
                                <div class="fw-medium">{{ $txn->wallet->owner?->name ?? $txn->wallet->owner?->business_name ?? '—' }}</div>
                                <div class="text-muted small text-capitalize">{{ class_basename($txn->wallet->owner_type) }}</div>
                            @else —
                            @endif
                        </td>
                        <td class="fw-bold">₹{{ number_format($txn->amount, 2) }}</td>
                        <td>
                            @if($txn->meta)
                                <div>{{ $txn->meta['bank_name'] ?? '—' }}</div>
                                <div class="text-muted small">{{ $txn->meta['account_number'] ?? '' }}</div>
                                <div class="text-muted small">{{ $txn->meta['account_name'] ?? '' }}</div>
                            @else —
                            @endif
                        </td>
                        <td>
                            @php $sc=['pending'=>'warning','completed'=>'success','cancelled'=>'danger']; @endphp
                            <span class="badge bg-{{ $sc[$txn->status]??'secondary' }}-lt text-capitalize">{{ $txn->status }}</span>
                        </td>
                        <td>{{ $txn->created_at->format('d M Y H:i') }}</td>
                        <td>
                            @if($txn->status === 'pending')
                            <div class="d-flex gap-1">
                                <form action="{{ route('admin.wallet.withdrawals.approve', $txn) }}" method="POST" class="d-inline">
                                    @csrf <button class="btn btn-sm btn-success" onclick="return confirm('Approve this withdrawal?')">Approve</button>
                                </form>
                                <form action="{{ route('admin.wallet.withdrawals.reject', $txn) }}" method="POST" class="d-inline">
                                    @csrf <button class="btn btn-sm btn-danger" onclick="return confirm('Reject and refund this withdrawal?')">Reject</button>
                                </form>
                            </div>
                            @else —
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No withdrawal requests</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">{{ $withdrawals->links() }}</div>
    </div>
</div>
@endsection
