<?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 KB_Usuarios usr
      WHERE (usr.Email = '$Email' OR usr.Usuario = '$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['IdUserKB'] = $row_login['Id'];

      if (isset($_SESSION['PrevUrl']) && true) {
        $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
      }
      header("Location: " . $MM_redirectLoginSuccess );
      echo '<script type="text/javascript">document.location.href="inicio/"</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]-->
  <style>
    .form-control::placeholder{
      color: #fff;
    }
  </style>
</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"
          style="
            /*background-image:none !important; background-color:#fff;*/
          "
        >
            <div
              class="login-box card p-login-out AltoScreen"
              style="
                float:right;
                margin-right: 10%;
                border: 0px;
              "
            >
            <div
              class="card-body p-login"
              style="
                box-shadow: 0px 0px 10px rgba(0,0,0,.50);
                /*background: #b71b1b !important;*/
                background-image: linear-gradient(-45deg, #20232f, #7780a5) !important;
              "
            >

              <!--<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"
                      style="
                        color: #fff;
                      "
                    >
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-xs-12">
                    <input
                      name="Pass"
                      class="form-control"
                      type="password"
                      required=""
                      placeholder="Contraseña"
                      style="
                        color: #fff;
                      "
                    >
                  </div>
                </div>

                <div class="form-group text-center m-t-20">
                  <div class="col-xs-12">
                    <button
                      class="btn btn-block btn-secondary btn-suc waves-effect waves-light"
                      style="
                        border: 1px solid #fff;
                        border-radius: 0px;
                      "
                      type="submit"
                    >
                      Ingresar
                    </button>
                  </div>
                </div>

                <!--<div class="form-group">
                  <div class="col-sm-12 text-center">
                    <a
                      href="javascript:void(0)"
                      id="to-recover"
                      class="text-dark pull-right"
                      style="color:#fff !important; width:100%"
                    >
                      ¿Olvidaste tu contraseña?
                    </a>
                  </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>
