<?
$r='';
$title='INICIO DE SESSION - QUIMIFOREN';
include_once $r."inc/funciones.php";
$error=$_GET['error'];
validSession($r);
if(isset($_POST["entrar"])) {
  $mysqli = connectDB($r);
  $correo = strtolower($_POST["correo"]);
  $contrasena=sha1($_POST["contraseña"]);
  $recordarme=$_POST["recordarme"];
  $qry1 = $mysqli->query("SELECT * FROM z_usuarios WHERE correo='$correo'");
  $num1 = $qry1->num_rows;
  if($num1 > 0){
    $row1 = $qry1->fetch_assoc();
    $password=$row1["contraseña"];
    if($password==$contrasena){
      if($row1["estado"] == 0) {
          $_SESSION["userid"]	= $row1["id"];
           $_SESSION['nombre']	= $row1["nombre"];
          $_SESSION["perfil"]	= $row1["tipo"];
          
          //if($recordarme=='on'){}
          $ahora = date("Y-m-d H:i:s"); 
          $mysqli->query("UPDATE z_usuairos SET online = 1, ultima = '$ahora' WHERE id = '".$_SESSION["id"]."'");
          $mysqli->query("INSERT INTO z_accesos (iduser,correo, fecha) VALUE ('".$_SESSION["id"]."', '$correo' , '$ahora')");
          
          if($row1["cpass"] == 1) {exit(header("Location:".$r."cpass.php"));} 
          else{ 
                    if($row1["tipo"]!='2'){exit(header("Location:".$r."dashboard.php"));}
                    else{exit(header("Location:".$r."movil/index.php"));}
                };
      } else $error = "usuario esta desactivado";
    } else $error = "Contraseña no coincide con el del usuario";
  } else $error = "usuario no existe";
}
?>
<!doctype html>
<html class="no-js " lang="es">
<head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <meta name="description" content="QUIMIFOREN">

    <title><? echo $title ; ?></title>
    <!-- Favicon-->
    <link rel="icon" href="<? echo $r ; ?>favicon.ico" type="image/x-icon">
    <!-- Custom Css -->
    <link rel="stylesheet" href="<? echo $r ; ?>assets/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="<? echo $r ; ?>assets/css/main.css">
    <link rel="stylesheet" href="<? echo $r ; ?>assets/css/authentication.css">
    <link rel="stylesheet" href="<? echo $r ; ?>assets/css/color_skins.css">
</head>

<body class="theme-orange">
<div class="authentication">
    <div class="card">
        <div class="body">
            <div class="row">
                <div class="col-lg-12">
                    <div class="header slideDown">
                        <div class="logo"><img src="<? echo $r ; ?>assets/images/logo.png" alt="Nexa"></div>
                        <h1><?echo $name; ?></h1>
                        <ul class="list-unstyled l-social">
                            <li><a href="#"><i class="zmdi zmdi-facebook-box"></i></a></li>
                            <li><a href="#"><i class="zmdi zmdi-linkedin-box"></i></a></li>                            
                            <li><a href="#"><i class="zmdi zmdi-twitter"></i></a></li>
                        </ul>
                    </div>                        
                </div>
                <? if($error!=''){?><div class="col-lg-12"><div class="alert alert-danger"><strong>Ups! Perdón!</strong> <? echo $error.$contraseña; ?>.</div></div> <?}?>
                <form class="col-lg-12" action="index.php" id="sign_in" method="POST">
                    <h5 class="title">Iniciar sesión en su cuenta</h5>
                    <div class="form-group form-float">
                        <div class="form-line">
                            <input name="correo" type="text" class="form-control">
                            <label class="form-label">Usuario</label>
                        </div>
                    </div>
                    <div class="form-group form-float">
                        <div class="form-line">
                            <input type="password" class="form-control" name="contraseña">
                            <label class="form-label">Contraseña</label>
                        </div>
                    </div>
                    <div>
                        <input type="checkbox" name="recordarme">
                        <label for="recordarme">Recordarme</label>
                    </div> 
                    <div class="col-lg-12">
                        <input type="submit" name="entrar" class="btn btn-raised btn-primary waves-effect" value="INICIAR SESSION"/>
                    </div>
                </form>   
                <!--<div class="col-lg-12 m-t-20">
                    <a class="" href="forgot-password.html">Olvide mi Contraseña?</a>
                </div>   -->                 
            </div>
        </div>
    </div>
</div>

<!-- Jquery Core Js -->
<script src="<? echo $r ; ?>assets/bundles/libscripts.bundle.js"></script>    
<script src="<? echo $r ; ?>assets/bundles/vendorscripts.bundle.js"></script>
<script src="<? echo $r ; ?>assets/bundles/mainscripts.bundle.js"></script>
</body>
</html>