<?php ob_start(); ?>

<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
    <div class="bg-white rounded-xl shadow-sm p-6 border border-gray-100">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-sm text-gray-500 mb-1">Entreprises</p>
                <p class="text-3xl font-bold text-gray-800"><?= $stats['total'] ?? 0 ?></p>
            </div>
            <div class="w-12 h-12 bg-indigo-100 rounded-lg flex items-center justify-center">
                <i class="fas fa-building text-xl text-indigo-600"></i>
            </div>
        </div>
        <p class="text-sm text-green-500 mt-2">
            <i class="fas fa-check-circle mr-1"></i><?= $stats['actives'] ?? 0 ?> actives
        </p>
    </div>
    
    <div class="bg-white rounded-xl shadow-sm p-6 border border-gray-100">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-sm text-gray-500 mb-1">Conversations</p>
                <p class="text-3xl font-bold text-gray-800"><?= number_format($totalConversations) ?></p>
            </div>
            <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                <i class="fas fa-comments text-xl text-green-600"></i>
            </div>
        </div>
        <p class="text-sm text-gray-500 mt-2">Total plateforme</p>
    </div>
    
    <div class="bg-white rounded-xl shadow-sm p-6 border border-gray-100">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-sm text-gray-500 mb-1">Agents</p>
                <p class="text-3xl font-bold text-gray-800"><?= $totalAgents ?></p>
            </div>
            <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                <i class="fas fa-users text-xl text-purple-600"></i>
            </div>
        </div>
        <p class="text-sm text-gray-500 mt-2">Tous les agents</p>
    </div>
    
    <div class="bg-white rounded-xl shadow-sm p-6 border border-gray-100">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-sm text-gray-500 mb-1">Abonnements</p>
                <p class="text-3xl font-bold text-gray-800"><?= ($stats['pro'] ?? 0) + ($stats['enterprise'] ?? 0) ?></p>
            </div>
            <div class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center">
                <i class="fas fa-crown text-xl text-yellow-600"></i>
            </div>
        </div>
        <p class="text-sm text-gray-500 mt-2">Pro & Enterprise</p>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <!-- Entreprises récentes -->
    <div class="lg:col-span-2 bg-white rounded-xl shadow-sm border border-gray-100">
        <div class="p-6 border-b border-gray-100 flex justify-between items-center">
            <h3 class="text-lg font-semibold text-gray-800">
                <i class="fas fa-building mr-2 text-indigo-600"></i>Entreprises
            </h3>
            <a href="<?= BASE_URL ?>/super-admin/entreprises" class="text-sm text-indigo-600 hover:text-indigo-800">
                Voir tout <i class="fas fa-arrow-right ml-1"></i>
            </a>
        </div>
        <div class="p-6">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="text-left text-sm text-gray-500">
                            <th class="pb-3">Entreprise</th>
                            <th class="pb-3">Secteur</th>
                            <th class="pb-3">Abonnement</th>
                            <th class="pb-3">Statut</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach (array_slice($entreprises, 0, 5) as $e): ?>
                        <tr class="border-t border-gray-100">
                            <td class="py-3">
                                <p class="font-medium text-gray-800"><?= htmlspecialchars($e['nom']) ?></p>
                                <p class="text-xs text-gray-500"><?= htmlspecialchars($e['email'] ?? '-') ?></p>
                            </td>
                            <td class="py-3 text-gray-600"><?= htmlspecialchars($e['secteur'] ?? '-') ?></td>
                            <td class="py-3">
                                <span class="px-2 py-1 text-xs rounded-full 
                                    <?= $e['type_abonnement'] === 'enterprise' ? 'bg-purple-100 text-purple-700' : 
                                       ($e['type_abonnement'] === 'pro' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-700') ?>">
                                    <?= ucfirst($e['type_abonnement']) ?>
                                </span>
                            </td>
                            <td class="py-3">
                                <?php if ($e['is_active']): ?>
                                    <span class="text-green-500"><i class="fas fa-circle text-xs mr-1"></i>Active</span>
                                <?php else: ?>
                                    <span class="text-red-500"><i class="fas fa-circle text-xs mr-1"></i>Inactive</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <!-- Alertes récentes -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100">
        <div class="p-6 border-b border-gray-100">
            <h3 class="text-lg font-semibold text-gray-800">
                <i class="fas fa-bell mr-2 text-red-600"></i>Alertes récentes
            </h3>
        </div>
        <div class="p-6">
            <?php if (empty($alertes)): ?>
                <p class="text-gray-500 text-center py-4">Aucune alerte</p>
            <?php else: ?>
                <div class="space-y-4">
                    <?php foreach (array_slice($alertes, 0, 5) as $alerte): ?>
                    <div class="flex items-start space-x-3 p-3 rounded-lg 
                        <?= $alerte['severite'] === 'critical' ? 'bg-red-50' : 
                           ($alerte['severite'] === 'warning' ? 'bg-yellow-50' : 'bg-blue-50') ?>">
                        <i class="fas fa-exclamation-triangle 
                            <?= $alerte['severite'] === 'critical' ? 'text-red-500' : 
                               ($alerte['severite'] === 'warning' ? 'text-yellow-500' : 'text-blue-500') ?>"></i>
                        <div>
                            <p class="text-sm font-medium text-gray-800"><?= htmlspecialchars($alerte['titre']) ?></p>
                            <p class="text-xs text-gray-500"><?= htmlspecialchars($alerte['entreprise_nom'] ?? 'Système') ?></p>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Super Admin - ' . APP_NAME;
$pageTitle = 'Tableau de bord Super Admin';
include VIEWS_PATH . '/layouts/main.php';
?>
