@extends('bo.layouts.backend')

@section('title', 'Retraits BO · LineUp France')

@section('content')
    <div class="bg-body-light bo-page-top">
        <div class="content content-full bo-page-top-content">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2 bo-page-head">
                <div class="flex-grow-1">
                    <h1 class="h3 fw-bold mb-1 bo-page-title">Retraits BO</h1>
                    <h2 class="fs-base lh-base fw-medium text-muted mb-0 bo-page-subtitle">Suivi et traitement des retraits liners.</h2>
                </div>
                <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-alt bo-breadcrumb">
                        <li class="breadcrumb-item">LineUp France</li>
                        <li class="breadcrumb-item" aria-current="page">Retraits</li>
                    </ol>
                </nav>
            </div>
            <div class="bo-hero-actions">
                <a href="{{ route('bo.dashboard') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-speedometer me-1"></i> Tableau de bord
                </a>
                <a href="{{ route('bo.finance') }}" class="btn btn-sm btn-alt-primary">
                    <i class="si si-wallet me-1"></i> Finance
                </a>
                <a href="{{ route('bo.kyc.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-shield me-1"></i> KYC
                </a>
            </div>
        </div>
    </div>

    <div class="content bo-page-content">
        <div class="block block-rounded">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-equalizer me-1"></i>Filtres avances</h3>
                <div class="block-options">
                    <a href="{{ route('bo.payouts.index') }}" class="btn btn-sm btn-alt-secondary">
                        <i class="fa fa-rotate-left opacity-50 me-1"></i> Reinitialiser
                    </a>
                </div>
            </div>
            <div class="block-content">
                <form method="GET" action="{{ route('bo.payouts.index') }}" class="row g-3 align-items-end bo-filter-form">
                    <div class="col-md-3">
                        <label class="form-label" for="q">Recherche</label>
                        <div class="input-group">
                            <span class="input-group-text bg-body-light border-0"><i class="fa fa-search text-muted"></i></span>
                            <input id="q" type="text" name="q" class="form-control form-control-alt" value="{{ $search }}" placeholder="Liner, e-mail, reference">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="status">Statut</label>
                        <select id="status" name="status" class="form-select">
                            <option value="">Tous</option>
                            @foreach(['pending', 'paid', 'failed'] as $statusOption)
                                @php
                                    $statusLabel = match ($statusOption) {
                                        'pending' => 'En attente',
                                        'paid' => 'Paye',
                                        'failed' => 'Echoue',
                                        default => ucfirst(str_replace('_', ' ', $statusOption)),
                                    };
                                @endphp
                                <option value="{{ $statusOption }}" @selected($status === $statusOption)>{{ $statusLabel }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="provider">Fournisseur</label>
                        <select id="provider" name="provider" class="form-select">
                            <option value="">Tous</option>
                            @foreach($providers as $providerOption)
                                <option value="{{ $providerOption }}" @selected($provider === $providerOption)>{{ strtoupper($providerOption) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label" for="date_from">Date demande</label>
                        <div class="input-group">
                            <input id="date_from" type="date" name="date_from" class="form-control form-control-alt" value="{{ $dateFrom }}">
                            <input id="date_to" type="date" name="date_to" class="form-control form-control-alt" value="{{ $dateTo }}">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="amount_min">Montant min/max</label>
                        <div class="input-group">
                            <input id="amount_min" type="number" step="0.01" min="0" name="amount_min" class="form-control form-control-alt" value="{{ $amountMin }}">
                            <input id="amount_max" type="number" step="0.01" min="0" name="amount_max" class="form-control form-control-alt" value="{{ $amountMax }}">
                        </div>
                    </div>
                    <div class="col-md-3 d-flex gap-2 bo-filter-actions">
                        <button type="submit" class="btn btn-alt-primary w-100">Filtrer</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="block block-rounded mb-0">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-wallet me-1"></i>Retraits liners</h3>
                <div class="block-options">
                    <span class="badge bg-primary">{{ $payouts->total() }} resultat(s)</span>
                </div>
            </div>
            <div class="block-content p-0">
                <div class="table-responsive">
                    <table class="table table-vcenter table-striped table-hover fs-sm mb-0 bo-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Liner</th>
                            <th>Methode</th>
                            <th>Montant</th>
                            <th>Statut</th>
                            <th>Dates</th>
                            <th style="width: 220px;">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($payouts as $payout)
                            <tr>
                                <td>#{{ $payout->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $payout->liner?->name ?? '-' }}</div>
                                    <div class="text-muted">{{ $payout->liner?->email ?? '-' }}</div>
                                </td>
                                <td>
                                    <div>{{ strtoupper($payout->provider ?? '-') }}</div>
                                    @if($hasPaymentMethodsTable && $payout->paymentMethod)
                                        <small class="text-muted">{{ $payout->paymentMethod->label }}</small>
                                    @endif
                                    @if($payout->provider_reference)
                                        <small class="d-block text-muted">{{ $payout->provider_reference }}</small>
                                    @endif
                                </td>
                                <td>{{ number_format((float) $payout->amount, 2, ',', ' ') }} {{ $payout->currency }}</td>
                                <td>
                                    @php
                                        $badgeClass = match($payout->status) {
                                            'paid' => 'bg-success',
                                            'failed' => 'bg-danger',
                                            default => 'bg-warning text-dark',
                                        };
                                    @endphp
                                    @php
                                        $payoutStatusLabel = match ($payout->status) {
                                            'pending' => 'En attente',
                                            'paid' => 'Paye',
                                            'failed' => 'Echoue',
                                            default => ucfirst(str_replace('_', ' ', (string) $payout->status)),
                                        };
                                    @endphp
                                    <span class="badge {{ $badgeClass }}">{{ $payoutStatusLabel }}</span>
                                    @if($payout->failure_reason)
                                        <small class="d-block text-danger mt-1">{{ $payout->failure_reason }}</small>
                                    @endif
                                </td>
                                <td>
                                    <div>Demande: {{ $payout->requested_at?->format('d/m/Y H:i') ?? '-' }}</div>
                                    <small class="text-muted">Paye: {{ $payout->paid_at?->format('d/m/Y H:i') ?? '-' }}</small>
                                </td>
                                <td>
                                    <div class="bo-table-actions">
                                        <form method="POST" action="{{ route('bo.finance.payouts.process', $payout) }}">
                                            @csrf
                                            <button class="btn btn-sm btn-alt-primary" type="submit">
                                                <i class="fa fa-rotate me-1"></i>Traiter
                                            </button>
                                        </form>
                                        <form method="POST" action="{{ route('bo.finance.payouts.mark_paid', $payout) }}">
                                            @csrf
                                            <button class="btn btn-sm btn-success" type="submit">
                                                <i class="fa fa-check me-1"></i>Marquer paye
                                            </button>
                                        </form>
                                        <form method="POST" action="{{ route('bo.finance.payouts.mark_failed', $payout) }}" class="d-flex gap-2">
                                            @csrf
                                            <input class="form-control form-control-sm" type="text" name="reason" placeholder="Motif" required>
                                            <button class="btn btn-sm btn-danger" type="submit">
                                                <i class="fa fa-xmark me-1"></i>Marquer en echec
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center text-muted">Aucun retrait.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="block-content">
                {{ $payouts->links() }}
            </div>
        </div>
    </div>
@endsection
