<?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'];
}

  $consulta = $conexion->prepare("SELECT * FROM Usuarios WHERE email = ? AND contrasena = ?");
  $consulta->bind_param("ss", $email, $contrasena);
  $consulta->execute();
  

  $resultado = $consulta->get_result();

  if ($resultado->num_rows > 0) {
      $fila = $resultado->fetch_assoc();
      session_start();
      $_SESSION['id'] = $fila['id'];
      $_SESSION['nombre'] = $fila['nombre'];
      $_SESSION['email'] = $fila['email'];
      $_SESSION['numero'] = $fila['numero'];
      $_SESSION['fecha'] = $fila['fecha'];

      if ($fila['tipo'] == 'admin') {
        header("Location: admin/index.php");
      } else {
        header("Location: index.php");
      }
      exit();
  } else {
      echo "Usuario o contraseña incorrectos";
  }
  $consulta->close();
}
$conexion->close();
?>

<!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">
    <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>Iniciar Sesion</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" action="" 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">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">Contraseña:</label>
                    <div class="uk-form-controls">
                        <input class="uk-input" id="password" name="pass" type="password" placeholder="Some text...">
                    </div>
                </div>   
                <div class="uk-margin">
                    <label class="uk-form-label" for="form-stacked-select">¿No tienes cuenta?</label>
                    <div class="">
                        <a href="registro.php">Registrarse</a>
                    </div>
                </div>
                <div class="uk-margin">
                    <div class="uk-form-controls">
                        <button tpye="submit" class="uk-button uk-liht uk-align-center">Send Request</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</div>

<script>
function validaciond() {
    let email = document.getElementById('email').value;
    if (email.length === 0) {
        alert('No has escrito nada en el Correo Electronico');
        return false;
    }
    let clave = document.getElementById('password').value;
    if (clave.length < 6) {
        alert('La clave debe contener al menos 6 digitos');
        return false;
    }
    return true;
}

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