<?php
session_start();
@date_default_timezone_set('Africa/Casablanca');
require_once __DIR__.'/config/connexion.php';
require_once __DIR__.'/config/auth.php';
gc_require_login();

// Statistiques basiques
$stats = [
    'total_backups'=>0,
    'today_backups'=>0,
    'companies'=>0,
    'last_log'=>null,
    'last_backup_time'=>null
];

// Compagnies actives
try { $stats['companies'] = (int)$gc_pdo->query("SELECT COUNT(*) FROM compagnies_table WHERE active=1")->fetchColumn(); } catch(Throwable $e){ }

// Compter backups locaux
$base = dirname(__DIR__).'/backup_general';
if(is_dir($base)){
    foreach(scandir($base) as $d){ if($d==='.'||$d==='..') continue; if(!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/',$d)) continue; $dayPath=$base.'/'.$d; if(!is_dir($dayPath)) continue; foreach(glob($dayPath.'/*.zip') as $z){ $stats['total_backups']++; if($d===date('Y-m-d')) $stats['today_backups']++; $mtime=filemtime($z); if(!$stats['last_backup_time'] || $mtime>$stats['last_backup_time']) $stats['last_backup_time']=$mtime; } }
}

// Dernière ligne log
$logFile=__DIR__.'/backup_general.log';
if(is_file($logFile)){
    $lines=@file($logFile, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    if($lines){ $stats['last_log']=trim(end($lines)); }
}

?><!DOCTYPE html><html lang="fr"><head><meta charset="utf-8"><title>Dashboard Backups</title><link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css"><link rel="stylesheet" href="../vendor/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="../css/ruang-admin.min.css"><style>.stat-card{min-height:120px}</style></head><body id="page-top"><div id="wrapper"><?php include __DIR__.'/sidebar.php'; ?><div id="content-wrapper" class="d-flex flex-column"><div id="content"><?php include __DIR__.'/topbar.php'; ?><div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4"><h3 class="mb-0"><i class="fas fa-chart-line text-primary"></i> Tableau de Bord</h3><a href="backups.php" class="btn btn-sm btn-primary"><i class="fas fa-database"></i> Voir Backups</a></div>
    <div class="row">
        <div class="col-md-3 mb-3"><div class="card border-left-primary shadow h-100 py-2 stat-card"><div class="card-body"><div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Backups Total</div><div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $stats['total_backups']; ?></div></div></div></div>
        <div class="col-md-3 mb-3"><div class="card border-left-success shadow h-100 py-2 stat-card"><div class="card-body"><div class="text-xs font-weight-bold text-success text-uppercase mb-1">Backups Aujourd'hui</div><div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $stats['today_backups']; ?></div></div></div></div>
        <div class="col-md-3 mb-3"><div class="card border-left-info shadow h-100 py-2 stat-card"><div class="card-body"><div class="text-xs font-weight-bold text-info text-uppercase mb-1">Compagnies Actives</div><div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $stats['companies']; ?></div></div></div></div>
        <div class="col-md-3 mb-3"><div class="card border-left-warning shadow h-100 py-2 stat-card"><div class="card-body"><div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Dernier Backup</div><div class="h6 mb-0 text-gray-800"><?php echo $stats['last_backup_time']?date('Y-m-d H:i:s',$stats['last_backup_time']):'--'; ?></div></div></div></div>
    </div>
    <div class="card mb-4"><div class="card-header py-2"><strong>Dernier Log</strong></div><div class="card-body"><pre class="mb-0" style="white-space:pre-wrap;max-height:200px;overflow:auto;"><?php echo htmlspecialchars($stats['last_log'] ?? 'Aucun log'); ?></pre></div></div>
    <div class="card mb-4"><div class="card-header py-2 d-flex justify-content-between align-items-center"><strong>Actions Rapides</strong><button id="run-cron" class="btn btn-sm btn-success"><i class="fas fa-play"></i> Lancer Backup</button></div><div class="card-body"><p class="mb-2 text-muted small">Lance immédiatement le script global.</p><div id="cron-output" class="small text-monospace"></div></div></div>
    <p class="text-center text-muted small mb-4">&copy; <?php echo date('Y'); ?> Dashboard Backups</p>
</div></div><footer class="sticky-footer bg-white"><div class="container my-auto"><div class="text-center my-auto small">&copy; <?php echo date('Y'); ?> Backups</div></div></footer></div></div><script src="../vendor/jquery/jquery.min.js"></script><script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script><script src="../vendor/jquery-easing/jquery.easing.min.js"></script><script src="../js/ruang-admin.min.js"></script><script>const btn=document.getElementById('run-cron'); if(btn){ btn.addEventListener('click',()=>{ if(!confirm('Lancer le backup maintenant ?')) return; btn.disabled=true; const old=btn.innerHTML; btn.innerHTML='<i class="fas fa-spinner fa-spin"></i>'; fetch('backups.php',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'action=run_cron'}).then(r=>r.json()).then(js=>{ document.getElementById('cron-output').textContent=js.output || ''; btn.disabled=false; btn.innerHTML=old; }).catch(e=>{ alert('Erreur: '+e); btn.disabled=false; btn.innerHTML=old; }); }); }</script></body></html>
