<?php
$formatBytes = static function (int $bytes): string {
    if ($bytes >= 1073741824) return number_format($bytes/1073741824,2,',',' ').' Go';
    if ($bytes >= 1048576) return number_format($bytes/1048576,2,',',' ').' Mo';
    if ($bytes >= 1024) return number_format($bytes/1024,2,',',' ').' Ko';
    return $bytes.' o';
};
$totalFiles = array_sum(array_column($groups, 'file_count'));
$totalSize = array_sum(array_column($groups, 'size'));
?>
<div class="d-flex justify-content-between align-items-start mb-4">
  <div><h1 class="darlink-title"><i class="bi bi-broom me-2"></i>Nettoyage</h1><p class="text-muted">Vider uniquement le cache et les fichiers temporaires autorisés.</p></div>
  <a class="btn btn-outline-secondary" href="index.php?page=maintenance"><i class="bi bi-arrow-left"></i> Retour</a>
</div>
<div class="alert alert-info"><strong><?= (int)$totalFiles ?> fichier(s)</strong> — espace récupérable estimé : <strong><?= $formatBytes((int)$totalSize) ?></strong>. Les sauvegardes, documents et pièces jointes ne sont jamais supprimés.</div>
<form method="post" action="index.php?page=maintenance_cleanup_run" onsubmit="return confirm('Confirmer le nettoyage des catégories sélectionnées ?');">
<div class="card shadow-sm border-0"><div class="card-body p-0"><div class="table-responsive"><table class="table align-middle mb-0">
<thead><tr><th class="ps-4">Sélection</th><th>Catégorie</th><th>Dossier</th><th>Fichiers</th><th>Taille</th><th>État</th></tr></thead><tbody>
<?php foreach($groups as $group): ?>
<tr><td class="ps-4"><input class="form-check-input" type="checkbox" name="targets[]" value="<?= htmlspecialchars($group['key']) ?>" <?= $group['file_count']>0?'checked':'' ?>></td>
<td><strong><?= htmlspecialchars($group['label']) ?></strong><?php if($group['oldest_at']): ?><div class="small text-muted">Plus ancien : <?= htmlspecialchars($group['oldest_at']) ?></div><?php endif; ?></td>
<td><code><?= htmlspecialchars($group['path']) ?></code></td><td><?= (int)$group['file_count'] ?></td><td><?= $formatBytes((int)$group['size']) ?></td>
<td><?php if(!$group['exists']): ?><span class="badge bg-secondary">Absent</span><?php elseif(!$group['writable']): ?><span class="badge bg-warning text-dark">Lecture seule</span><?php else: ?><span class="badge bg-success-subtle text-success border border-success-subtle">Prêt</span><?php endif; ?></td></tr>
<?php endforeach; ?>
</tbody></table></div></div></div>
<div class="d-flex justify-content-end align-items-center gap-3 mt-3"><label class="form-label mb-0">Tapez <strong>NETTOYER</strong></label><input class="form-control" style="max-width:180px" name="confirmation" required><button class="btn btn-danger"><i class="bi bi-trash3 me-1"></i> Nettoyer</button></div>
</form>
