<?php
session_start();
error_reporting(0);

include('includes/config.php');
require_once("includes/dbcontroller.php");
$db_handle = new DBController();

if(isset($_SESSION['uid']) && !empty($_SESSION['uid'])){
	$extra="home.php";
	$host=$_SERVER['HTTP_HOST'];
	$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
	header("location:$website_url/$extra");
	exit();
}
// Code for User login

if(isset($_POST['login'])){
	$email=$_POST['email'];
	$password=md5($_POST['password']);
	$query=mysqli_query($con,"SELECT * FROM users WHERE Useremail='$email' and password='$password'");
	$num=mysqli_fetch_array($query);
	mysqli_close($con);
	if($num>0 && $num['Active']==1 ){
		$extra="deliveries";
		$_SESSION['email']=$num['Useremail'];
		if($num['Admin']==1){
			$_SESSION['role']='Admin';
		}
		else{
			$_SESSION['role']='User';
		}
		$_SESSION['uid']=$num['userid'];
		$host=$_SERVER['HTTP_HOST'];
		$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
		header("location:$website_url/$extra");
		exit();
	}
	elseif($num>0 && $num['Active']==0 ){
		//$extra="/";
		$status=0;
		$host  = $_SERVER['HTTP_HOST'];
		$uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
		header("location:$website_url");
		$_SESSION['errmsg']="email id is Inactive";
		exit();
	}
	else{
		//$extra="/";
		$status=0;
		$host  = $_SERVER['HTTP_HOST'];
		$uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
		header("location:$website_url");
		$_SESSION['errmsg']="Invalid email id or Password";
		exit();
	}
}

include('includes/header.php'); 
?>

<!-- Sign-in -->

<section id="main-container" class="main-container">	
	<div class="container">
		<div class="row">
			<div class="login-template">
				<div class="login-form">
					<h2>Login</h2>
					<form class="login-form outer-top-xs" method="post">
						<p>Please Enter your Username and Password</p>
						<span style="color:red;" >
						<?php
						echo htmlentities($_SESSION['errmsg']);
						?>
						<?php
						echo htmlentities($_SESSION['errmsg']="");
						?>
						</span>
						<h3>Email</h3>
						<label for="inputEmail" class="sr-only">Email address</label>
					    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="" autofocus="">
						<h3>Password</h3>
					    <label for="inputPassword" class="sr-only">Password</label>
					    <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="">
					    <div class="form-group" >
							<button class="btnSubmit" type="submit" name="login" style="margin:2px !important;">Login</button>
							<button class="btnSubmit"><a href="forgotpassword.php" style="margin: 2px !important;">Forgot Password</a></button>	
						</div>
					</form>	
					
						
				</div>
			</div>
		</div>
	</div>
</section>
<!-- Sign-in -->
<?php
include('includes/footer.php');
include('includes/webfooter.php');
?>