<?php
//login
include('db_con.php');
//include('function.php');
if(isset($_SESSION['type']))
{
	header("location: dashboard");
}
$message = '';
if(isset($_POST["login"]))
{
	$query = "
	SELECT u.user_id,u.user_name,u.user_pass,u.user_type,u.branch_id,b.branch_name,u.status
	 ,u.store_id,u.user_acc_no,s.store_name FROM user_detail u,branch b,store s WHERE b.branch_id=u.branch_id 
		and u.store_id = s.store_id AND u.user_email = :user_email
	";
	$statement = $connect->prepare($query);
	$statement->execute(
		array(
				'user_email'	=>	$_POST["user_email"]
			)
	);
	$count = $statement->rowCount();
	if($count > 0)
	{
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			if($row['status'] == 'Active')
			{
				if($_POST["user_pass"]==$row["user_pass"])
				{
				
					$_SESSION['user_type'] = $row['user_type'];
					$_SESSION['user_id'] = $row['user_id'];
					$_SESSION['user_name'] = $row['user_name'];
					$_SESSION['branch_id'] = $row['branch_id'];
					$_SESSION['branch_name'] = $row['branch_name'];
					$_SESSION['store_name'] = $row['store_name'];
					$_SESSION['store_id'] = $row['store_id'];
					$_SESSION['user_acc_no'] = $row['user_acc_no'];
					header("location: dashboard");
				}
				else
				{
					$message = "<label>Wrong Password</label>";
				}
			}
			else
			{
				$message = "<label>Your account is disabled, Contact Administrator</label>";
			}
		}
	}
	else
	{
		$message = "<label>Wrong Email Address</labe>";
	}
}
?>
	<!DOCTYPE html>
<html>
<head>
	<title>Login</title>
	<?php include('include/head.php'); ?>
</head>
<body>
	<div class="login-wrap customscroll d-flex align-items-center flex-wrap justify-content-center pd-20">
		<div class="login-box bg-white box-shadow pd-30 border-radius-5">
			<img src="vendors/images/login-img.png" alt="login" class="login-img">
			<h2 class="text-center mb-30">Login</h2>
			<form method="post" autocomplete="off" />
				<div class="input-group custom input-group-lg">
					<input type="text" name="user_email" class="form-control" placeholder="email">
					<div class="input-group-append custom">
						<span class="input-group-text"><i class="fa fa-envelope" aria-hidden="true"></i></span>
					</div>
				</div>
				<div class="input-group custom input-group-lg">
					<input type="password" name="user_pass" class="form-control" placeholder="**********">
					<div class="input-group-append custom">
						<span class="input-group-text"><i class="fa fa-lock" aria-hidden="true"></i></span>
					</div>
				</div>
				<div class="row">
					<div class="col-sm-6">
						<div class="input-group">
							<!--
								use code for form submit
								<input class="btn btn-outline-primary btn-lg btn-block" type="submit" value="Sign In">
							-->
							<input type="submit" name="login" class="btn btn-outline-primary btn-lg btn-block" value="Login" />
						</div>
					</div>
					
				</div>
			</form>
		</div>
	</div>
	
</body>
</html>