<?php
  $Seccion    = 'Login';
  $Position   = '';

  require_once('config/config.php');

  // *** Validate request to login to this site.
  if (!isset($_SESSION)) {
    session_start();
  }

  $loginFormAction = $_SERVER['PHP_SELF'];
  if (isset($_GET['accesscheck'])) {
    $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  }

  if (isset($_POST['Email'])) {

    echo 'Login...';
    require_once('config/conexion.php');
    $Email = addslashes(strip_tags($_POST['Email']));
    $Pass = md5($_POST['Pass']);
    $MM_fldUserAuthorization = "";
    $MM_redirectLoginSuccess = "home/";
    $MM_redirectLoginFailed = "index.php";
    $MM_redirecttoReferrer = true;

    //Si El Usuario y Pass son correctos
    $Query = "
    SELECT
    usr.Id
    FROM Admins usr
    WHERE (usr.User = '$Email') AND usr.Pass = '$Pass'
    ";
    $result = mysqli_query($mysqli, $Query);
    $row_login = mysqli_fetch_assoc($result);
    $loginFoundUser = $row_login['Id'];

    //Cerrar conexion
    mysqli_close($conexion);

    if ($loginFoundUser) {
       $loginStrGroup = "";

  	//declaracion de variable de seccion
  	  $_SESSION['IdUser'] = $row_login['Id'];

      if (isset($_SESSION['PrevUrl']) && true) {
        $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
      }
      header("Location: " . $MM_redirectLoginSuccess );
      echo '<script type="text/javascript">document.location.href="home/"</script>';

    }else {
      echo "Fallo";
      header("Location: ". $MM_redirectLoginFailed );
      echo '<script type="text/javascript">document.location.href="index.php"</script>';
    }
  }

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="<?php echo $Position; ?>images/favicon.png">
    <link rel="apple-touch-icon" type="image/png" href="<?php echo $Position; ?>images/icon.png">
    <link rel="apple-touch-icon" type="image/png" sizes="152x152" href="<?php echo $Position; ?>images/icon.png"/>
    <link rel="apple-touch-icon" type="image/png" sizes="167x167" href="<?php echo $Position; ?>images/icon.png">
    <link rel="apple-touch-icon" type="image/png" sizes="180x180" href="<?php echo $Position; ?>images/icon.png">
    <link rel="icon" type="image/png" sizes="192x192" href="<?php echo $Position; ?>images/icon.png">
    <link rel="icon" type="image/png" sizes="128x128" href="<?php echo $Position; ?>images/icon.png">
    <title><?php echo $SiteName; ?></title>
    <!-- Bootstrap Core CSS -->
    <link href="<?php echo $Position; ?>assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="css/style.css" rel="stylesheet">
    <!-- You can change the theme colors from here -->
    <link href="css/colors/blue.css" id="theme" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <div class="preloader">
        <svg class="circular" viewBox="25 25 50 50">
            <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
    </div>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <section id="wrapper">
        <div class="login-register">
            <div class="login-box card p-login-out AltoScreen">
            <div class="card-body p-login">

              <!--<div class="LoginBottom row">
                <div class="col-sm-5">
                  <a href="<?php echo $Position; ?>avisodeprivacidad">Aviso de Privadidad</a>
                </div>
                <div class="col-sm-7 text-right">
                  ########
                </div>
              </div>-->

              <form class="form-horizontal form-material" method="post">
                <div class="text-center">
                  <img src="images/logo1.png" class="mar-b-40" style="max-width: 200px;"/>
                </div>
                <div class="form-group ">
                  <div class="col-xs-12">
                    <input name="Email" class="form-control" type="text" required="" placeholder="Usuario">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-xs-12">
                    <input name="Pass" class="form-control" type="password" required="" placeholder="Contraseña">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-md-12">
                    <!--<a href="javascript:void(0)" id="to-recover" class="text-dark pull-right">
                      <i class="fa fa-lock m-r-5"></i>¿Olvidaste tu contraseña?
                    </a>-->
                  </div>
                </div>
                <div class="form-group text-center m-t-20">
                    <div class="col-xs-12">
                      <button class="btn btn-block btn-success btn-suc waves-effect waves-light" type="submit">Ingresar</button>
                      <!--<div class="m-t-40">
                        <a href="registro">Crear cuenta</a>
                        |
                        <a href="#">Olvidé mi contraseña</a>
                      </div>-->
                    </div>
                  </div>
              </form>
              <form class="form-horizontal" id="recoverform" action="index.html">
                <div class="form-group ">
                  <div class="col-xs-12">
                    <h3>Recuperar Contraseña</h3>
                    <p class="text-muted">Ingresa tu correo electrónico para reestablecer tu contraseña. </p>
                  </div>
                </div>
                <div class="form-group ">
                  <div class="col-xs-12">
                    <input class="form-control" type="text" required="" placeholder="Email">
                  </div>
                </div>
                  <div class="form-group text-center m-t-20">
                    <div class="col-xs-12">
                      <button class="btn btn-primary btn-lg btn-block text-uppercase waves-effect waves-light" type="submit">Enviar</button>
                    </div>
                  </div>
              </form>

            </div>
          </div>
        </div>

    </section>
    <!-- ============================================================== -->
    <!-- End Wrapper -->
    <!-- ============================================================== -->
    <!-- ============================================================== -->
    <!-- All Jquery -->
    <!-- ============================================================== -->
    <script src="<?php echo $Position; ?>assets/plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="<?php echo $Position; ?>assets/plugins/bootstrap/js/popper.min.js"></script>
    <script src="<?php echo $Position; ?>assets/plugins/bootstrap/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="<?php echo $Position; ?>js/jquery.slimscroll.js"></script>
    <!--Wave Effects -->
    <script src="<?php echo $Position; ?>js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="<?php echo $Position; ?>js/sidebarmenu.js"></script>
    <!--stickey kit -->
    <script src="<?php echo $Position; ?>assets/plugins/sticky-kit-master/dist/sticky-kit.min.js"></script>
    <script src="<?php echo $Position; ?>assets/plugins/sparkline/jquery.sparkline.min.js"></script>
    <!--Custom JavaScript -->
    <script src="<?php echo $Position; ?>js/custom.min.js"></script>
    <!-- ============================================================== -->
    <!-- Style switcher -->
    <!-- ============================================================== -->
    <script src="<?php echo $Position; ?>assets/plugins/styleswitcher/jQuery.style.switcher.js"></script>
    <script>
      $(document).ready(function() {

        AltoScreen      = $( window ).height();
        $(".AltoScreen").css("height", AltoScreen);

        $( window ).resize(function() {
          AltoScreen      = $( window ).height();
          $(".AltoScreen").css("height", AltoScreen);
        });

      });
    </script>
</body>

</html>
