<?php
	session_start(); 
	include('../conn/connect.php');
	if($_POST['txt_user']==""||$_POST['txt_pass']==""){
		echo '
			<script type="text/javascript">
				alert("กรุณากรอกชื่อผู้ใช้หรือรหัสผ่าน");
				window.history.back();
			</script>';
	}else{
		$useradminSQL = "SELECT * FROM user_admin WHERE ID_user_admin ='".strtolower($_POST['txt_user'])."' and status_admin = 'Y' ";
		$useradminQuery = mysqli_query($conn,$useradminSQL);
		$useradminresult = mysqli_fetch_array($useradminQuery);
		if(mysqli_num_rows($useradminQuery) > 0){
			//******************ผ่านemail**********************************

			//******************จากsql************************************
			$userSQL = "SELECT * FROM user_pass WHERE ID_user_pass ='".strtolower($_POST['txt_user'])."' and password = '".md5(sha1(md5($_POST['txt_pass'])))."' ";
			$userQuery = mysqli_query($conn,$userSQL);
			$user_result = mysqli_fetch_array($userQuery);
			if($user_result['status']=="Y"){
				$_SESSION['name_user']=$user_result['ID_user_pass'];
				$_SESSION['Group_user']=$useradminresult['Group_user'];
				if($_POST['link_work']=="admin"&&$_SESSION['Group_user']=="admin"){
					header('Location: '.$IP_SERVER.'admin/');
				}else if($_POST['link_work']=="work"){
					header('Location: '.$IP_SERVER.'work_request_list/');
				}else{
					header('Location: '.$IP_SERVER.'no_url/');
				}
			}else{
				echo '
					<script type="text/javascript">
						alert("ชื่อผู้ใช้ไม่ถูกต้อง");
						window.history.back();
				</script>';
			}
			//******************************************************
		}else{
			echo '
			<script type="text/javascript">
				alert("ชื่อผู้ใช้ไม่ถูกต้อง");
				window.history.back();
			</script>';
		}
		/*
		*/
	}
?>