<?php
// Gráfico de vendas dos últimos 7 dias
$labels = [];
$values = [];
for ($i = 6; $i >= 0; $i--) {
    $data = date('Y-m-d', strtotime("-$i days"));
    $labels[] = date('d/m', strtotime($data));
    $stmt = $pdo->prepare("SELECT SUM(value) as total FROM transactions WHERE status = 'paid' AND DATE(created_at) = :data");
    $stmt->execute([':data' => $data]);
    $total = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
    $values[] = floatval($total);
}
?>
<div class="space-y-6">
    <!-- Cards de Estatísticas -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
        <div class="bg-gradient-to-br from-blue-500 to-blue-600 rounded-xl p-6 text-white shadow-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-blue-100 text-sm">Total de Usuários</p>
                    <p class="text-3xl font-bold mt-2"><?= number_format($totalUsers, 0, ',', '.') ?></p>
                </div>
                <i data-lucide="users" class="w-12 h-12 text-blue-200"></i>
            </div>
        </div>
        
        <div class="bg-gradient-to-br from-green-500 to-green-600 rounded-xl p-6 text-white shadow-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-green-100 text-sm">Investimentos Ativos</p>
                    <p class="text-3xl font-bold mt-2"><?= number_format($totalInvestments, 0, ',', '.') ?></p>
                </div>
                <i data-lucide="trending-up" class="w-12 h-12 text-green-200"></i>
            </div>
        </div>
        
        <div class="bg-gradient-to-br from-purple-500 to-purple-600 rounded-xl p-6 text-white shadow-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-purple-100 text-sm">Total Investido</p>
                    <p class="text-3xl font-bold mt-2">R$ <?= number_format($totalInvested, 2, ',', '.') ?></p>
                </div>
                <i data-lucide="dollar-sign" class="w-12 h-12 text-purple-200"></i>
            </div>
        </div>
        
        <div class="bg-gradient-to-br from-orange-500 to-orange-600 rounded-xl p-6 text-white shadow-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-orange-100 text-sm">Saques Pendentes</p>
                    <p class="text-3xl font-bold mt-2"><?= number_format($totalPendingWithdrawals, 0, ',', '.') ?></p>
                </div>
                <i data-lucide="alert-circle" class="w-12 h-12 text-orange-200"></i>
            </div>
        </div>
    </div>

    <!-- Gráfico de Vendas -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <h3 class="text-xl font-bold text-gray-800 mb-4">Vendas dos Últimos 7 Dias</h3>
        <canvas id="salesChart" height="100"></canvas>
    </div>

    <!-- Tabelas Recentes -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- Investimentos Recentes -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h3 class="text-xl font-bold text-gray-800 mb-4">Investimentos Recentes</h3>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-gray-700">Usuário</th>
                            <th class="px-4 py-3 text-left text-gray-700">Valor</th>
                            <th class="px-4 py-3 text-left text-gray-700">Data</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentInvestments as $inv): ?>
                        <tr class="border-t">
                            <td class="px-4 py-3"><?= htmlspecialchars($inv['user_name']) ?></td>
                            <td class="px-4 py-3 font-semibold">R$ <?= number_format($inv['invested_amount'], 2, ',', '.') ?></td>
                            <td class="px-4 py-3 text-gray-500"><?= date('d/m/Y H:i', strtotime($inv['created_at'])) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Saques Pendentes -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h3 class="text-xl font-bold text-gray-800 mb-4">Saques Pendentes</h3>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-gray-700">Usuário</th>
                            <th class="px-4 py-3 text-left text-gray-700">Valor</th>
                            <th class="px-4 py-3 text-left text-gray-700">Data</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($pendingWithdrawals as $with): ?>
                        <tr class="border-t">
                            <td class="px-4 py-3"><?= htmlspecialchars($with['user_name']) ?></td>
                            <td class="px-4 py-3 font-semibold">R$ <?= number_format($with['value'], 2, ',', '.') ?></td>
                            <td class="px-4 py-3 text-gray-500"><?= date('d/m/Y H:i', strtotime($with['created_at'])) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
// Gráfico de vendas
const ctx = document.getElementById('salesChart');
if (ctx) {
    new Chart(ctx, {
        type: 'line',
        data: {
            labels: <?= json_encode($labels) ?>,
            datasets: [{
                label: 'Vendas (R$)',
                data: <?= json_encode($values) ?>,
                borderColor: 'rgb(99, 102, 241)',
                backgroundColor: 'rgba(99, 102, 241, 0.1)',
                tension: 0.4,
                fill: true
            }]
        },
        options: {
            responsive: true,
            plugins: {
                legend: {
                    display: false
                }
            },
            scales: {
                y: {
                    beginAtZero: true
                }
            }
        }
    });
}
lucide.createIcons();
</script>
