<html lang="en">
<head>
	<title>Login e-IMS</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
	<link rel="icon" type="image/png" href="Login/images/icons/favicon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/vendor/animate/animate.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="Login/vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/vendor/select2/select2.min.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="Login/vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="Login/css/util.css">
	<link rel="stylesheet" type="text/css" href="Login/css/main.css">
<!--===============================================================================================-->


</head>
<body>    
	<div class="limiter">
		<div class="container-login100">
			<div class="wrap-login100 p-l-85 p-r-85 p-t-55 p-b-55">
				<form class="login100-form validate-form flex-sb flex-w" method="post" action="login.php">
					<span class="login100-form-title p-b-32">
						Account Login
					</span>

					<span class="txt1 p-b-11">
						Username
					</span>
					<div class="wrap-input100 validate-input m-b-36" data-validate = "Username is required">
						<input class="input100" type="text" name="username" >
						<span class="focus-input100"></span>
					</div>
					
					<span class="txt1 p-b-11">
						Password
					</span>
					<div class="wrap-input100 validate-input m-b-12" data-validate = "Password is required">
						<span class="btn-show-pass">
							<i class="fa fa-eye"></i>
						</span>
						<input class="input100" type="password" name="pass" >                                                
						<span class="focus-input100"></span>
					</div>
					                                       
					<div class="container-login100-form-btn">
						<button class="login100-form-btn" name="login_click">
							Login
						</button>
					</div>                                                   
<?php
if(isset($_POST['login_click'])) { 
include 'db_connection.php';
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

session_start(); // Start session nya
$username = $_POST['username']; // Ambil value username yang dikirim dari form
$password = $_POST['pass']; // Ambil value password yang dikirim dari form

// menyeleksi data dengan username dan password yang sesuai
$data = mysqli_query($conn,"select * from user where user_id='$username' and user_pwd=sha1('$password') and status in ('A','O','I')");
// menghitung jumlah data yang ditemukan
$cek = mysqli_num_rows($data);
//get data from mysqli_query
$row = mysqli_fetch_assoc($data);
$cek_sts = $row['status'];
$cek_uid = $row['user_id'];
$level = $row['usr_level'];
$cek_activity = $row['last_activity'];
$conv_activity = new DateTime(Date($cek_activity));
$str_activity = $conv_activity->format("Y-m-d H:i:s");

//set datetime Jakarta Timezone
$timezone = "Asia/Jakarta";
date_default_timezone_set($timezone);  
$datetime = new DateTime(Date('Y-m-d H:i:s'));
$str_datetime = $datetime->format("Y-m-d H:i:s");

$d1 = new DateTime(date("Y-m-d H:i:s"));
$d2 = new DateTime($str_activity);
$interval = $d1->diff($d2);
$d3 = $interval->h."-".$interval->i."-".$interval->s;
//$out = $d1->format("Y-m-d H:i:s")." : ".$d2->format("Y-m-d H:i:s")." : ".$d3;
$str_datetime = $d1->format("Y-m-d H:i:s");//." : ".$d2->format("Y-m-d H:i:s")." : ".$d3;
//echo "<script type='text/javascript'>alert('{$out}');</script>";

$tahun = $interval->y;
$bulan = $interval->m;
$hari = $interval->d;
$jam = $interval->h;
$menit = $interval->i;
$detik = $interval->s;

//echo "<script type='text/javascript'>alert('today: {$out}');</script>";
//echo "<script type='text/javascript'>alert('activity: {$str_activity}');</script>";
//echo "<script type='text/javascript'>alert('tahun: {$tahun}');</script>";
//echo "<script type='text/javascript'>alert('bulan: {$bulan}');</script>";
//echo "<script type='text/javascript'>alert('hari: {$int_hari}');</script>";
//echo "<script type='text/javascript'>alert('jam: {$jam}');</script>";
//echo "<script type='text/javascript'>alert('menit: {$menit}');</script>";

//echo "<script type='text/javascript'>alert('cek: {$cek}');</script>";
//echo "<script type='text/javascript'>alert('uid: {$str_datetime}');</script>";

if($cek > 0 && $cek_sts=='A'){          
        $_SESSION['username']=$username;
        $_SESSION['level']=$level;                
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>"; 
}elseif ($cek > 0 && $cek_sts=='O' && $tahun > 0) {
        $_SESSION['username']=$username; 
        $_SESSION['level']=$level;  
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>";
}elseif ($cek > 0 && $cek_sts=='O' && $bulan > 0) {
        $_SESSION['username']=$username; 
        $_SESSION['level']=$level;  
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>";
}elseif ($cek > 0 && $cek_sts=='O' && $hari > 0) {
        $_SESSION['username']=$username; 
        $_SESSION['level']=$level;  
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>";
}elseif ($cek > 0 && $cek_sts=='O' && $jam > 0) {
        $_SESSION['username']=$username; 
        $_SESSION['level']=$level;  
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>";
}elseif ($cek > 0 && $cek_sts=='O' && $menit > 30) {
        $_SESSION['username']=$username; 
        $_SESSION['level']=$level; 
        $sql  = ("UPDATE user SET status = 'O', last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");        
        $query = mysqli_query($conn, $sql);        
	echo "<script>  window.location.href='index.php';</script>";
}elseif ($cek > 0 && $cek_sts=='O') {
    echo "<align='center'><font color=red>User Already Online</font>";
}elseif ($cek > 0 && $cek_sts=='I') {
    echo "<align='center'><font color=red>User Has Been Blocked</font>";
}elseif ($cek == 0) {
    echo "<align='center'><font color=red>Incorrect UserName or Password</font>"; 
}else {
    
}}

?>
				</form>
			</div>
		</div>
	</div>
	

	<div id="dropDownSelect1"></div>
	
<!--===============================================================================================-->
	<script src="Login/vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="Login/vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="Login/vendor/bootstrap/js/popper.js"></script>
	<script src="Login/vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="Login/vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="Login/vendor/daterangepicker/moment.min.js"></script>
	<script src="Login/vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="Login/vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="Login/js/main.js"></script>
        
</body>
</html>