@extends('layouts.marketing')

@section('title', 'Dashboard Marketing')
@section('page-title', 'Tableau de Bord Marketing')

@section('content')
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="card bg-primary text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Total Membres</h6>
                        <h3 class="mb-0">{{ number_format($stats['total_users']) }}</h3>
                    </div>
                    <i class="bi bi-people fs-1 opacity-50"></i>
                </div>
                <small>{{ $stats['active_users'] }} actifs</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-success text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Nouveaux ce mois</h6>
                        <h3 class="mb-0">{{ number_format($stats['new_users_month']) }}</h3>
                    </div>
                    <i class="bi bi-person-plus fs-1 opacity-50"></i>
                </div>
                <small>Inscriptions</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-warning text-dark h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Commandes</h6>
                        <h3 class="mb-0">{{ number_format($stats['total_orders']) }}</h3>
                    </div>
                    <i class="bi bi-cart3 fs-1 opacity-50"></i>
                </div>
                <small>{{ $stats['orders_month'] }} ce mois</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-info text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Revenus</h6>
                        <h3 class="mb-0">{{ number_format($stats['total_revenue'], 0, ',', ' ') }}</h3>
                    </div>
                    <i class="bi bi-cash-stack fs-1 opacity-50"></i>
                </div>
                <small>{{ number_format($stats['revenue_month'], 0, ',', ' ') }} XOF ce mois</small>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-graph-up me-2"></i>Inscriptions (7 derniers jours)</h5>
            </div>
            <div class="card-body">
                <canvas id="registrationsChart" height="100"></canvas>
            </div>
        </div>
        
        <div class="card mt-3">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-trophy me-2"></i>Top 5 Produits</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Produit</th>
                                <th>Prix</th>
                                <th>Ventes</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($topProducts as $product)
                                <tr>
                                    <td>{{ $product->name }}</td>
                                    <td>{{ number_format($product->price, 0, ',', ' ') }} XOF</td>
                                    <td><span class="badge bg-success">{{ $product->order_items_count }}</span></td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3" class="text-center text-muted">Aucun produit vendu</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-person-check me-2"></i>Utilisateurs Récents</h5>
            </div>
            <div class="card-body" style="max-height: 500px; overflow-y: auto;">
                @forelse($recentUsers as $user)
                    <div class="d-flex align-items-center mb-3 pb-3 border-bottom">
                        <div class="flex-shrink-0">
                            <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                {{ strtoupper(substr($user->name, 0, 1)) }}
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="mb-0">{{ $user->name }}</h6>
                            <small class="text-muted">{{ $user->email }}</small>
                            <br>
                            <small class="text-muted"><i class="bi bi-calendar"></i> {{ $user->created_at->diffForHumans() }}</small>
                        </div>
                    </div>
                @empty
                    <p class="text-center text-muted">Aucun utilisateur récent</p>
                @endforelse
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
const ctx = document.getElementById('registrationsChart');
const registrations = @json($registrations);

const labels = registrations.map(r => r.date);
const data = registrations.map(r => r.count);

new Chart(ctx, {
    type: 'line',
    data: {
        labels: labels,
        datasets: [{
            label: 'Inscriptions',
            data: data,
            borderColor: '#0d6efd',
            backgroundColor: 'rgba(13, 110, 253, 0.1)',
            tension: 0.4,
            fill: true
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                display: false
            }
        },
        scales: {
            y: {
                beginAtZero: true,
                ticks: {
                    stepSize: 1
                }
            }
        }
    }
});
</script>
@endpush
