<?php
session_start();
$nombre = @$_POST['nombre'];
$email = @$_POST['email'];
$fecha = @$_POST['fecha'];
$numero = @$_POST['numero'];
$tipo = @$_POST['tipo'];
$name = $_SESSION['nombre'];
$id = $_SESSION['id'];

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

// Crear conexión
$conexion = new mysqli($servername, $username, $password, $database);

// Verificar conexión
if ($conexion->connect_error) {
    die("Error de conexión: " . $conexion->connect_error);
}
if(!empty($_GET["id"])){
    $id = $_GET["id"];
}

$query = "SELECT * FROM Usuarios WHERE id = '$id'";
$todo = $conexion->query($query);


    while ($total = $todo->fetch_assoc()) {
        $name = $total["nombre"];
    }


if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $nombre = $_POST["nombre"];
    $query = "UPDATE Usuarios SET nombre = '$nombre', email = '$email', fecha = '$fecha', numero = '$numero', tipo = '$tipo' WHERE id='$id'";
    $editar = $conexion->prepare($query);
    $editar->execute();
    header("Location: editar.php?id=".$id);
    exit(); 
}

?>

<!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-section">
        <div class="uk-container">
            <div class="uk-grid uk-child-width-1-2">
                <form method="post" onsubmit="return validaciond();" class="uk-form-stacked uk-align-center uk-margin-xlarge-top">
                    <h3 class="uk-text-center uk-margin-medium-bottom">Estas editando a <?php echo $name?></h3>
                    <div class="uk-margin">
                        <label class="uk-form-label" for="form-stacked-text">Nombre:</label>
                        <div class="uk-form-controls">
                            <input class="uk-input" id="nombre" name="nombre" type="text" placeholder="Some text...">
                        </div>
                    </div>
                    <div class="uk-margin">
                        <label class="uk-form-label" for="form-stacked-select">Email:</label>
                        <div class="uk-form-controls">
                            <input class="uk-input" id="email" name="email" type="email" placeholder="Some text...">
                        </div>
                    </div>
                    <div class="uk-margin">
                        <label class="uk-form-label" for="form-stacked-select">Fecha de nacimiento:</label>
                        <div class="uk-form-controls">
                            <input class="uk-input" id="date" name="fecha" type="date" placeholder="Some text...">
                        </div>
                    </div>
                    <div class="uk-margin">
                        <label class="uk-form-label" for="form-stacked-select">Numero:</label>
                        <div class="uk-form-controls">
                            <input class="uk-input" id="number" name="numero" type="number" placeholder="Some text...">
                        </div>
                    </div>
                    <div class="uk-margin">
                        <div class="uk-form-controls">
                            <button type="submit" class="uk-button uk-button-primary uk-light uk-align-center">Send Request</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<script>
    function validaciond() {
        let nombre = document.getElementById('nombre').value;
        let email = document.getElementById('email').value;
        let date = document.getElementById('date').value;
        let number = document.getElementById('number').value;

        if (nombre.length === 0) {
            alert('Nombre esta vacio');
            return false;
        }
        if (email.length === 0) {
            alert('Correo Electronico esta vacio');
            return false;
        }
        if (date.length === 0) {
            alert('No ha elegido fecha');
            return false;
        }
        if (number.length !== 9) {
            alert('Su numero telefonico debe tener 9 digitos');
            return false;
        }

        alert('Se edito al usuario correctamente');
        return true;
    }
</script>
</body>
</html>
