<?php include 'sesion.php'; ?>
<?php include 'concha.php'; ?>
<!DOCTYPE html>
<html lang="es">
<head><meta http-equiv="Content-Type" content="text/html; 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="http://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">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

    <style type="text/css">
        *{
            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;
        }

        .resultado-box{
            background: #fff;
            border: 1px solid #ddd;
            padding: 25px;
            margin-top: 20px;
            margin-bottom: 20px;
        }
    </style>
</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" style="text-align:center;">
        <h2>Guardar HTML directo</h2>
    </div>

    <div class="col-12 pt-3">

<?php
$lugar = isset($_POST['lugar']) ? trim($_POST['lugar']) : '';
$html_directo = isset($_POST['html_directo']) ? $_POST['html_directo'] : '';

if ($lugar === '') {
    echo '<div class="resultado-box alert alert-danger">No se recibió el lugar donde guardar el bloque.</div>';
} elseif ($html_directo === '') {
    echo '<div class="resultado-box alert alert-danger">No pegaste ningún código HTML.</div>';
} else {

    $bloque_guardar = "[html_directo]\n" . $html_directo;

    if (!isset($conn) || !($conn instanceof mysqli)) {
        echo '<div class="resultado-box alert alert-danger">Error: no hay conexión mysqli disponible en concha.php.</div>';
    } else {
        $stmt = $conn->prepare("UPDATE columna SET bloque = ? WHERE lugar = ? LIMIT 1");

        if ($stmt) {
            $stmt->bind_param("ss", $bloque_guardar, $lugar);

            if ($stmt->execute()) {
                if ($stmt->affected_rows >= 0) {
                    echo '<div class="resultado-box alert alert-success">';
                    echo '<strong>Perfecto.</strong> El HTML directo se guardó correctamente en el lugar <strong>' . htmlspecialchars($lugar, ENT_QUOTES, 'UTF-8') . '</strong>.';
                    echo '</div>';
                } else {
                    echo '<div class="resultado-box alert alert-danger">No se pudo actualizar el registro.</div>';
                }
            } else {
                echo '<div class="resultado-box alert alert-danger"><strong>Error al ejecutar:</strong> ' . htmlspecialchars($stmt->error, ENT_QUOTES, 'UTF-8') . '</div>';
            }

            $stmt->close();
        } else {
            echo '<div class="resultado-box alert alert-danger"><strong>Error al preparar la consulta:</strong> ' . htmlspecialchars($conn->error, ENT_QUOTES, 'UTF-8') . '</div>';
        }
    }
}
?>

    </div>

    <div class="row">
        <div class="col-12 text-center mb-3">
            <a class="btn btn-primary" href="columna.php">Volver a columnas</a>
        </div>
    </div>

</div>

<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>
</body>
</html>