<?php
	session_start();
	include '../db/db.php';

	if (array_key_exists('login_admin', $_POST)) {
		/*echo $_POST['uname'];*/
		$user = htmlspecialchars(trim($_POST['uname']), ENT_QUOTES, 'UTF-8');
		$pass = htmlspecialchars(trim($_POST['pwd']), ENT_QUOTES, 'UTF-8');
		
		$uname = strip_tags($user);
		$pwd = strip_tags($pass);

		$upass = hash('sha256', $pwd);

		$sql = mysqli_query($conn, "SELECT * FROM details_of_users WHERE user='$uname' and val=1");	
		$row = mysqli_fetch_assoc($sql);
		
		if(mysqli_num_rows($sql) == 1) {
			
			if($row['pass']==$upass){
				//echo "OK";
				$response['status'] = 'ok'; // log in
				$_SESSION['user'] = $row['user'];
			
			} else{
				$response['status'] = 'error';
				 // wrong details
				$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.'; 
			}
		}
		
		else{
			$response['status'] = 'error';
			// wrong details
			$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.'; 
		}
		echo json_encode($response);
		//echo "In";
	}
?>