<?php
session_start();
require_once("../conn/conn.php");
date_default_timezone_set("Asia/Bangkok");
$u_Username=$_POST['u_Username'];
$u_Password=$_POST['u_Password'];


//echo $u_Username.$u_Password;


$strSQL = "SELECT * FROM qc_user WHERE u_Username = '".$u_Username."'
and u_Password = '".$u_Password."'";
$objQuery = mysqli_query($con,$strSQL);
$objResult = mysqli_fetch_array($objQuery);

	if(!$objResult){
		?>
		<script language="JavaScript" type="text/javascript">
			<!--
			alert("Username Or Password Incorrect! \n ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง!");
			location.href='../index.php';
			//-->
			</script>
	<?php
	} else {
		if ($objResult["u_Status"] == "1") {
    ?>
		<script language="JavaScript" type="text/javascript">
			<!--
			alert("User : <?= $u_Username; ?>  กำลังเข้าใช้งานอยู่!");
			location.href='../index.php';
			//-->
			</script>
		<?php
		}else{
			if($objResult["u_Level"] == "1"){
				$sql_up = "UPDATE qc_user SET u_Status = '1' , u_LastUpdate = NOW() WHERE u_Username = '$u_Username'";
				$query_up = mysqli_query($con,$sql_up);
				
					
					$_SESSION['sess_name_user']=$objResult['u_Username'];
					$_SESSION['sess_u_Level']=$objResult['u_Level'];
					$_SESSION['u_Id']=$objResult['u_Id'];
//echo $_SESSION['sess_u_Level'];

					?>
						<script language="javascript" type="text/javascript">
						alert(".:Welcome Administrator");
						location="../admin/home.php"
						</script>
					<?php
			}elseif($objResult["u_Level"] == "2"){

			//echo 'user';
				$sql_up = "UPDATE qc_user SET u_Status = '1' , u_LastUpdate = NOW() WHERE u_Username = '$u_Username'";
				$query_up = mysqli_query($con,$sql_up);
				
					$_SESSION['sess_name_user']=$objResult['u_Username'];
					$_SESSION['sess_u_Level']=$objResult['u_Level'];
					$_SESSION['u_Id']=$objResult['u_Id'];
//echo $_SESSION['sess_u_Level'];

					?>
						<script language="javascript" type="text/javascript">
						alert(".:Welcome Administrator");
						location="../admin/home.php"
						</script>
					<?php
			}elseif($objResult["u_Level"] == "3"){

			//echo 'user';
				$sql_up = "UPDATE qc_user SET u_Status = '1' , u_LastUpdate = NOW() WHERE u_Username = '$u_Username'";
				$query_up = mysqli_query($con,$sql_up);
				
					//$_SESSION['sess_id_user']=$session_id();
					$_SESSION['sess_name_user']=$objResult['u_Username'];
					$_SESSION['sess_u_Level']=$objResult['u_Level'];
					$_SESSION['u_Id']=$objResult['u_Id'];
				
				//	echo $_SESSION['sess_u_Level'];

					?>
						<script language="javascript" type="text/javascript">
						alert(".:Welcome User");
						location="../user/home.php"
						</script>
					<?php
			}
			
		}
	}	

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>IT Inforittion | Sanko</title>
<body>
 
</body>
</html>
