<?php ob_start(); ?>

<div class="max-w-4xl">
    <div class="mb-6">
        <a href="<?= BASE_URL ?>/super-admin/entreprises" class="text-indigo-600 hover:text-indigo-800">
            <i class="fas fa-arrow-left mr-2"></i>Retour aux entreprises
        </a>
    </div>
    
    <div class="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><?= htmlspecialchars($entreprise['nom']) ?>
            </h3>
            <div class="flex items-center space-x-2">
                <span class="text-sm text-gray-500">Token widget:</span>
                <code class="text-xs bg-gray-100 px-2 py-1 rounded"><?= $entreprise['widget_token'] ?></code>
            </div>
        </div>
        
        <form action="<?= BASE_URL ?>/super-admin/entreprises/<?= $entreprise['id'] ?>/update" method="POST" class="p-6">
            <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Nom de l'entreprise *</label>
                    <input type="text" name="nom" required value="<?= htmlspecialchars($entreprise['nom']) ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Secteur d'activité</label>
                    <input type="text" name="secteur" value="<?= htmlspecialchars($entreprise['secteur'] ?? '') ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Pays</label>
                    <input type="text" name="pays" value="<?= htmlspecialchars($entreprise['pays'] ?? 'Mali') ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Région</label>
                    <input type="text" name="region" value="<?= htmlspecialchars($entreprise['region'] ?? '') ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                    <input type="email" name="email" value="<?= htmlspecialchars($entreprise['email'] ?? '') ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Téléphone</label>
                    <input type="text" name="telephone" value="<?= htmlspecialchars($entreprise['telephone'] ?? '') ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-2">Adresse</label>
                    <textarea name="adresse" rows="2"
                              class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent"><?= htmlspecialchars($entreprise['adresse'] ?? '') ?></textarea>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Type d'abonnement</label>
                    <select name="type_abonnement" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                        <?php foreach ($abonnements as $key => $label): ?>
                            <option value="<?= $key ?>" <?= $entreprise['type_abonnement'] === $key ? 'selected' : '' ?>><?= $label ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Nombre max d'agents</label>
                    <input type="number" name="max_agents" value="<?= $entreprise['max_agents'] ?>" min="1" max="100"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent">
                </div>
                
                <?php $languesActives = json_decode($entreprise['langues_actives'] ?? '["fr"]', true); ?>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Langues activées</label>
                    <div class="space-y-2">
                        <?php foreach ($langues as $code => $nom): ?>
                        <label class="flex items-center">
                            <input type="checkbox" name="langues[]" value="<?= $code ?>" 
                                   <?= in_array($code, $languesActives) ? 'checked' : '' ?>
                                   class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                            <span class="ml-2 text-gray-700"><?= $nom ?></span>
                        </label>
                        <?php endforeach; ?>
                    </div>
                </div>
                
                <?php $canauxActifs = json_decode($entreprise['canaux_actifs'] ?? '["web"]', true); ?>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Canaux activés</label>
                    <div class="space-y-2">
                        <?php foreach ($canaux as $code => $nom): ?>
                        <label class="flex items-center">
                            <input type="checkbox" name="canaux[]" value="<?= $code ?>"
                                   <?= in_array($code, $canauxActifs) ? 'checked' : '' ?>
                                   class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                            <span class="ml-2 text-gray-700"><?= $nom ?></span>
                        </label>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
            
            <div class="mt-8 flex justify-end space-x-4">
                <a href="<?= BASE_URL ?>/super-admin/entreprises" class="px-6 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                    Annuler
                </a>
                <button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition flex items-center">
                    <i class="fas fa-save mr-2"></i>Mettre à jour
                </button>
            </div>
        </form>
    </div>
    
    <!-- Administrateurs -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 mt-6">
        <div class="p-6 border-b border-gray-100">
            <h3 class="text-lg font-semibold text-gray-800">
                <i class="fas fa-user-shield mr-2 text-indigo-600"></i>Administrateurs
            </h3>
        </div>
        <div class="p-6">
            <?php if (empty($admins)): ?>
                <p class="text-gray-500">Aucun administrateur configuré</p>
            <?php else: ?>
                <div class="space-y-3">
                    <?php foreach ($admins as $admin): ?>
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div class="flex items-center">
                            <div class="w-10 h-10 bg-indigo-100 rounded-full flex items-center justify-center mr-3">
                                <i class="fas fa-user text-indigo-600"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-800"><?= htmlspecialchars($admin['prenom'] . ' ' . $admin['nom']) ?></p>
                                <p class="text-sm text-gray-500"><?= htmlspecialchars($admin['email']) ?></p>
                            </div>
                        </div>
                        <span class="text-xs px-2 py-1 rounded-full <?= $admin['is_active'] ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' ?>">
                            <?= $admin['is_active'] ? 'Actif' : 'Inactif' ?>
                        </span>
                    </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Modifier ' . htmlspecialchars($entreprise['nom']) . ' - ' . APP_NAME;
$pageTitle = 'Modifier l\'entreprise';
include VIEWS_PATH . '/layouts/main.php';
?>
