<?php
include 'sesion.php';
include 'concha.php';

$ID  = isset($_GET['ID']) ? (int)$_GET['ID'] : 0;
$cat = isset($_GET['cat']) ? (string)$_GET['cat'] : '';
$c   = isset($_GET['c']) ? (int)$_GET['c'] : 0;
if ($c < 0) $c = 0;

if (!isset($asdas)) $asdas = '';

/* =========================
   FIX UTF (mojibake tipo: atenciÃ³n)
   ========================= */
function fix_mojibake($s) {
    if ($s === null) return '';
    $s = (string)$s;

    // Si aparece mojibake típico, revertimos
    if (strpos($s, 'Ã') !== false || strpos($s, 'Â') !== false || strpos($s, 'â') !== false) {
        $d = utf8_decode($s);

        // Si queda válido como UTF-8, lo usamos; si no, devolvemos original
        if (preg_match('//u', $d)) return $d;
    }
    return $s;
}

/* =========================
   CONEXIÓN ALMACÉN (conn23)
   ========================= */
$usuario23   = "almanoti_usuarios_rn";
$contra23    = "T96QG821J7ao";
$host23      = "localhost";
$basedatos23 = "almanoti_base";

$conn23 = new mysqli($host23, $usuario23, $contra23, $basedatos23);
if ($conn23->connect_error) {
    die("<pre style='background:#fee;padding:10px;border:1px solid #f99'>ERROR CONEXION almanoti_base: " . htmlspecialchars($conn23->connect_error) . "</pre>");
}

// Recomendado en 8.2
$conn23->set_charset('utf8mb4');
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="description" content="Panel de Administracion">
<meta name="author" content="<?php echo $nombresitio; ?>">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title><?php echo $nombresitio; ?> - 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">
<link rel="stylesheet" href="">
<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-image: url(../fondeux.jpg);
    background-position:top center;
    background-repeat: repeat-x;
    background-color:#EBEBEB;
    height:100%;
}
.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; }
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 include 'menu.php'; ?>
</div>

<div role="main" style="background-color:#fff;" class="container pt-3">

  <div class="page-header mb-3" style="text-align:center;">
    <h2><?php echo $asdas; ?>Almacén de noticias</h2>
    <h4 class="text-capitalize text-light"><?php echo htmlspecialchars(fix_mojibake($cat), ENT_QUOTES, 'UTF-8'); ?></h4>
  </div>

  <div class="col-12 p-0">
    <?php
    $catEsc = $conn23->real_escape_string($cat);
    $cInt   = (int)$c;

    $sql = "SELECT * FROM `noticias`
            WHERE `cat` LIKE '$catEsc'
            ORDER BY ID DESC
            LIMIT $cInt, 1";

    $consulta = $conn23->query($sql);

    if (!$consulta) {
        echo '<div class="alert alert-danger">Error SQL: ' . htmlspecialchars($conn23->error, ENT_QUOTES, 'UTF-8') . '</div>';
    } else {

        if ($consulta->num_rows == 0) {
            echo '<div class="alert alert-warning text-center">No hay noticias para esta categoría (o llegaste al final).</div>';
        }

        while ($row = $consulta->fetch_assoc()) {
            $ID     = (int)$row["ID"];
            $titulo = fix_mojibake($row["titulo"]);
            $catRow = (string)$row["cat"];
            $img    = (string)$row["img"];
            $bajada = fix_mojibake($row["bajada"]);
            $fecha  = fix_mojibake($row["fecha"]);
            ?>

            <div class="col-12 mb-3 border">

              <div class="col-12 d-flex justify-content-between p-2">
                <h5 class="Estilo46"><?php echo htmlspecialchars($fecha, ENT_QUOTES, 'UTF-8'); ?></h5>
              </div>

              <div class="container-fluid">
                <div class="row">
                  <div class="col-12 col-sm-4 p-0">
                    <?php
                    $external_link = $img;
                    if ($external_link !== '' && @getimagesize($external_link)) { ?>
                      <img src="<?php echo htmlspecialchars($img, ENT_QUOTES, 'UTF-8'); ?>" class="img-fluid" />
                    <?php } ?>
                  </div>

                  <div class="col-12 col-sm-8 ">
                    <div>
                      <h4><?php echo htmlspecialchars($titulo, ENT_QUOTES, 'UTF-8'); ?></h4>
                    </div>

                    <div>
                      <p><?php echo $bajada; ?></p>
                    </div>
                  </div>
                </div>
              </div>

              <ul class="m-0 p-3 d-flex justify-content-center align-items-center">
                <li>
                  <a class="btn btn-outline-primary" href="ver_nota_almacen.php?ID=<?php echo (int)$ID; ?>">VER NOTICIA COMPLETA</a>
                </li>
                <li>
                  <a class="btn btn-primary" href="cargar_noticia_almacen.php?ID=<?php echo (int)$ID; ?>">Añadir al sitio</a>
                </li>
              </ul>

            </div>

            <?php
            $cat = $catRow ?: $cat;
        }

        $consulta->free();
    }
    ?>
  </div>

  <div class="col-12 text-center my-3 p-3">
    <?php $nextC = $c + 1; ?>
    <a class="btn btn-primary" href="ver_almacen.php?cat=<?php echo rawurlencode($cat); ?>&c=<?php echo (int)$nextC; ?>">SIGUIENTE ></a>
  </div>

  <div class="col-12 text-center my-3 p-3">
    <a class="btn btn-primary" href="almacen.php">VOLVER</a>
  </div>

</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" 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>
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>
