@extends('layouts.admin', ['heading' => 'KYC Review', 'subheading' => 'Verify user documents'])

@section('content')
    <form method="GET" class="mb-4 flex flex-wrap gap-2">
        <select name="status" class="rounded border p-2">
            @foreach(['pending','approved','rejected'] 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>
    </form>

    <form id="bulkKycForm" method="POST" action="{{ route('admin.kyc.bulk-approve') }}">
        @csrf
    </form>
    <button form="bulkKycForm" class="mb-4 rounded bg-emerald-600 px-4 py-2 text-sm text-white">Bulk Approve Selected</button>
    <div class="mt-3 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="kyc">
                    </th>
                    <th class="p-3">Member</th>
                    <th class="p-3">PAN</th>
                    <th class="p-3">Bank</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($kycRows as $row)
                    <tr class="border-t">
                        <td class="p-3">
                            <input type="checkbox" name="kyc_ids[]" value="{{ $row->id }}" data-bulk-item="kyc" form="bulkKycForm">
                        </td>
                        <td class="p-3">
                            <div class="font-semibold">{{ $row->user?->member_code ?? 'N/A' }}</div>
                            <div class="text-xs text-slate-500">{{ $row->user?->name ?? '' }}</div>
                        </td>
                        <td class="p-3">{{ $row->pan_number }}</td>
                        <td class="p-3">{{ $row->bank_account_number }}</td>
                        <td class="p-3">{{ $row->status }}</td>
                        <td class="p-3">
                            <div class="flex gap-2">
                                <form method="POST" action="{{ route('admin.kyc.approve', $row) }}">
                                    @csrf
                                    <button class="rounded bg-emerald-500 px-3 py-1 text-white">Approve</button>
                                </form>
                                <form method="POST" action="{{ route('admin.kyc.reject', $row) }}">
                                    @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.kyc.reupload', $row) }}">
                                    @csrf
                                    <button class="rounded bg-amber-500 px-3 py-1 text-white">Force Re-upload</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="p-4 text-center text-slate-500">No KYC records found.</td></tr>
                @endforelse
            </tbody>
            </table>
        </div>

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