<?php
$cfg = @include __DIR__ . '/config.php';
session_start();
if (!empty($_POST['senha']) && hash_equals((string)($cfg['senha_admin']??''), (string)$_POST['senha'])) { $_SESSION['ok']=true; }
if (!empty($_GET['sair'])) { session_destroy(); header('Location: '.strtok($_SERVER['REQUEST_URI'],'?')); exit; }
if (empty($_SESSION['ok'])) {
  echo '<!DOCTYPE html><html><meta charset=utf-8><title>Painel Central</title><body style="font-family:system-ui;background:#0f172a;color:#fff;display:flex;align-items:center;justify-content:center;min-height:100vh"><div style="background:#1e293b;padding:2rem;border-radius:12px;max-width:380px;width:90%"><h1>Painel Central - Fire Sistemas</h1><form method=post><input type=password name=senha placeholder=Senha required style="width:100%;padding:.7rem;border-radius:6px;border:1px solid #475569;background:#0f172a;color:#fff;box-sizing:border-box"><button style="margin-top:.7rem;width:100%;padding:.7rem;background:#6366f1;color:#fff;border:none;border-radius:6px;font-weight:700">Entrar</button></form></div></body></html>';
  exit;
}
$cs=[]; foreach (glob(__DIR__.'/storage/*.json') ?: [] as $f) { $j=@json_decode((string)@file_get_contents($f),true); if (is_array($j)) $cs[]=$j; }
$ok=$fa=$at=0; foreach($cs as $c){ $e=strtotime($c['em']??'now'); if(($c['status']??'')==='falha') $fa++; elseif(time()-$e>172800) $at++; else $ok++; }
usort($cs, function($a,$b){ $r=function($c){ if(($c['status']??'')==='falha') return 1; $e=strtotime($c['em']??'now'); return time()-$e>172800?2:3; }; return $r($a)-$r($b); });
$mb = function($b){ if($b<1024) return $b.' B'; if($b<1048576) return round($b/1024).' KB'; if($b<1073741824) return round($b/1048576,1).' MB'; return round($b/1073741824,2).' GB'; };
$ago = function($e){ $d=time()-$e; if($d<60) return 'agora'; if($d<3600) return floor($d/60).' min atras'; if($d<86400) return floor($d/3600).'h atras'; return floor($d/86400).'d atras'; };
?>
<!DOCTYPE html><html><head><meta charset=utf-8><title>Painel Central - Fire Sistemas</title><meta http-equiv=refresh content=60>
<style>body{font-family:system-ui;background:#f8fafc;padding:1.5rem;margin:0}h1{font-size:1.5rem;margin:0 0 1rem}.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:.7rem;margin-bottom:1.5rem}.c{background:#fff;border-left:4px solid #94a3b8;border-radius:8px;padding:.9rem}.c.ok{border-color:#16a34a;background:#dcfce7}.c.fa{border-color:#dc2626;background:#fee2e2}.c.at{border-color:#f59e0b;background:#fef3c7}.c b{font-size:2rem;display:block}table{width:100%;border-collapse:collapse;background:#fff;border-radius:8px;overflow:hidden}th,td{padding:.65rem .85rem;text-align:left;border-top:1px solid #f1f5f9;font-size:.9rem}th{background:#f8fafc;font-size:.75rem;color:#475569;text-transform:uppercase}.t{display:inline-block;padding:.2rem .65rem;border-radius:12px;font-size:.72rem;font-weight:800}.t.ok{background:#dcfce7;color:#166534}.t.fa{background:#fee2e2;color:#991b1b}.t.at{background:#fef3c7;color:#854d0e}</style>
</head><body>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:1rem"><h1>Painel Central de Backups - Fire Sistemas</h1><a href="?sair=1" style="color:#6366f1">Sair</a></div>
<div class=cards>
<div class="c ok"><small>EM DIA</small><b><?=$ok?></b></div>
<div class="c fa"><small>FALHA</small><b><?=$fa?></b></div>
<div class="c at"><small>ATRASADO</small><b><?=$at?></b></div>
<div class=c><small>TOTAL</small><b><?=count($cs)?></b></div>
</div>
<table><thead><tr><th>Cliente</th><th>Status</th><th>Ultimo</th><th>Tamanho</th><th>Maquina</th><th>IP</th><th>Erro</th></tr></thead><tbody>
<?php if(empty($cs)): ?><tr><td colspan=7 style="text-align:center;padding:3rem;color:#94a3b8">Nenhum cliente reportou ainda.</td></tr><?php endif; ?>
<?php foreach($cs as $c): $e=strtotime($c['em']??'now'); if(($c['status']??'')==='falha'){$cls='fa';$lbl='FALHA';} elseif(time()-$e>172800){$cls='at';$lbl='ATRASADO';} else{$cls='ok';$lbl='OK';} ?>
<tr><td><b><?=htmlspecialchars($c['cliente']??'?')?></b></td><td><span class="t <?=$cls?>"><?=$lbl?></span></td><td><?=date('d/m/Y H:i',$e)?><br><small style="color:#94a3b8"><?=$ago($e)?></small></td><td><?=$mb((int)($c['tamanho']??0))?></td><td style="color:#475569"><?=htmlspecialchars($c['host_cliente']??'?')?></td><td style="color:#94a3b8;font-family:monospace;font-size:.75rem"><?=htmlspecialchars($c['ip_remoto']??'?')?></td><td style="color:#dc2626;font-size:.75rem"><?=htmlspecialchars(mb_strimwidth((string)($c['erro']??''),0,80,'...'))?></td></tr>
<?php endforeach; ?>
</tbody></table></body></html>
