<?php
session_start();
include ('include/conexion.php');

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
            
  $email = @$_POST['email'];
  $contrasena = @$_POST['pass'];
  $consulta = $conexion->prepare("SELECT * FROM produc");
    $consulta->execute();
    
    $resultado = $consulta->get_result();

if ($resultado->num_rows > 0) {
    $fila1 = $resultado->fetch_assoc();
    $_SESSION['id'] = $fila1['id'];
    $_SESSION['producto'] = $fila1['producto'];
    $_SESSION['precio'] = $fila1['precio'];
    $_SESSION['descripcion'] = $fila1['descripcion'];
}

    $name = $_POST["nombre"];
    $correo = $_POST["email"];
    $contra = md5($_POST["contrasena"]);
    $fecha = $_POST["fecha"];
    $numero = $_POST["numero"];
    $tipo = "usuario";
    
    $consulta = $conexion->prepare("INSERT INTO Usuarios (nombre, email, contrasena, fecha, numero, tipo)  
    VALUES('$name', '$correo', '$contra', '$fecha', '$numero', '$tipo')");
    $consulta->execute();

    $consulta2 = $conexion->prepare("SELECT * FROM Usuarios WHERE nombre = '$name' AND contrasena = '$contra'");
    $consulta2->execute();
    $resultado1 = $consulta2->get_result();


    if ($resultado1->num_rows == TRUE) {
        $fila = $resultado1->fetch_assoc();
        session_start();
        $_SESSION['id'] = $fila['id'];
        $_SESSION['nombre'] = $fila['nombre'];
        $_SESSION['email'] = $fila['email'];
        $_SESSION['numero'] = $fila['numero'];
        $_SESSION['fecha'] = $fila['fecha'];
        // Redirigir después de la inserción
        header("Location: index.php");
        exit();
    }
    
 }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/validacion.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/css/uikit.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/js/uikit.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/js/uikit-icons.min.js"></script>
    <title>Registro</title>
</head>
<body>
    <?php include "include/img.php"?>
    <div class="uk-background-cover" style="height: 100vh;" data-src="<?php echo $path . $img['backs'][0]['backs']; ?>" uk-img>
    <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">
                <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-text">Contraseña:</label>
                    <div class="uk-form-controls">
                        <input class="uk-input" id="pass" name="contrasena" type="password" 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">Número:</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">
                    <label class="uk-form-label" for="form-stacked-select">¿Ya tiene una cuenta?</label>
                    <div class="">
                        <a href="login.php">Iniciar Sesion</a>
                    </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>
</div>
<script>
function validaciond() {
    let nombre = document.getElementById('nombre').value;
    let email = document.getElementById('email').value;
    let pass = document.getElementById('pass').value;
    let date = document.getElementById('date').value;
    let number = document.getElementById('number').value;
    
    if (nombre.length === 0) {
        alert('Nombre está vacío');
        return false;
    }
    if (email.length === 0) {
        alert('Correo Electrónico está vacío');
        return false;
    }
    if (pass.length < 6) {
        alert('La clave debe contener al menos 6 dígitos');
        return false;
    }
    if (date.length === 0) {
        alert('No ha elegido fecha');
        return false;
    }
    if (number.length !== 9) {
        alert('Su número teléfonico debe tener 9 dígitos');
        return false;
    }

    return true;
}

</script>
</body>
</html>