<?php
header('Content-type: text/html; charset=UTF-8');
include("config.php");
$conexao = mysqli_connect($dbserver, $dbuser, $dbpass, $dbname);

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id <= 0) {
  die("ID inválido.");
}

$sql = "SELECT * FROM $dbtb WHERE id = $id LIMIT 1";
$resultado = mysqli_query($conexao, $sql);
$linha = mysqli_fetch_assoc($resultado);

if (!$linha) {
  die("Notícia não encontrada.");
}

$titulo = htmlspecialchars($linha["titulo"]);
$subtitulo = nl2br(mb_convert_encoding($linha["subtitulo"], 'UTF-8', 'ISO-8859-1'));
$texto = nl2br(mb_convert_encoding($linha["conteudo"], 'UTF-8', 'ISO-8859-1'));

$pag_ant = $id - 1;
$pag_prox = $id + 1;
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
  <meta charset="UTF-8">
  <title><?= htmlspecialchars($tituloshz) ?></title>
  <style>
    body {
      font-size: 14px;
      font-family: Verdana, Arial, Helvetica, sans-serif;
      background-color: <?= $colorbg ?>;
    }
    a {
      font-size: 14px;
      color: <?= $colortex ?>;
      text-decoration: none;
    }
    a:hover {
      color: <?= $colortexl ?>;
    }
    .content {
      max-width: 800px;
      margin: auto;
    }
    .navigation {
      display: flex;
      justify-content: space-between;
      margin: 20px 0;
    }
    .navigation a {
      font-weight: bold;
    }
  </style>
</head>
<body onload="setTimeout(() => window.location.reload(true), 900000)">
<div class="content">
  <img src="img/noticias.gif" alt="Notícia">
  <hr>
  <h2 style="color: <?= $Ccolortextit ?>;"><?= $titulo ?></h2>
  <p><?= $subtitulo ?></p>
  <div><?= $texto ?></div>
  <hr>
  <div class="navigation">
    <div>
      <?php
      $sql_ant = "SELECT id, titulo FROM $dbtb WHERE id = $pag_ant";
      $res_ant = mysqli_query($conexao, $sql_ant);
      if ($row_ant = mysqli_fetch_assoc($res_ant)) {
        echo '<a href="not.php?id=' . $row_ant['id'] . '">Anterior: ' . htmlspecialchars($row_ant['titulo']) . '</a>';
      }
      ?>
    </div>
    <div>
      <?php
      $sql_prox = "SELECT id, titulo FROM $dbtb WHERE id = $pag_prox";
      $res_prox = mysqli_query($conexao, $sql_prox);
      if ($row_prox = mysqli_fetch_assoc($res_prox)) {
        echo '<a href="not.php?id=' . $row_prox['id'] . '">Próximo: ' . htmlspecialchars($row_prox['titulo']) . '</a>';
      }
      ?>
    </div>
  </div>
  <div style="text-align: center; margin-top: 20px;">
    <a href="noticias.php"><b>Voltar</b></a>
  </div>
</div>
</body>
</html>