<?php $formatBytes=static fn(int $b):string=>$b>=1048576?number_format($b/1048576,2,',',' ').' Mo':($b>=1024?number_format($b/1024,2,',',' ').' Ko':$b.' o'); ?>
<div class="d-flex justify-content-between align-items-start mb-4"><div><h1 class="darlink-title"><i class="bi bi-journal-text me-2"></i>Journaux</h1><p class="text-muted">Consultation sécurisée des 500 dernières lignes.</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="row g-4"><div class="col-lg-4"><div class="card shadow-sm border-0"><div class="card-header fw-semibold">Fichiers disponibles</div><div class="list-group list-group-flush">
<?php if(!$logs): ?><div class="p-4 text-muted">Aucun journal trouvé.</div><?php endif; ?>
<?php foreach($logs as $log): ?><a class="list-group-item list-group-item-action <?= $selectedId===$log['id']?'active':'' ?>" href="index.php?page=maintenance_logs&log=<?= urlencode($log['id']) ?>"><div class="fw-semibold"><?= htmlspecialchars($log['name']) ?></div><small><?= htmlspecialchars($log['modified_at']) ?> — <?= $formatBytes((int)$log['size']) ?></small></a><?php endforeach; ?>
</div></div></div><div class="col-lg-8">
<?php if($selectedLog): ?>
<div class="card shadow-sm border-0"><div class="card-header d-flex justify-content-between align-items-center"><strong><?= htmlspecialchars($selectedLog['name']) ?></strong><div class="d-flex gap-2"><a class="btn btn-sm btn-outline-primary" href="index.php?page=maintenance_logs_download&log=<?= urlencode($selectedId) ?>"><i class="bi bi-download"></i> Télécharger</a><button class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#clearLogModal"><i class="bi bi-eraser"></i> Vider</button></div></div>
<div class="card-body"><form class="row g-2 mb-3" method="get"><input type="hidden" name="page" value="maintenance_logs"><input type="hidden" name="log" value="<?= htmlspecialchars($selectedId) ?>"><div class="col-md-7"><input class="form-control" name="search" value="<?= htmlspecialchars((string)($_GET['search']??'')) ?>" placeholder="Rechercher dans le journal"></div><div class="col-md-3"><select class="form-select" name="level"><option value="">Tous niveaux</option><?php foreach(['ERROR','WARNING','INFO'] as $l): ?><option value="<?= $l ?>" <?= (($_GET['level']??'')===$l)?'selected':'' ?>><?= $l ?></option><?php endforeach; ?></select></div><div class="col-md-2"><button class="btn btn-primary w-100">Filtrer</button></div></form>
<div style="background:#0f172a;color:#e2e8f0;border-radius:10px;padding:16px;max-height:600px;overflow:auto;font-family:Consolas,monospace;font-size:.86rem;white-space:pre-wrap"><?php foreach($selectedLog['lines'] as $line): ?><div class="mb-1"><?= htmlspecialchars($line) ?></div><?php endforeach; ?></div></div></div>
<div class="modal fade" id="clearLogModal" tabindex="-1"><div class="modal-dialog"><form class="modal-content" method="post" action="index.php?page=maintenance_logs_clear"><div class="modal-header"><h5 class="modal-title">Vider le journal</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><p>Cette action conserve le fichier mais efface son contenu.</p><input type="hidden" name="log" value="<?= htmlspecialchars($selectedId) ?>"><label class="form-label">Tapez <strong>VIDER</strong></label><input class="form-control" name="confirmation" required></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button><button class="btn btn-danger">Vider</button></div></form></div></div>
<?php else: ?><div class="card shadow-sm border-0"><div class="card-body text-center py-5 text-muted"><i class="bi bi-journal-text display-4"></i><p class="mt-3">Sélectionnez un journal.</p></div></div><?php endif; ?>
</div></div>
