<?php
// Step 1: Force error reporting AT THE VERY TOP.
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

// echo "DEBUG: Script start.<br>"; // DEBUG 1

// Step 2: Include connection file
require_once('../Connections/EVENTS.php');

// echo "DEBUG: Connection file included.<br>"; // DEBUG 2

$error = '';

// Step 3: Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // echo "DEBUG: POST method detected.<br>"; // DEBUG 3

    // Step 4: Get form data (PHP 5.4 compatible)
    $nom = '';
    if (isset($_POST['nom_poblacion'])) {
        $nom = trim($_POST['nom_poblacion']);
    }
    
    // echo "DEBUG: Poblacion name: " . htmlspecialchars($nom) . "<br>"; // DEBUG 4

    if ($nom === '') {
        $error = 'Debe introducir el nombre de la población.';
        // echo "DEBUG: Error - nombre vacio.<br>"; // DEBUG 5
    } else {
        // echo "DEBUG: Preparing statement.<br>"; // DEBUG 6
        $stmt = $EVENTS->prepare("INSERT INTO poblaciones (nom_poblacion) VALUES (?)");

        if ($stmt === false) {
            // If prepare fails, this is a critical SQL error.
            // echo "DEBUG: Prepare failed.<br>"; // DEBUG 7
            die('<strong>Error en la consulta SQL:</strong> ' . htmlspecialchars($EVENTS->error));
        }

        // echo "DEBUG: Binding params.<br>"; // DEBUG 8
        $stmt->bind_param('s', $nom);
        
        // echo "DEBUG: Executing statement.<br>"; // DEBUG 9
        if ($stmt->execute()) {
            // echo "DEBUG: Execute successful. Redirecting...<br>"; // DEBUG 10
            header('Location: index.php');
            exit;

        } else {
            // echo "DEBUG: Execute failed.<br>"; // DEBUG 11
            $error = 'Error al insertar población: ' . htmlspecialchars($stmt->error);
        }
    }
}

// For GET requests or if POST fails and sets an error
// echo "DEBUG: Script end, preparing to render HTML.<br>"; // DEBUG 12

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insertar Población</title>

 <link rel="stylesheet" type="text/css" href="css/default.css" />
 
	<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.7.2.custom.css" />
    
     <link rel="stylesheet" type="text/css" href="css/miscss.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

</head>
<body>
    <h1>Insertar Población</h1>
    <?php if ($error): ?>
        <p class="error"><?php echo $error; ?></p>
    <?php endif; ?>
    <form method="post">
        <label>Nombre de la población: 
            <input type="text" name="nom_poblacion" value="<?php echo htmlspecialchars(isset($_POST['nom_poblacion']) ? $_POST['nom_poblacion'] : ''); ?>" />
        </label>
        <button type="submit">Insertar población</button>
    </form>
    <p><a href="index.php">Volver al inicio</a></p>
</body>
</html>