<?php
declare(strict_types=1);
require_once __DIR__.'/auth.php';require_once __DIR__.'/conexao.php';require_once __DIR__.'/inventario_mobile_helpers.php';date_default_timezone_set('America/Sao_Paulo');
if(function_exists('tiPermExigir'))tiPermExigir($mysqli,'inventario_mobile','visualizar');$mysqli->set_charset('utf8mb4');$csrf=mobCsrf();$flash=mobGetFlash();
if($_SERVER['REQUEST_METHOD']==='POST'){try{mobCheckCsrf();$eventId=(int)($_POST['evento_id']??0);$action=mobText($_POST['acao']??'',30);$note=mobNullable($_POST['observacao']??'',1000);$user=mobUsuario();if($action==='reconhecer'){$stmt=$mysqli->prepare("UPDATE ti_mobile_eventos SET status_revisao='RECONHECIDO',reconhecido_por=?,reconhecido_em=NOW(),observacao_revisao=? WHERE id=? AND resolvido=0");$stmt->bind_param('ssi',$user,$note,$eventId);}elseif($action==='resolver'){$stmt=$mysqli->prepare("UPDATE ti_mobile_eventos SET status_revisao='RESOLVIDO',resolvido=1,resolvido_por=?,resolvido_em=NOW(),observacao_revisao=? WHERE id=?");$stmt->bind_param('ssi',$user,$note,$eventId);}else throw new RuntimeException('Ação inválida.');$stmt->execute();$stmt->close();mobAudit($mysqli,'ALERTA_'.strtoupper($action),null,'Evento #'.$eventId);mobFlash('ok','Alerta atualizado.');mobRedirect('inventario_mobile_alertas.php');}catch(Throwable $e){mobFlash('err',$e->getMessage());mobRedirect('inventario_mobile_alertas.php');}}
$cfg=mobConfig($mysqli);$silent=max(1,(int)($cfg['silent_hours']??24));$offlineH=max($silent,(int)($cfg['offline_alert_hours']??168));$events=[];$r=$mysqli->query("SELECT e.*,d.agent_id,d.patrimonio,d.fabricante,d.modelo,d.responsavel_nome,d.setor FROM ti_mobile_eventos e JOIN ti_mobile_dispositivos d ON d.id=e.dispositivo_id WHERE e.resolvido=0 ORDER BY FIELD(e.severidade,'CRITICO','ATENCAO','INFO'),e.id DESC LIMIT 300");while($r&&$x=$r->fetch_assoc())$events[]=$x;$offline=[];$r=$mysqli->query("SELECT * FROM ti_mobile_dispositivos WHERE status='ATIVO' AND (ultima_comunicacao IS NULL OR ultima_comunicacao<DATE_SUB(NOW(),INTERVAL {$silent} HOUR)) ORDER BY ultima_comunicacao ASC LIMIT 200");while($r&&$x=$r->fetch_assoc())$offline[]=$x;
?>
<!doctype html><html lang="pt-BR"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Alertas Android</title><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet"><link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet"><link href="inventario_mobile_style.css?v=2" rel="stylesheet"></head><body><div class="topline"></div><div class="gridbg"></div><div class="app"><header class="header"><div class="header-inner"><div class="brand"><div class="logo"><img src="assets/logo_pref.jpg" alt="Prefeitura"></div><div><span class="eyebrow"><i class="bi bi-bell-fill"></i>Inventário Android</span><h1 class="title">Alertas e mudanças</h1><p class="subtitle">Reconhecimento, resolução e observações de auditoria dos alertas técnicos.</p></div></div><nav class="nav"><a class="primary" href="inventario_equipamentos.php">Voltar</a></nav></div></header><main class="main"><?php if($flash):?><div class="flash <?=$flash['tipo']==='ok'?'ok':'err'?>"><?=mobE($flash['msg'])?></div><?php endif;?><div class="two-col"><section class="panel"><div class="panel-head"><div class="panel-title"><span class="picon"><i class="bi bi-exclamation-triangle-fill"></i></span><div><h3>Eventos abertos</h3><p><?=count($events)?> alerta(s).</p></div></div></div><div class="timeline"><?php foreach($events as $event):?><article class="event"><a class="event-icon" href="inventario_dispositivo.php?id=<?=(int)$event['dispositivo_id']?>"><i class="bi bi-exclamation-triangle-fill"></i></a><div><strong><?=mobE($event['titulo'])?> • <?=mobE($event['status_revisao']??'ABERTO')?></strong><p><?=mobE(($event['agent_id']?:'Android').' • Pat. '.($event['patrimonio']?:'N/I').' • '.($event['setor']?:'Sem setor'))?></p><form method="post"><input type="hidden" name="csrf_token" value="<?=mobE($csrf)?>"><input type="hidden" name="evento_id" value="<?=(int)$event['id']?>"><input class="control" name="observacao" placeholder="Observação"><button class="mini" name="acao" value="reconhecer">Reconhecer</button><button class="mini primary" name="acao" value="resolver">Resolver</button><a class="mini" href="inventario_dispositivo.php?id=<?=(int)$event['dispositivo_id']?>">Abrir dispositivo</a></form></div><time><?=mobE(mobData($event['criado_em']))?></time></article><?php endforeach;?><?php if(!$events):?><div class="empty"><i class="bi bi-check-circle-fill"></i><strong>Sem alertas abertos</strong></div><?php endif;?></div></section><section class="panel"><div class="panel-head"><div class="panel-title"><span class="picon"><i class="bi bi-wifi-off"></i></span><div><h3>Sem comunicação +<?=$silent?>h</h3><p><?=count($offline)?> dispositivo(s); offline prolongado após <?=$offlineH?>h.</p></div></div></div><div class="timeline"><?php foreach($offline as $device):?><a class="event" href="inventario_dispositivo.php?id=<?=(int)$device['id']?>"><span class="event-icon"><i class="bi bi-wifi-off"></i></span><div><strong><?=mobE(($device['fabricante']?:'Android').' '.$device['modelo'])?></strong><p>Pat. <?=mobE($device['patrimonio']?:'N/I')?> • última: <?=mobE(mobData($device['ultima_comunicacao']))?></p></div><time><?=mobE(mobAgo($device['ultima_comunicacao']))?></time></a><?php endforeach;?></div></section></div></main></div></body></html>
