<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>
<div class="core-card p-4">
    <div class="d-flex flex-column flex-lg-row justify-content-between gap-3 mb-3">
        <div>
            <h2 class="h5 mb-1">Modules installés</h2>
            <p class="text-muted mb-0">Les modules ajoutés dans <code>app/Modules/</code> apparaîtront ici.</p>
        </div>
    </div>

    <?php if (empty($modules)): ?>
        <div class="border rounded p-4 text-center bg-light">
            <div class="fw-semibold mb-1">Aucun module installé</div>
            <div class="text-muted">ONE CORE est prêt. Ajoutez un module compatible pour l’activer depuis cette page.</div>
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                <tr>
                    <th>Module</th>
                    <th>Version</th>
                    <th>Statut</th>
                    <th class="text-end">Action</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($modules as $module): ?>
                    <tr>
                        <td>
                            <strong><?= esc($module['label']) ?></strong>
                            <div class="text-muted small"><?= esc($module['name']) ?></div>
                        </td>
                        <td><?= esc($module['version']) ?></td>
                        <td><?= (int) $module['enabled'] === 1 ? '<span class="badge text-bg-success">Actif</span>' : '<span class="badge text-bg-secondary">Inactif</span>' ?></td>
                        <td class="text-end">
                            <form method="post" action="<?= site_url('/modules/' . $module['name'] . '/toggle') ?>">
                                <?= csrf_field() ?>
                                <input type="hidden" name="enabled" value="<?= (int) $module['enabled'] === 1 ? '0' : '1' ?>">
                                <button class="btn btn-outline-primary btn-sm" type="submit">
                                    <?= (int) $module['enabled'] === 1 ? 'Désactiver' : 'Activer' ?>
                                </button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
<?= $this->endSection() ?>
