{{-- Onglet Suivi AMM --}}
<div class="row g-4">
    {{-- En-tête AMM --}}
    <div class="col-12">
        <div class="alert alert-info d-flex align-items-center">
            <i class="bi bi-info-circle me-3 fs-4"></i>
            <div>
                <strong>Achat Mensuel Minimum (AMM) : {{ number_format($ammRequired ?? 50000, 0, ',', ' ') }} XOF</strong>
                <p class="mb-0 small">Mois en cours : {{ $currentMonth ?? now()->format('F Y') }} • Jours restants : {{ now()->daysInMonth - now()->day }}</p>
            </div>
            <div class="ms-auto">
                <form action="{{ route('admin.analytics.bulk-amm-reminders') }}" method="POST" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-warning" onclick="return confirm('Envoyer des rappels à tous les membres sans AMM ?')">
                        <i class="bi bi-send me-1"></i> Rappels en Masse
                    </button>
                </form>
            </div>
        </div>
    </div>
    
    {{-- Statistiques AMM --}}
    <div class="col-md-3">
        <div class="stat-card success p-3 text-center">
            <div class="h2 mb-1 fw-bold text-success">{{ $membersWithAmm->count() ?? 0 }}</div>
            <small class="text-muted">AMM Atteint</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card primary p-3 text-center">
            <div class="h2 mb-1 fw-bold text-primary">{{ isset($membersWithAdvancePayment) ? $membersWithAdvancePayment->count() : 0 }}</div>
            <small class="text-muted">Avec Paiement Avancé</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card danger p-3 text-center">
            <div class="h2 mb-1 fw-bold text-danger">{{ $membersWithoutAmm->count() ?? 0 }}</div>
            <small class="text-muted">Sans AMM</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card info p-3 text-center">
            @php 
                $totalActive = ($membersWithAmm->count() ?? 0) + ($membersWithoutAmm->count() ?? 0);
                $ammRate = $totalActive > 0 ? round((($membersWithAmm->count() ?? 0) / $totalActive) * 100, 1) : 0;
            @endphp
            <div class="h2 mb-1 fw-bold">{{ $ammRate }}%</div>
            <small class="text-muted">Taux de conformité</small>
        </div>
    </div>
    
    {{-- Graphique Evolution AMM --}}
    <div class="col-md-6">
        <div class="card h-100">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-graph-up text-primary me-2"></i>Évolution AMM (6 mois)</h5>
            </div>
            <div class="card-body">
                <canvas id="ammHistoryChart" height="250"></canvas>
            </div>
        </div>
    </div>
    
    {{-- Membres avec AMM --}}
    <div class="col-md-6">
        <div class="card h-100">
            <div class="card-header bg-white bg-success bg-opacity-10">
                <h5 class="mb-0 text-success"><i class="bi bi-check-circle me-2"></i>Membres avec AMM Validé</h5>
            </div>
            <div class="card-body p-0" style="max-height: 300px; overflow-y: auto;">
                @if(isset($membersWithAmm) && $membersWithAmm->isNotEmpty())
                    <div class="table-responsive">
                        <table class="table table-sm table-hover mb-0">
                            <thead class="table-light sticky-top">
                                <tr>
                                    <th>Membre</th>
                                    <th class="text-end">Achats</th>
                                    <th class="text-end">Avance</th>
                                    <th class="text-end">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($membersWithAmm as $tracking)
                                <tr>
                                    <td>{{ $tracking->user->name ?? 'Inconnu' }}</td>
                                    <td class="text-end">
                                        <span class="badge bg-success">{{ number_format($tracking->total_purchase_amount, 0, ',', ' ') }}</span>
                                    </td>
                                    <td class="text-end">
                                        @if(($tracking->advance_payment_amount ?? 0) > 0)
                                            <span class="badge bg-primary">+{{ number_format($tracking->advance_payment_amount ?? 0, 0, ',', ' ') }}</span>
                                        @else
                                            <span class="text-muted">-</span>
                                        @endif
                                    </td>
                                    <td class="text-end fw-bold">{{ number_format($tracking->total_with_advance ?? $tracking->total_purchase_amount, 0, ',', ' ') }} XOF</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center text-muted py-4">
                        <p>Aucun membre n'a atteint l'AMM ce mois</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
    
    {{-- Membres sans AMM --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-white bg-danger bg-opacity-10 d-flex justify-content-between align-items-center">
                <h5 class="mb-0 text-danger"><i class="bi bi-exclamation-triangle me-2"></i>Membres Sans AMM ({{ $membersWithoutAmm->count() }})</h5>
                <span class="badge bg-danger">À contacter</span>
            </div>
            <div class="card-body p-0">
                @if(isset($membersWithoutAmm) && $membersWithoutAmm->isNotEmpty())
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Membre</th>
                                    <th>Contact</th>
                                    <th class="text-center">Progression</th>
                                    <th class="text-end">Acheté</th>
                                    <th class="text-end">Avance</th>
                                    <th class="text-end">Restant</th>
                                    <th class="text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($membersWithoutAmm->take(50) as $member)
                                <tr class="member-row">
                                    <td>
                                        <strong>{{ $member->name }}</strong>
                                    </td>
                                    <td>
                                        <small>
                                            @if($member->email)
                                                <i class="bi bi-envelope text-muted"></i> {{ Str::limit($member->email, 20) }}<br>
                                            @endif
                                            @if($member->phone)
                                                <i class="bi bi-phone text-muted"></i> {{ $member->phone }}
                                            @endif
                                        </small>
                                    </td>
                                    <td style="min-width: 150px;">
                                        <div class="progress progress-slim mb-1">
                                            <div class="progress-bar bg-{{ $member->percentage >= 75 ? 'warning' : ($member->percentage >= 50 ? 'info' : 'danger') }}" 
                                                 style="width: {{ $member->percentage }}%"></div>
                                        </div>
                                        <small class="text-muted">{{ $member->percentage }}%</small>
                                    </td>
                                    <td class="text-end">{{ number_format($member->total_purchase, 0, ',', ' ') }} XOF</td>
                                    <td class="text-end">
                                        @if(($member->advance_payment ?? 0) > 0)
                                            <span class="badge bg-primary">+{{ number_format($member->advance_payment, 0, ',', ' ') }}</span>
                                        @else
                                            <span class="text-muted">-</span>
                                        @endif
                                    </td>
                                    <td class="text-end">
                                        <span class="text-danger fw-bold">{{ number_format($member->remaining, 0, ',', ' ') }} XOF</span>
                                    </td>
                                    <td class="text-center">
                                        @if($member->email)
                                            <form action="{{ route('admin.analytics.send-amm-reminder') }}" method="POST" class="d-inline">
                                                @csrf
                                                <input type="hidden" name="member_id" value="{{ $member->id }}">
                                                <input type="hidden" name="channel" value="email">
                                                <button type="submit" class="btn btn-sm btn-outline-primary" title="Rappel Email">
                                                    <i class="bi bi-envelope"></i>
                                                </button>
                                            </form>
                                        @endif
                                        @if($member->whatsapp)
                                            <form action="{{ route('admin.analytics.send-amm-reminder') }}" method="POST" class="d-inline">
                                                @csrf
                                                <input type="hidden" name="member_id" value="{{ $member->id }}">
                                                <input type="hidden" name="channel" value="whatsapp">
                                                <button type="submit" class="btn btn-sm btn-outline-success" title="Rappel WhatsApp">
                                                    <i class="bi bi-whatsapp"></i>
                                                </button>
                                            </form>
                                        @endif
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center text-muted py-4">
                        <i class="bi bi-emoji-smile text-success" style="font-size: 3rem;"></i>
                        <p class="mt-2">Tous les membres ont atteint leur AMM ce mois !</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
    
    {{-- Membres Exonérés --}}
    @if(isset($exemptMembers) && $exemptMembers->isNotEmpty())
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-shield-check text-warning me-2"></i>Membres Exonérés d'AMM</h5>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Membre</th>
                                <th>Email</th>
                                <th>Exonéré jusqu'au</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($exemptMembers as $member)
                            <tr>
                                <td>{{ $member->name }}</td>
                                <td>{{ $member->email }}</td>
                                <td><span class="badge bg-warning">{{ $member->amm_exemption_until->format('d/m/Y') }}</span></td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    @endif
</div>

@push('scripts')
<script>
    @if(isset($ammHistory))
    new Chart(document.getElementById('ammHistoryChart'), {
        type: 'bar',
        data: {
            labels: {!! json_encode(array_column($ammHistory, 'label')) !!},
            datasets: [{
                label: 'AMM Atteint',
                data: {!! json_encode(array_column($ammHistory, 'with_amm')) !!},
                backgroundColor: 'rgba(16, 185, 129, 0.8)',
            }, {
                label: 'Avec Paiement Avancé',
                data: {!! json_encode(array_column($ammHistory, 'with_advance')) !!},
                backgroundColor: 'rgba(59, 130, 246, 0.8)',
            }, {
                label: 'Sans AMM',
                data: {!! json_encode(array_column($ammHistory, 'without_amm')) !!},
                backgroundColor: 'rgba(239, 68, 68, 0.8)',
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            scales: {
                x: { stacked: true },
                y: { stacked: true, beginAtZero: true }
            }
        }
    });
    @endif
</script>
@endpush
