@extends('layouts.admin')

@section('title', 'Historique du Pool')
@section('page-title', 'Historique des Distributions de Pool')

@section('content')
<div class="row mb-4">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0"><i class="bi bi-clock-history me-2"></i>Toutes les Distributions</h5>
            </div>
            <div class="card-body">
                @if($shares->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Utilisateur</th>
                                    <th>Période</th>
                                    <th class="text-end">Montant (XOF)</th>
                                    <th class="text-center">Pourcentage</th>
                                    <th class="text-center">Statut</th>
                                    <th class="text-center">Payé le</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($shares as $share)
                                    <tr>
                                        <td>
                                            <small class="text-muted">
                                                {{ $share->created_at->format('d/m/Y H:i') }}
                                            </small>
                                        </td>
                                        <td>
                                            <strong>{{ $share->user->name ?? 'N/A' }}</strong>
                                            @if($share->user)
                                                <br><small class="text-muted">{{ $share->user->email }}</small>
                                            @endif
                                        </td>
                                        <td>
                                            <span class="badge bg-light text-dark">
                                                {{ $share->period_start->format('m/Y') }} - {{ $share->period_end->format('m/Y') }}
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            <strong class="text-success">
                                                {{ number_format($share->amount, 0, ',', ' ') }}
                                            </strong>
                                        </td>
                                        <td class="text-center">
                                            <span class="badge bg-info">
                                                {{ number_format($share->percentage, 2, ',', ' ') }}%
                                            </span>
                                        </td>
                                        <td class="text-center">
                                            @if($share->status === 'paid')
                                                <span class="badge bg-success">
                                                    <i class="bi bi-check-circle me-1"></i>Payé
                                                </span>
                                            @elseif($share->status === 'pending')
                                                <span class="badge bg-warning">
                                                    <i class="bi bi-clock me-1"></i>En attente
                                                </span>
                                            @else
                                                <span class="badge bg-secondary">
                                                    {{ ucfirst($share->status) }}
                                                </span>
                                            @endif
                                        </td>
                                        <td class="text-center">
                                            @if($share->paid_at)
                                                <small class="text-muted">
                                                    {{ $share->paid_at->format('d/m/Y') }}
                                                </small>
                                            @else
                                                <span class="text-muted">—</span>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <nav class="mt-4">
                        {{ $shares->links() }}
                    </nav>

                    <!-- Totaux -->
                    <div class="row mt-4 pt-4 border-top">
                        <div class="col-md-4">
                            <div class="p-3 bg-light rounded">
                                <p class="text-muted mb-1">Total distribué</p>
                                <h4 class="text-success mb-0">
                                    {{ number_format($totalDistributed, 0, ',', ' ') }} XOF
                                </h4>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="p-3 bg-light rounded">
                                <p class="text-muted mb-1">Nombre total de bénéficiaires</p>
                                <h4 class="text-primary mb-0">
                                    {{ $totalDistributors }}
                                </h4>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="p-3 bg-light rounded">
                                <p class="text-muted mb-1">Montant moyen par part</p>
                                <h4 class="text-info mb-0">
                                    {{ $totalDistributors > 0 ? number_format($totalDistributed / $totalDistributors, 0, ',', ' ') : 0 }} XOF
                                </h4>
                            </div>
                        </div>
                    </div>
                @else
                    <div class="alert alert-info">
                        <i class="bi bi-info-circle me-2"></i>
                        Aucune distribution n'a encore eu lieu.
                    </div>
                @endif
            </div>
        </div>

        <div class="mt-4">
            <a href="{{ route('admin.pool.index') }}" class="btn btn-secondary">
                <i class="bi bi-arrow-left me-2"></i>Retour au Dashboard
            </a>
        </div>
    </div>
</div>

<style>
    .stat-card {
        padding: 20px;
        border-radius: 8px;
        background: #f8f9fa;
        border: 1px solid #dee2e6;
    }

    table.table tbody tr {
        transition: background-color 0.2s;
    }

    table.table tbody tr:hover {
        background-color: #f8f9fa;
    }
</style>
@endsection
