<?php
require ("../connection.php");
$ip=$_SERVER['REMOTE_ADDR'];
date_default_timezone_set("Asia/Kolkata");
session_start();
	if (isset($_SESSION['user'])) {
	//header('Location:../login/lockscreen.php'); 
	}
		$user_name=$_REQUEST['username'];
		$p_w=$_REQUEST['password'];
		$password=md5($_REQUEST['password']);
	
		$usermaster=mysqli_query($con,"select u.user_id,u.full_name,u.user_name,u.password,rm.code,b.branch_id, case when bm.branch_name is null then 'MAPPING PENDING' else bm.branch_name end as branch ,rr.ho_admin_access as ha from user_master u left join branch_mapping b on u.user_id=b.user_id left join branch_master bm on bm.id=b.division_id left join role_mapping rm on rm.user_id=u.user_id left join role_master rr on rr.code=rm.code where b.to_date is null and rm.to_date is null and u.user_name='$user_name' and u.status='0'");
	
////echo "select u.user_id,u.full_name,u.user_name,u.password,rm.code,b.branch_id,lm.location, case when bm.branch_name is null then 'MAPPING PENDING' else bm.branch_name end as branch ,rr.ho_admin_access as ha from user_master u left join branch_mapping b on u.user_id=b.user_id left join branch_master bm on bm.id=b.division_id left join role_mapping rm on rm.user_id=u.user_id left join role_master rr on rr.code=rm.code join location_master lm on b.branch_id=lm.id where b.to_date is null and rm.to_date is null and u.user_name='$user_name' and u.status='0'";
			$ucount=mysqli_num_rows($usermaster);

		if($ucount!=0)
		{
			$user=mysqli_fetch_array($usermaster);
			$pwd=$user['password'];
			$uid=$user['user_id'];
			$branch_id=$user['branch_id'];
					$ha=$user['ha'];
			if($pwd==$password)
			{
				$_SESSION['user'] = $user['user_id'];
				$_SESSION['branch_id'] = $user['branch_id'];
				$_SESSION['code'] = $user['code'];
				$_SESSION['full_name'] = $user['full_name'];
				$_SESSION['ha'] = $user['ha'];
				$_SESSION['location'] = $user['location'];
				$_SESSION['division_name'] = $user['branch'];
				$_SESSION['user_name']=$user_name;
				$_SESSION['start'] = time(); // Taking now logged in time.
					// Ending a session in 30 minutes from the starting time.
				$_SESSION['expire'] = $_SESSION['start'] + (60*30);
					
			    $datetime = date("Y-m-d H:i:s");
                $home_ins = mysqli_query($con, "INSERT INTO log_entry(user_id, user_name, logdate, system_ip, time) VALUES('$uid', '/$user_name', '$datetime', '$ip', '$datetime')");

// 			$home_ins=mysqli_query($con,"insert into log_entry(user_id,user_name,logdate,system_ip,time) values('$uid','/$user_name',now(),'$ip',now())");
				header('Location:../index.php');			
			}
			else
			{
			$home_ins1=mysqli_query($con,"insert into log_error(user_name,password,ip_address,date,time) values('$user_name','$p_w','$ip',now(),now())");
				header('Location: index.php?p=1');
			}
		}
		else
		{
			$home_ins1=mysqli_query($con,"insert into log_error(user_name,password,ip_address,date,time) values('$user_name','$p_w','$ip',now(),now())");
				header('Location: index.php?u=1');
		}		
				
		
?>
	
