<?php
session_start();

$servername = "localhost";
$username = "itladevprubycom_enma";
$password = "53gNpj4t.xDc";
$database = "itladevprubycom_enma1";

$conexion = new mysqli($servername, $username, $password, $database);

if ($conexion->connect_error) {
    die("Error de conexión: " . $conexion->connect_error);
}

$query = $conexion->prepare("SELECT * FROM contactos");
$query->execute();
$result = $query->get_result();

$datos = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $datos[] = $row;
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Editar Registro</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/css/uikit.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit-icons.min.js"></script>
</head>
<body>
    <?php include "navbar.php"; ?>
    <div class="uk-container uk-margin-top uk-margin-bottom">
        <div class="uk-card uk-card-body">
            <h3 class="uk-card-title uk-text-center">Mensajes y opiniones:</h3>
            <div class="uk-grid uk-child-width-1-1@s uk-child-width-1-2@m uk-child-width-1-3@l uk-margin-large-top" uk-grid>
                <?php foreach($datos as $row): ?>
                    <div>
                        <div class="uk-card uk-card-default uk-card-body">
                            <h4 class="uk-card-title"><?php echo $row["nombre"]; ?></h4>
                            <p><?php echo $row["email"]; ?></p>
                            <p><?php echo $row["numero"]; ?></p>
                            <p><?php echo $row["asunto"]; ?></p>
                            <a class="uk-button uk-button-danger" onclick="confirmarEliminacion(<?php echo $row["id"]; ?>)">Eliminar mensaje</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <div class="uk-container uk-flex uk-flex-center">
        <a href="index.php" class="uk-button uk-button-default">Volver al inicio</a>
    </div>
    <script>
        function confirmarEliminacion(id) {
            if (confirm("¿Estás seguro de que quieres eliminar este mensaje?")) {
                window.location.href = "eliminar2.php?id=" + id;
            }
        }
    </script>
</body>
</html>
