@extends('layouts.admin')

@section('title', 'Face checks')

@section('content')
    <div class="admin-page-head mb-2">
        <div>
            <p class="loot-muted mb-25">Cashouts / Face checks</p>
            <h1>Face verification captures</h1>
        </div>
    </div>

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

    <form class="admin-filter-bar mb-2" method="GET">
        <select class="form-select" name="status">
            <option value="">All statuses</option>
            @foreach(['pending', 'approved', 'rejected', 'duplicate_review', 'cashout_submitted'] as $status)
                <option value="{{ $status }}" @selected(request('status') === $status)>{{ ucfirst(str_replace('_', ' ', $status)) }}</option>
            @endforeach
        </select>
        <select class="form-select" name="context">
            <option value="">All contexts</option>
            <option value="profile" @selected(request('context') === 'profile')>Profile verification</option>
            <option value="cashout" @selected(request('context') === 'cashout')>Cashout check</option>
        </select>
        <button class="btn btn-loot">Filter</button>
        <a class="btn btn-outline-light" href="{{ route('admin.face-verifications') }}">Reset</a>
    </form>

    <form id="faceBulk" data-bulk-form="face-checks" method="POST" action="{{ route('admin.face-verifications.bulk') }}" class="admin-bulk-bar mb-2">
        @csrf
        <label class="admin-select-all"><input class="form-check-input" type="checkbox" data-bulk-select-all="face-checks"> Select page</label>
        <select class="form-select" name="action" required>
            <option value="">Bulk actions</option><option value="approve">Approve</option><option value="reject">Reject</option><option value="review">Needs review</option><option value="delete">Delete</option>
        </select>
        <button class="btn btn-outline-light" data-bulk-submit="face-checks" disabled>Apply</button>
        <span class="loot-muted" data-bulk-count="face-checks">0 selected</span>
        <button type="button" class="btn btn-outline-light ms-auto" data-bulk-export="face-checks" data-export-url="{{ route('admin.export.face-verifications', request()->query()) }}"><i data-feather="download" class="me-50"></i>Export CSV</button>
    </form>

    <div class="row">
        @forelse($verifications as $verification)
            <div class="col-xl-3 col-md-4 col-sm-6 mb-2">
                <div class="loot-action-card h-100">
                    <label class="face-card-select"><input class="form-check-input" type="checkbox" name="ids[]" value="{{ $verification->id }}" form="faceBulk" data-bulk-item="face-checks" aria-label="Select face check {{ $verification->id }}"><span>Select</span></label>
                    <img class="img-fluid rounded mb-1" src="{{ route('face-verifications.image', $verification) }}" alt="Face capture">
                    <div class="d-flex justify-content-between gap-1 mb-1">
                        <span class="loot-status {{ $verification->status === 'approved' ? 'success' : ($verification->status === 'rejected' ? 'danger' : 'warning') }}">
                            {{ ucfirst(str_replace('_', ' ', $verification->status)) }}
                        </span>
                        <span class="loot-status">{{ ucfirst($verification->context ?? 'profile') }}</span>
                    </div>
                    <p class="mb-50"><strong>{{ $verification->user?->username ?? 'Unknown user' }}</strong></p>
                    <p class="loot-muted mb-50">{{ $verification->user?->email }}</p>
                    @if($verification->withdrawal)
                        <p class="mb-50">Withdrawal #{{ $verification->withdrawal->id }} · <strong>{{ format_coins_as_usd($verification->withdrawal->amount) }}</strong><small class="d-block loot-muted">{{ number_format($verification->withdrawal->amount, 0) }} coins</small></p>
                    @endif
                    @if($verification->duplicateOfUser)
                        <p class="text-warning mb-50">Exact image replay matched {{ $verification->duplicateOfUser->username }}.</p>
                    @endif
                    <p class="loot-muted mb-1">{{ $verification->created_at->diffForHumans() }}</p>
                    <form method="POST" action="{{ route('admin.face-verifications.update', $verification) }}">
                        @csrf
                        @method('PATCH')
                        <textarea class="form-control mb-1" name="admin_note" rows="2" placeholder="Admin note">{{ $verification->admin_note }}</textarea>
                        <div class="d-flex flex-wrap gap-50">
                            <button class="btn btn-sm btn-success" name="action" value="approve">Approve</button>
                            <button class="btn btn-sm btn-danger" name="action" value="reject">Reject</button>
                            <button class="btn btn-sm btn-outline-warning" name="action" value="review">Review</button>
                        </div>
                    </form>
                </div>
            </div>
        @empty
            <div class="col-12">
                <div class="user-page-card text-center py-4">
                    <p class="loot-muted mb-0">No face captures found.</p>
                </div>
            </div>
        @endforelse
    </div>

    {{ $verifications->links() }}
@endsection
