<?php
	include("my_functions.php");
	
	$err= "";
	 
	
	//if(check_tor() > 0){ die(); }
	
	
	

	if(isset($_SESSION["connecteduserid"]))
	{
		
		$row = DB::queryFirstRow("SELECT is_loggedin FROM personnel_details WHERE id=%i LIMIT 1", $_SESSION["connecteduserid"]);
		
		if($row['is_loggedin'] == 1)
		{
?>
			<script>
				//window.location = "<?= $base_url?>dashboard.php";
					window.location = "<?= $base_url?>multiple.php";
				
			</script>
<?php
			exit();
		}
	}
	
	if (isset($dologin))
	{
		
		 
		//if ($_SERVER["REQUEST_METHOD"] == "POST" and check_tor() <  1 ) {
			if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
			/*  if( stristr($_POST['username'], '@')){ blocktor(getTorIP()); exit('Error 1');}
			if(!cleanurl()){ blocktor(getTorIP()); exit('Error 2'); exit();}
			
			*/
		
			
			
		if (trim($_POST['username'])=="")
			$err = $err."Username Required!<br>";
		if (trim($_POST['password'])=="")
			$err = $err."Password Required!<br>";
			
		if (trim($_POST['username'])!="" && trim($_POST['password'])!="")
		{
			 
			$pass = $_POST['password'];
			
			$useraccountdata = DB::queryFirstRow("SELECT personnel_details.id, password, personnel_details.display_name, personnel_details.status, personnel_details.role,
			personnel_details.branch, personnel_details.is_loggedin, personnel_details.is_locked FROM personnel_details WHERE personnel_details.login_name=%s  LIMIT 1", trim($_POST['username']) );
		 
			if(DB::count()==1)
			{
			 $check_pass = 0;
				 
				 if(password_verify($pass, $useraccountdata['password'])){
					 $check_pass = 1;
					 
					 
					 
					 
				 }
				 if(  $check_pass > 0)
				{
				
				if($useraccountdata['status'] == 1 and $check_pass > 0)
				{
					
					if($useraccountdata['is_locked'] == 0)
					{
						$_SESSION["connecteduserid"] =  $useraccountdata['id'];
						$_SESSION["connectedusergroup"] =  $useraccountdata['role'];
						$_SESSION["connecteduserbranch"] =  $useraccountdata['branch'];
						$_SESSION["connecteduserdisplayname"] =  $useraccountdata['display_name'];
						
						DB::query("UPDATE personnel_details SET is_loggedin=%i, last_login_date = current_timestamp() WHERE id=%i", 1, $useraccountdata['id']);
						
?>
						<script>
						//	window.location = "<?= $base_url?>dashboard.php"; 
					window.location = "<?= $base_url?>multiple.php";
						</script>
<?php						
						unset($_POST['username']); 
						unset($_POST['password']); 
						exit();	
					}
					else
					{
						$err = $err."You Account Is Locked. Contact System Admin!<br>";
					}
				}
				else
				{
					$err = $err."You Account Is Inactive. Contact System Admin!<br>";
				}
			}else
				{
					$err = $err."Invalid login . Contact System Admin!<br>";
				}
			}
			else
			{
				$err = $err."Invalid Credentials!<br>";
				$_POST['maxloginattempts'] += 1;
				
				if($_POST['maxloginattempts'] >= 3)
				{
					DB::update
					('personnel_details',
						array
						(
							'updated_date' => DB::sqleval("current_timestamp()"),
							'updated_by' => 0,
							'is_locked' => 1
						), 
						"login_name=%s", trim($_POST['username'])
					);
				}
			}
			
		  }
		}
	}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
      <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>KAMPANIS | KITUNZI POS </title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="<?php echo $base_url; ?>/mod/plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="<?php echo $base_url; ?>/mod/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo $base_url; ?>/mod/dist/css/adminlte.min.css">
  <!-- Google Font: Source Sans Pro -->
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
	<link rel="shortcut icon" href="<?php echo $base_url; ?>favicon.jpg">
   
  </head>
  
	
	
	<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="#"><b>PO</b>S</a>
  </div>
  <!-- /.login-logo -->
  <div class="card">
    <div class="card-body login-card-body">
      <p class="login-box-msg">Sign in to start your session</p>

      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<INPUT name=dologin type=hidden value=1>
			<INPUT type=hidden name=maxloginattempts id="maxloginattempts" value="<?php echo isset($_POST['maxloginattempts']) ? $_POST['maxloginattempts'] : 0 ?>">
			<div class="errorForm"></div>
			<?php
				if($err!="")
				{
			?>
					<div class="alert alert-danger">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						<?php 
							echo $err; 
						?>
					</div>
			<?php
				}
			?>
          <div class="form-group has-feedback">
            <input type="text" name="username" id="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : "" ?>" class="form-control" placeholder="Username"/>
            <span class="glyphicon glyphicon-user form-control-feedback"></span>
          </div>
          <div class="form-group has-feedback">
            <input type="password" name="password" id="password" value="<?php echo isset($_POST['password']) ? $_POST['password'] : "" ?>" class="form-control" placeholder="Password"/>
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
          </div>
          <div class="row">
            
              <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
 
          </div>
        </form>

 
      <!-- /.social-auth-links -->

 
    </div>
    <!-- /.login-card-body -->
  </div>
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script src="<?php echo $base_url; ?>/mod/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="<?php echo $base_url; ?>/mod/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="<?php echo $base_url; ?>/mod/dist/js/adminlte.min.js"></script>
    <script>
      
    </script>
  </body>
</html>