<?php ob_start(); ?>

<div class="mb-6 flex justify-between items-center">
    <div>
        <p class="text-gray-600">Gérez toutes les entreprises clientes de la plateforme</p>
    </div>
    <a href="<?= BASE_URL ?>/super-admin/entreprises/create" class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition flex items-center">
        <i class="fas fa-plus mr-2"></i>Nouvelle entreprise
    </a>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100">
    <div class="p-6">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="text-left text-sm text-gray-500 border-b border-gray-200">
                        <th class="pb-4 font-medium">Entreprise</th>
                        <th class="pb-4 font-medium">Secteur</th>
                        <th class="pb-4 font-medium">Abonnement</th>
                        <th class="pb-4 font-medium">Agents</th>
                        <th class="pb-4 font-medium">Conversations</th>
                        <th class="pb-4 font-medium">Statut</th>
                        <th class="pb-4 font-medium">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entreprises as $e): ?>
                    <tr class="border-b border-gray-100 hover:bg-gray-50">
                        <td class="py-4">
                            <div class="flex items-center">
                                <div class="w-10 h-10 bg-indigo-100 rounded-lg flex items-center justify-center mr-3">
                                    <i class="fas fa-building text-indigo-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-800"><?= htmlspecialchars($e['nom']) ?></p>
                                    <p class="text-xs text-gray-500"><?= htmlspecialchars($e['email'] ?? '-') ?></p>
                                </div>
                            </div>
                        </td>
                        <td class="py-4 text-gray-600"><?= htmlspecialchars($e['secteur'] ?? '-') ?></td>
                        <td class="py-4">
                            <span class="px-3 py-1 text-xs rounded-full font-medium
                                <?= $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-4 text-gray-600">
                            <i class="fas fa-users text-gray-400 mr-1"></i><?= $e['nb_agents'] ?? 0 ?> / <?= $e['max_agents'] ?>
                        </td>
                        <td class="py-4 text-gray-600">
                            <i class="fas fa-comments text-gray-400 mr-1"></i><?= number_format($e['nb_conversations'] ?? 0) ?>
                        </td>
                        <td class="py-4">
                            <?php if ($e['is_active']): ?>
                                <span class="inline-flex items-center text-green-600">
                                    <span class="w-2 h-2 bg-green-500 rounded-full mr-2"></span>Active
                                </span>
                            <?php else: ?>
                                <span class="inline-flex items-center text-red-600">
                                    <span class="w-2 h-2 bg-red-500 rounded-full mr-2"></span>Inactive
                                </span>
                            <?php endif; ?>
                        </td>
                        <td class="py-4">
                            <div class="flex items-center space-x-2">
                                <a href="<?= BASE_URL ?>/super-admin/entreprises/<?= $e['id'] ?>" 
                                   class="p-2 text-indigo-600 hover:bg-indigo-50 rounded-lg" title="Modifier">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <form action="<?= BASE_URL ?>/super-admin/entreprises/<?= $e['id'] ?>/toggle" method="POST" class="inline">
                                    <button type="submit" class="p-2 <?= $e['is_active'] ? 'text-red-600 hover:bg-red-50' : 'text-green-600 hover:bg-green-50' ?> rounded-lg"
                                            title="<?= $e['is_active'] ? 'Désactiver' : 'Activer' ?>">
                                        <i class="fas <?= $e['is_active'] ? 'fa-ban' : 'fa-check' ?>"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        
        <?php if (empty($entreprises)): ?>
        <div class="text-center py-12">
            <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                <i class="fas fa-building text-2xl text-gray-400"></i>
            </div>
            <p class="text-gray-500">Aucune entreprise pour le moment</p>
            <a href="<?= BASE_URL ?>/super-admin/entreprises/create" class="inline-flex items-center mt-4 text-indigo-600 hover:text-indigo-800">
                <i class="fas fa-plus mr-2"></i>Créer la première entreprise
            </a>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Entreprises - ' . APP_NAME;
$pageTitle = 'Gestion des Entreprises';
include VIEWS_PATH . '/layouts/main.php';
?>
