<?php
declare(strict_types=1);

require_once 'sesion.php';
require_once '../url_amigable.php';
require_once 'concha.php';

/* =========================================================
   Compat PHP 8.2: evitar notices por variables no definidas
   ========================================================= */
$nombresitio = $nombresitio ?? '';
$estiloweb   = (int)($estiloweb ?? 0);
$asdasd      = $asdasd ?? '';

/* =========================================================
   Contadores (más rápido que traer todas las filas)
   ========================================================= */
function count_unread(mysqli $conn, string $table): int {
  // whitelist por seguridad (evita inyección por nombre de tabla)
  $allowed = ['chat', 'comentarios', 'fotosoyentes', 'avisos'];
  if (!in_array($table, $allowed, true)) return 0;

  $sql = "SELECT COUNT(*) AS c FROM `$table` WHERE `ver`='no'";
  $res = $conn->query($sql);
  $row = $res ? $res->fetch_assoc() : null;
  if ($res) $res->free_result();
  return (int)($row['c'] ?? 0);
}

$m_mensajes    = count_unread($conn, 'chat');
$m_comentarios = count_unread($conn, 'comentarios');
$m_fotos       = count_unread($conn, 'fotosoyentes');
$m_avisos      = count_unread($conn, 'avisos');
$m_tot         = $m_mensajes + $m_comentarios + $m_fotos + $m_avisos;

/* =========================================================
   GET (compat 8.2)
   ========================================================= */
$ID = isset($_GET['ID']) ? (int)$_GET['ID'] : 0;
$i  = $_GET['i'] ?? '';
$p  = $_GET['p'] ?? '';
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <meta name="description" content="Panel de Administracion">
  <meta name="author" content="<?php echo htmlspecialchars($nombresitio, ENT_QUOTES, 'UTF-8'); ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title><?php echo htmlspecialchars($nombresitio, ENT_QUOTES, 'UTF-8'); ?> - Panel de Administracion</title>

  <link href="https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900&display=swap" rel="stylesheet">
  <link rel="shortcut icon" href="" type="image/x-icon">
  <link rel="apple-touch-icon" href="">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <script src="https://code.jquery.com/jquery-3.4.1.min.js"
          integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

  <style>
    .carousel-cell { width: 66%; margin-right: 5px; }
    .display-flex { display:flex; justify-content:center; align-items:center; }
    *{ border-radius:0!important; }
    body { background:#e4e4e4; }

    .btn, a, h1, h2, h3, h4, h5, h6, b{ font-family:'Montserrat', sans-serif; }
    .btn, h1, h2, h3, h4, h5, h6 { font-weight:600; }

    .navbar { position:relative; min-height:50px; padding-bottom:20px; }

    .btn-primary {
      color:#fff!important;
      background-color:#0066FF;
      border-color:#0066FF;
    }

    .page-header{
      margin:0;
      padding:1em;
      background-color:#0066FF;
      border:0;
      text-align:center;
    }

    .page-header h2{
      color:#ffffff;
      font-weight:bold;
    }

    .bloqueAzul{ text-align:center; }
    .bloqueAzul .div{
      border:1px solid #e4e4e4;
      background-color:#fff;
    }

    ul{ list-style:none; padding:0; }
    .far{ color:#0066FF; }
    .faso{ color:#fff; }
    .colorAzul h3{ color:#0066FF; }
  </style>

  <script>
    function ventanaSecundaria(URL){
      window.open(URL,"ventana1","width=550,height=450,scrollbars=NO");
    }
  </script>
</head>

<body>
  <div class="container" style="background-color:#F1F2ED;">
    <?php
      if ($estiloweb === 9 || $estiloweb === 85 || $estiloweb === 86) {
        include 'menu_estilo9.php';
      } else {
        include 'menu.php';
      }
    ?>
  </div>

  <div role="main"
       class="container pt-3"
       style="background-color:#fff;min-height:80vh;<?php echo ($estiloweb === 9 || $estiloweb === 85 || $estiloweb === 86) ? 'margin-top:120px;' : ''; ?>">

    <div class="page-header" style="text-align:center;">
      <h2><?php echo htmlspecialchars($asdasd, ENT_QUOTES, 'UTF-8'); ?>Buscar noticias destacadas</h2>
    </div>

    <div class="col-12 my-3 text-center">

      <form id="form1" name="form1" method="GET" action="buscar_noticias.php">
        <div>
          <label>BUSCAR UNA NOTICIA</label>
          <input name="busqueda" type="text" class="form-control" id="busqueda" value="<?php echo isset($_GET['busqueda']) ? htmlspecialchars((string)$_GET['busqueda'], ENT_QUOTES, 'UTF-8') : ''; ?>">
          <button name="button" type="submit" class="btn btn-outline-primary" id="button">Buscar</button>
        </div>
      </form>

      <div class="col-12">
        <?php
          // NOMBRE DEL ARCHIVO PHP
          $pagina = "notas.php";
          // NOMBRE DE LA TABLA
          $tabla = "notas";
          // RESULTADOS POR PÁGINA (no se usa acá, lo dejo por compat)
          $c = 15;

          // IMPORTANTE: mostrar_notis.php probablemente depende de estos nombres de variables.
          $sql = "SELECT * FROM `$tabla` WHERE `destacado`='si' ORDER BY `ID`";
          $consulta = $conn->query($sql);

          while ($row = $consulta->fetch_assoc()) {
            $i_d = 0;

            $ID          = (int)($row["ID"] ?? 0);
            $titulo      = (string)($row["titulo"] ?? '');
            $fechapub    = (string)($row["fechapub"] ?? '');
            $carpetafoto = (string)($row["carpetafoto"] ?? '');
            $foto        = (string)($row["foto"] ?? '');
            $destacado   = (string)($row["destacado"] ?? '');
            $audio       = (string)($row["audio"] ?? '');

            $d           = (string)($row["d"] ?? '');
            $youtube     = (string)($row["youtube"] ?? '');
            $m           = (string)($row["m"] ?? '');
            $ver         = (string)($row["ver"] ?? '');
            $a           = (string)($row["a"] ?? '');
            $color       = (string)($row["color"] ?? '');
            $visitas     = (string)($row["visitas"] ?? '');
            $impresiones = (string)($row["impresiones"] ?? '');

            $fecha = trim($d.' - '.$m.' - '.$a);

            include 'mostrar_notis.php';
          }

          $consulta->free_result();
        ?>
      </div>

    </div>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
          integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
          integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

  <script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
  <script>
    window.setTimeout(function(){
      if (typeof $.fn.isotope === 'function' && $('.grid').length) {
        $('.grid').isotope({ itemSelector: '.grid-item' });
      }
    }, 3000);

    function ventanaSecundaria56(URL){
      window.open(URL,"ventana1","width=960,height=630,scrollbars=NO");
    }
  </script>

  <script defer src="https://use.fontawesome.com/releases/v5.8.1/js/all.js"
          integrity="sha384-g5uSoOSBd7KkhAMlnQILrecXvzst9TdC09/VM+pjDTCM+1il8RHz5fKANTFFb+gQ" crossorigin="anonymous"></script>
</body>
</html>