<?php
session_start();
$servername = "localhost";
$username = "itladevprubycom_enma";
$password = "53gNpj4t.xDc";
$database = "itladevprubycom_enma1";

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

$nombre = @$_POST["nombre"];
$email = @$_POST["email"];
$contra = @$_POST["contrasena"];
$fecha = @$_POST["fecha"];
$numero = @$_POST["numero"];
$tipo = @$_POST["tipo"];

if(isset($nombre)){
    $agregar = $conexion->prepare("INSERT INTO Usuarios (nombre, email, contrasena, fecha, numero, tipo)  
    VALUES('$nombre', '$email', '$contra', '$fecha', '$numero', '$tipo')");
    $agregar->execute();
    header("location: index.php");
}
?><!DOCTYPE html>
<html lang="en">
<head>
    <?php include "navbar.php";?>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>agregar</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>
    <div class="uk-container uk-flex uk-flex-center uk-margin-large-top">
    <div class="uk-card uk-card-body uk-width-2-3">
        <h2 class="">Agregar Usuarios:</h2>
        <form action="" onsubmit="return validaciond();" class="uk-dark" method="post">
            <div class="uk-margin">
                <input class="uk-input uk-form-medium" id="nombre" type="text" placeholder="Nombre" name="nombre">
            </div>
            <div class="uk-margin">
                <input class="uk-input uk-form-medium" id="email" type="email" placeholder="Email" name="email">
            </div>
            <div class="uk-margin">
                <input class="uk-input uk-form-medium" id="pass" type="password" placeholder="Contraseña" name="contrasena">
            </div>
            <div class="uk-margin">
                <input class="uk-input uk-form-medium" id="date" type="date" placeholder="Fecha" name="fecha">
            </div>
            <div class="uk-margin">
                <input class="uk-input uk-form-medium" id="number" type="number" placeholder="Número" name="numero">
            </div>
            <div class="uk-margin">
                <div class="uk-form-controls">
                    <select class="uk-select" name="tipo" id="form-stacked-select">
                        <option>admin</option>
                        <option>usuario</option>
                    </select>
                </div>
            </div>
            <div class="uk-margin uk-flex uk-flex-center">
                <button class="uk-button uk-button-primary" type="submit">Submit</button>
            </div>
        </form>
        <a href="index.php" class="uk-button uk-button-default uk-margin-small-top">Volver al inicio</a>
    </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;
    }

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