@extends('layouts.admin', ['heading' => 'Withdrawals', 'subheading' => 'Approve, reject, and payout control'])

@section('content')
    <form method="GET" class="mb-4 flex flex-wrap gap-2">
        <select name="status" class="rounded border p-2">
            <option value="">All Status</option>
            @foreach(['pending','processing','approved','rejected','failed','paid'] as $option)
                <option value="{{ $option }}" @selected($status === $option)>{{ ucfirst($option) }}</option>
            @endforeach
        </select>
        <button class="rounded bg-slate-900 px-4 py-2 text-white">Filter</button>
        <a href="{{ request()->fullUrlWithQuery(['export' => 'excel']) }}" class="rounded bg-emerald-600 px-4 py-2 text-sm text-white">Export Excel</a>
        <a href="{{ request()->fullUrlWithQuery(['export' => 'pdf']) }}" class="rounded bg-slate-700 px-4 py-2 text-sm text-white">Export PDF</a>
    </form>

    <form id="bulkWithdrawalsForm" method="POST" action="{{ route('admin.withdrawals.bulk-approve') }}">
        @csrf
    </form>
    <div class="mb-3 flex gap-2">
        <button form="bulkWithdrawalsForm" class="rounded bg-emerald-600 px-4 py-2 text-sm text-white">Bulk Approve Selected</button>
    </div>
    <div class="overflow-x-auto rounded-2xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">
                        <input type="checkbox" data-bulk-toggle="withdrawals">
                    </th>
                    <th class="p-3">Request</th>
                    <th class="p-3">Member</th>
                    <th class="p-3">Amount</th>
                    <th class="p-3">Net</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($withdrawals as $withdrawal)
                    <tr class="border-t">
                        <td class="p-3">
                            <input type="checkbox" name="withdrawal_ids[]" value="{{ $withdrawal->id }}" data-bulk-item="withdrawals" form="bulkWithdrawalsForm">
                        </td>
                        <td class="p-3">
                            <div class="font-semibold">{{ $withdrawal->request_number }}</div>
                            <div class="text-xs text-slate-500">{{ optional($withdrawal->requested_at)->format('Y-m-d H:i') }}</div>
                        </td>
                        <td class="p-3">
                            <div class="font-semibold">{{ $withdrawal->user?->member_code ?? 'N/A' }}</div>
                            <div class="text-xs text-slate-500">{{ $withdrawal->user?->name ?? '' }}</div>
                        </td>
                        <td class="p-3">{{ number_format((float) $withdrawal->amount, 2) }}</td>
                        <td class="p-3">{{ number_format((float) $withdrawal->net_amount, 2) }}</td>
                        <td class="p-3">{{ $withdrawal->status }}</td>
                        <td class="p-3">
                            <div class="flex flex-wrap gap-2">
                                <form method="POST" action="{{ route('admin.withdrawals.approve', $withdrawal) }}">
                                    @csrf
                                    <button class="rounded bg-emerald-500 px-3 py-1 text-white">Approve</button>
                                </form>
                                <form method="POST" action="{{ route('admin.withdrawals.reject', $withdrawal) }}">
                                    @csrf
                                    <input type="hidden" name="reason" value="Rejected by admin" />
                                    <button class="rounded bg-rose-500 px-3 py-1 text-white">Reject</button>
                                </form>
                                <form method="POST" action="{{ route('admin.withdrawals.retry', $withdrawal) }}">
                                    @csrf
                                    <button class="rounded bg-slate-700 px-3 py-1 text-white">Retry Payout</button>
                                </form>
                            </div>
                            <form method="POST" action="{{ route('admin.withdrawals.proof', $withdrawal) }}" enctype="multipart/form-data" class="mt-2 flex flex-wrap gap-2">
                                @csrf
                                <input name="payment_reference" placeholder="Payment Ref" class="rounded border p-1 text-xs">
                                <input type="file" name="payment_proof" class="rounded border p-1 text-xs">
                                <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Upload Proof</button>
                            </form>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="p-4 text-center text-slate-500">No withdrawals found.</td></tr>
                @endforelse
            </tbody>
            </table>
        </div>

    <div class="mt-4">{{ $withdrawals->links() }}</div>
@endsection
