@extends('layouts.member')

@section('title', 'Mon Portefeuille')
@section('page-title', 'Portefeuille')

@section('content')
<div class="row g-4 mb-4">
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Solde disponible</p>
            <h3 class="fw-700 text-success mb-2">{{ number_format($wallet->balance ?? 0, 0, ',', ' ') }} <span class="fs-6">XOF</span></h3>
            <small class="text-muted">Utilisable immédiatement</small>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Solde en attente</p>
            <h3 class="fw-700 text-warning mb-2">{{ number_format($wallet->pending_balance ?? 0, 0, ',', ' ') }} <span class="fs-6">XOF</span></h3>
            <small class="text-muted">En cours de déblocage</small>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Entrées (30j)</p>
            <h4 class="fw-700 text-primary mb-2">{{ number_format($incomesLast30 ?? 0, 0, ',', ' ') }} XOF</h4>
            <small class="text-muted">Commissions, ventes, bonus</small>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Sorties (30j)</p>
            <h4 class="fw-700 text-danger mb-2">{{ number_format($outcomesLast30 ?? 0, 0, ',', ' ') }} XOF</h4>
            <div class="d-flex justify-content-between align-items-center">
                <small class="text-muted">Retraits + achats</small>
                <a class="btn btn-sm btn-outline-primary" href="{{ route('member.wallet.withdrawals') }}">Retraits</a>
            </div>
        </div>
    </div>
</div>

<div class="card modern shadow-sm border-0 rounded-3">
    <div class="card-header bg-white border-bottom rounded-top-3 d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3">
        <div>
            <h5 class="mb-1">Historique des transactions</h5>
            <small class="text-muted">Filtrez, recherchez, exportez vos mouvements</small>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-sm btn-outline-secondary" href="{{ request()->fullUrlWithQuery(['export' => 'csv']) }}">
                <i class="bi bi-download me-1"></i> Export CSV
            </a>
            <a class="btn btn-sm btn-outline-secondary" href="{{ request()->fullUrlWithQuery(['export' => 'pdf']) }}">
                <i class="bi bi-file-earmark-pdf me-1"></i> Export PDF
            </a>
        </div>
    </div>

    <div class="card-body border-bottom">
        <form action="{{ route('member.wallet.index') }}" method="GET" class="row g-3 align-items-end">
            <div class="col-6 col-md-3">
                <label class="form-label small">Type</label>
                <select name="type" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <option value="credit" {{ request('type')==='credit' ? 'selected' : '' }}>Crédit</option>
                    <option value="debit" {{ request('type')==='debit' ? 'selected' : '' }}>Débit</option>
                </select>
            </div>
            <div class="col-6 col-md-3">
                <label class="form-label small">Statut</label>
                <select name="status" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <option value="completed" {{ request('status')==='completed' ? 'selected' : '' }}>Terminé</option>
                    <option value="pending" {{ request('status')==='pending' ? 'selected' : '' }}>En attente</option>
                    <option value="failed" {{ request('status')==='failed' ? 'selected' : '' }}>Échoué</option>
                </select>
            </div>
            <div class="col-6 col-md-3">
                <label class="form-label small">Du</label>
                <input type="date" name="from" class="form-control form-control-sm" value="{{ request('from') }}">
            </div>
            <div class="col-6 col-md-3">
                <label class="form-label small">Au</label>
                <input type="date" name="to" class="form-control form-control-sm" value="{{ request('to') }}">
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label small">Recherche</label>
                <input type="text" name="q" class="form-control form-control-sm" placeholder="Description, référence..." value="{{ request('q') }}">
            </div>
            <div class="col-12 col-md-6 d-flex gap-2 justify-content-end">
                <a href="{{ route('member.wallet.index') }}" class="btn btn-outline-secondary btn-sm">Réinitialiser</a>
                <button type="submit" class="btn btn-primary btn-sm">Filtrer</button>
            </div>
        </form>
    </div>

    <div class="table-responsive">
        <table class="table table-hover table-modern mb-0">
            <thead class="table-light">
                <tr>
                    <th class="small text-muted fw-600">Date</th>
                    <th class="small text-muted fw-600">Type</th>
                    <th class="small text-muted fw-600">Description</th>
                    <th class="small text-muted fw-600 text-end">Montant</th>
                    <th class="small text-muted fw-600">Statut</th>
                </tr>
            </thead>
            <tbody>
                @forelse($transactions as $transaction)
                    @php
                        // Déterminer crédit/débit par le signe du montant
                        $isCredit = ($transaction->amount ?? 0) > 0;
                        
                        // Traduire le type de transaction
                        $typeLabels = [
                            'bonus' => 'Bonus',
                            'commission' => 'Commission',
                            'withdrawal' => 'Retrait',
                            'withdrawal_request' => 'Demande de retrait',
                            'transfer' => 'Transfert',
                            'subscription' => 'Abonnement',
                            'deposit' => 'Dépôt',
                            'refund' => 'Remboursement',
                            'payment' => 'Paiement',
                            'sale' => 'Vente',
                            'purchase' => 'Achat',
                            'investment' => 'Investissement',
                            'credit' => 'Crédit',
                            'debit' => 'Débit',
                        ];
                        $typeLabel = $typeLabels[$transaction->type] ?? ucfirst($transaction->type ?? 'Transaction');
                    @endphp
                    <tr class="align-middle">
                        <td class="small">{{ $transaction->created_at?->format('d/m/Y H:i') ?? '—' }}</td>
                        <td>
                            <span class="badge rounded-pill {{ $isCredit ? 'bg-success-subtle text-success' : 'bg-danger-subtle text-danger' }}">
                                {{ $typeLabel }}
                            </span>
                        </td>
                        <td class="small">{{ $transaction->description ?? '—' }}</td>
                        <td class="text-end fw-600 {{ $isCredit ? 'text-success' : 'text-danger' }}">
                            {{ $isCredit ? '+' : '' }}{{ number_format($transaction->amount ?? 0, 0, ',', ' ') }} XOF
                        </td>
                        <td>
                            @php
                                $st = $transaction->status ?? 'completed';
                                $map = [
                                    'completed' => ['success','Terminé'],
                                    'pending' => ['warning','En attente'],
                                    'failed' => ['danger','Échoué']
                                ];
                                $badge = $map[$st] ?? ['secondary', ucfirst($st)];
                            @endphp
                            <span class="badge rounded-pill bg-{{ $badge[0] }} bg-opacity-75">{{ $badge[1] }}</span>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">
                            <i class="bi bi-inbox fs-3 d-block mb-2 opacity-50"></i>
                            Aucune transaction trouvée
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="card-footer bg-light d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-2">
        <small class="text-muted">Total: {{ $transactions->total() ?? 0 }} lignes</small>
        <div>{{ $transactions->appends(request()->query())->links() }}</div>
    </div>
</div>

@push('scripts')
<script>
// Rien de spécifique ici pour l'instant; hooks prêts si besoin de toasts/exports dynamiques.
</script>
@endpush
@endsection
