<?php include_once('config/db.php'); 
             
             $today = date("d.m.Y"); $today = date("Y-m-d", strtotime($today));      
             date_default_timezone_set('Africa/Nairobi');    $now = date("h:i:s "); 
        
              $uip =  htmlspecialchars( strtoupper($_SERVER['REMOTE_ADDR'])); 
              $ub= htmlspecialchars($_SERVER['HTTP_USER_AGENT'] );
      
      if (filter_has_var(INPUT_POST, "login")) 
       try{      
            $uLEmail =    htmlspecialchars ( strtoupper($_POST['loginEmail']));    
            $uLPassword = htmlspecialchars ( strtoupper($_POST['loginPassword']));
         
            $select = $conn->prepare("SELECT * FROM sys_user  WHERE  (email= :e OR username=:e ) and status=1   ");
            $select->execute(['e'=>$uLEmail]);  
            $data=$select->fetch(PDO::FETCH_ASSOC); 
           
            if ( $data and password_verify($uLPassword, $data['password']))
                      {
                          session_start();
                           $_SESSION['uid']= $data['id'];                    
                             
                           // successfull login
                            $sql = 'INSERT INTO sys_logs(user,auth,time,date,uip,ub ) VALUES(:u,:auth,:time,:date,:uip,:ub )';
                            $conn->prepare($sql)->execute(['u' => $data['id'],'auth' => 1,'time' => $now,'date' => $today,'uip'=>$uip ,'ub' =>$ub  ]); 
                               // update user and online
                                 $Q = " UPDATE sys_user  SET lastactivity = :lastactivity    WHERE id= :id  ";
                                 $uOnline = $conn ->prepare($Q);
                                 $uOnline ->execute( [ 'lastactivity'  => date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))), 'id'=>$_SESSION["uid"]  ]); 
                                 header("location:home");               
                      }
              else
                      {  
                        // Failed login
                        if ( $data['id'] ==''){ $myid=0; } else { $myid= $data['id']; }  
                          $sql = 'INSERT INTO sys_logs(user,auth,time,date,uip,ub ) VALUES(:u,:auth,:time,:date,:uip,:ub )';
                          $conn->prepare($sql)->execute(['u' => $myid,'auth' =>2,'time' => $now,'date' =>$today,'uip'=>$uip,'ub'=>$ub  ]); 
                          $Lerror='<div class="alert alert-danger alert-dismissable" style="margin-top:5px;"><i class="fa fa-key"></i><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><b> invalid User Name or Password</b></div>';  
                        } 
           
      } catch (Exception $e) { echo 'Message: ' .$e->getMessage();}
      
     
      
      ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title> FARM & Stay | Login  </title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Favicon ----->
  
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="../assets/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../assets/bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../assets/bower_components/Ionicons/css/ionicons.min.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="../assets/bower_components/jvectormap/jquery-jvectormap.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../assets/dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <!--<link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">-->
  <link rel="stylesheet" href="../assets/dist/css/skins/skin-blue.min.css">
  <!--<link rel="stylesheet" href="../assets/site/site.css">-->
  <link rel="stylesheet" href="../assets/site/register.css">
  <link rel="stylesheet" href="../assets/fonts/font.css">



</head>

<body    >


 
  
<div class="limiter" id="login">
        <div class="container-login100" style="background-image: linear-gradient(rgba(128,0,128,.9), rgba(128,0,128,.9)), 
                 url(../assets/site/images/image2.jpg) ">
            <div class="container">
                <div class="row">
                    <div class="col-md-6"></div>
                    <div class="col-md-5 col-md-offset-1">
                        <div class="login_topimg">
                        
                        </div>
                        <div class="wrap-login100">
                                 

                            <form class="login100-form " action="" method="POST"> 
                                   <h5 class="login100-form-title"> 
                                   <i class="fa fa-cubes fa-bed" style="color:green;"></i> FARM & STAY ADMIN
                                   </h5>
                                   <hr>
                                   
                                   <h5 class="login100-form-subtitle">Sign in to start your session </h5>
                                   <?php 
                                                    if(isset($_POST['login']))
                                                          {
                                                            echo "$Lerror " ; 
                                                          } else { } 
                                              ?> 
                                              
                                <div class="wrap-input100  m-b-16" >
                                    <label> Username or Email address </label>
                                    <input type="text" class="form-control" style="border-radius:15px;" name="loginEmail" placeholder="Email"  autocomplete="off" required="">
                                     </div>

                                <div class="wrap-input100  m-b-16" >
                                <label > Password </label>
                                <input type="password" class="form-control"  style="border-radius:15px;" name="loginPassword" placeholder="Password" required="" autocomplete="off">
                                     </div>

                                <div class="flex-sb-m w-full p-b-30">
                                    <div class="contact100-form-checkbox">                                        
                                        <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                                        <label class="label-checkbox100" for="ckb1">
                                            Remember me
                                        </label>
                                    </div>

                                    <div>
                                        <a href="#" class="txt1"> Forgot Password?         </a>
                                    </div>
                                </div>
                                   <div class="container-login100-form-btn p-t-25"> 
                                      <button type="submit"  name="login"class=" login100-form-btn btn btn-primary btn-flat"> Log In</button>
                                    <!-- <button class="login100-form-btn"> Login </button> -->
                                   </div>
                                   
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<!-- ./wrapper -->

<!-- jQuery 3 -->
<script src="../assets/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="../assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- AdminLTE App -->
<script src="../assets/dist/js/adminlte.min.js"></script>
<!-- Sparkline -->
<script src="../assets/bower_components/jquery-sparkline/dist/jquery.sparkline.min.js"></script>
<!-- jvectormap  -->
<script src="../assets/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../assets/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<!-- SlimScroll -->
<script src="../assets/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- AdminLTE for demo purposes -->
<!-- <script src="dist/js/demo.js"></script> -->

</body>
</html>