@extends('layouts.admin')

@section('title', 'Historique des récompenses')

@section('content')
<div class="container-fluid">
    <div class="d-flex align-items-center mb-4">
        <a href="{{ route('admin.top-sponsors.index') }}" class="btn btn-outline-secondary me-3">
            <i class="fas fa-arrow-left"></i>
        </a>
        <div>
            <h1 class="h3 mb-0">Historique des récompenses</h1>
            <p class="text-muted mb-0">Bonus attribués aux parrains du mois</p>
        </div>
    </div>

    <div class="card">
        <div class="card-body p-0">
            @if($history->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Membre</th>
                            <th>Période</th>
                            <th>Montant</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($history as $transaction)
                        <tr>
                            <td>{{ $transaction->created_at->format('d/m/Y H:i') }}</td>
                            <td>
                                @if($transaction->user)
                                    <a href="{{ route('admin.users.show', $transaction->user) }}">
                                        {{ $transaction->user->name }}
                                    </a>
                                @else
                                    <span class="text-muted">Utilisateur supprimé</span>
                                @endif
                            </td>
                            <td>
                                @if($transaction->metadata)
                                    @php
                                        $meta = is_string($transaction->metadata) ? json_decode($transaction->metadata, true) : $transaction->metadata;
                                    @endphp
                                    {{ $meta['month'] ?? '-' }}/{{ $meta['year'] ?? '-' }}
                                @else
                                    -
                                @endif
                            </td>
                            <td>
                                <span class="text-success fw-bold">
                                    +{{ number_format($transaction->amount, 0, ',', ' ') }} FCFA
                                </span>
                            </td>
                            <td>{{ $transaction->description ?? 'Bonus parrain du mois' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="text-center py-5">
                <i class="fas fa-gift fa-3x text-muted mb-3"></i>
                <p class="text-muted">Aucune récompense attribuée pour le moment</p>
            </div>
            @endif
        </div>
        @if($history->hasPages())
        <div class="card-footer">
            {{ $history->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
