<?php
declare(strict_types=1);

include 'sesion.php';
include 'concha.php';

/*
    Se espera que $conn sea mysqli desde concha.php
    Ejemplo:
    $conn = new mysqli($host, $usuario, $contra, $basedatos);
    $conn->set_charset('utf8mb4');
*/

if (!isset($conn) || !($conn instanceof mysqli)) {
    die('Error: conexión MySQL no disponible en $conn');
}

function limpiarTexto(?string $valor): string
{
    return trim((string)$valor);
}

function textoAEntidadesHtml(string $texto): string
{
    return mb_encode_numericentity(
        $texto,
        [0x80, 0x10FFFF, 0, 0xFFFFFF],
        'UTF-8'
    );
}

$ID = filter_input(INPUT_GET, 'ID', FILTER_VALIDATE_INT);
if (!$ID) {
    die('ID inválido');
}

$titulo = limpiarTexto($_POST['titulo'] ?? '');
$cat    = limpiarTexto($_POST['cat'] ?? '');
$bajada = limpiarTexto($_POST['bajada'] ?? '');

/*
    Convertimos a entidades HTML antes de guardar
    Ejemplo:
    á -> &#225;
    é -> &#233;
*/
$tituloGuardado = textoAEntidadesHtml($titulo);
$catGuardado    = textoAEntidadesHtml($cat);
$bajadaGuardada = textoAEntidadesHtml($bajada);

$sql = "UPDATE videos_home 
        SET titulo = ?, cat = ?, bajada = ?
        WHERE ID = ?";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die('Error al preparar la consulta: ' . $conn->error);
}

$stmt->bind_param('sssi', $tituloGuardado, $catGuardado, $bajadaGuardada, $ID);

if (!$stmt->execute()) {
    die('Error al actualizar: ' . $stmt->error);
}

$stmt->close();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="description" content="Panel de Administracion">
    <meta name="author" content="<?php echo htmlspecialchars((string)$nombresitio, ENT_QUOTES, 'UTF-8'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title><?php echo htmlspecialchars((string)$nombresitio, ENT_QUOTES, 'UTF-8'); ?> - Panel de Administracion</title>

    <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet" type="text/css">
    <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.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

    <style>
        *{
            border-radius:0 !important;
        }

        body{
            background-image:url(../fondeux.jpg);
        }

        .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:#0066FF;
        }

        .colorAzul h3{
            color:#0066FF;
        }

        textarea{
            width:100%;
            min-height:250px;
        }
    </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;max-width:960px;">
    <?php include 'menu.php'; ?>
</div>

<div style="background-color:#fff;max-width:960px;" class="container pt-3">
    <div class="page-header">
        <h2><?php echo htmlspecialchars((string)($idioma_100485 ?? ''), ENT_QUOTES, 'UTF-8'); ?></h2>
    </div>

    <div class="row my-3">
        <div class="col-12 text-center">
            <h3><?php echo htmlspecialchars((string)($idioma_100486 ?? ''), ENT_QUOTES, 'UTF-8'); ?></h3>
            <a class="btn btn-primary m-5" href="video_destacado_home.php">
                <?php echo htmlspecialchars((string)($idioma_100252 ?? ''), ENT_QUOTES, 'UTF-8'); ?>
            </a>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>

<script>
window.setInterval(function(){
    if (typeof $ !== 'undefined' && $('.grid').length) {
        $('.grid').isotope({
            itemSelector: '.grid-item'
        });
    }
}, 500);
</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>