<?php
require "include/session.php";
session_start();
$session = new Session();
$isLoggedAdmin = $session->isLoggedAdmin();
$isLoggedStaff = $session->isLoggedStaff();
if($isLoggedAdmin|| $isLoggedStaff){
	header('Location: index.php');
}
if (isset($_REQUEST['submit'])){
	require "include/db.php";
	if($conn){    
			$user_email = $_REQUEST['user_email'];
			$password = $_REQUEST['password'];
			$query = "SELECT * FROM employee WHERE employee_email = '$user_email' AND status = 'active'";	
			
			$run = mysqli_query($conn, $query);

			if(mysqli_num_rows($run)==1){
				$row = mysqli_fetch_array($run);
				$pass = $row['password_hash'];
				$employee_id = $row['employee_id'];
				if(strlen($password) > 0){	
					if(strlen($pass) > 0){				
						$verify = password_verify($password,$pass);
						if($verify){				
							$session = new Session();
							$employee_login = $session-> logUserStaff($employee_id);
							if($employee_login){								
								header('Location: index.php');
								exit;
							}else{								
								$message = "Technical Problem. Contact admin!";				
							}
						}else{
							$message = "Login Details Incorrect. Try Again!";					
						} 
					}else{
						$message = "Login Details Incorrect. Try Again!";					
					} 
				}else{
					$message = "Login Details Incorrect. Try Again!";					
				}                  
			}else{
				$query2 = "SELECT * FROM admin WHERE admin_email = '$user_email' AND status = 'active'";	
			
				$run2 = mysqli_query($conn, $query2);

				if(mysqli_num_rows($run2)==1){
					$row2 = mysqli_fetch_array($run2);
					$pass = $row2['password_hash'];
					$admin_id = $row2['admin_id'];
					if(strlen($password) > 0){	
						if(strlen($pass) > 0){	
							$verify = password_verify($password,$pass);	
							if($verify){				
								$session = new Session();
								$admin_login = $session-> logUserAdmin($admin_id);
								if($admin_login){								
								header('Location: index.php');
								exit;
								echo "yes";
								}else{
									$message = "Technical Problem. contact Admin!";		
								}
							}else{
								
								$message = "Login Details are Incorrect. Try Again!";					
							}  
						}else{
								
							$message = "Login Details are Incorrect. Try Again!";					
						}	
					}else{
								
						$message = "Login Details are Incorrect. Try Again!";					
					}

				}else{
					$message = "An Account with Email ID $user_email does not exist contact management.";
				}				
			}
		
	}else{
		$message = "Technical problem. Contact Admin!";
	}       
}
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Bid Estimation Software</title>

    <!-- Bootstrap Core CSS -->
	<link href="css/bootstrap.3.min.css" rel="stylesheet">
    <!-- MetisMenu CSS -->
    <link href="css/metisMenu.min.css" rel="stylesheet">

    <!-- Custom CSS -->
	<link href="css/sb-admin-2.min.css" rel="stylesheet">
	<link href="css/custom.css" rel="stylesheet">


    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body id = "login">
<div class="navbar navbar-fixed-top">
		<div class="navbar-inner">
			<div class="container">
				<a class="btn btn-navbar" data-toggle="collapse" data-target=".navbar-inverse-collapse">
					<i class="icon-reorder shaded"></i>
				</a>
			<div id = "login-container">
			<div id = "login-logo">	

			  	<a class="brand" href="" style="text-align:center; float:none; padding: 0px;">
				  <?php
				  require "include/db.php";
$est_message_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
$est_message_run = mysqli_query($conn, $est_message_query);
$est_message_row = mysqli_fetch_array($est_message_run);
$estimate_logo = $est_message_row['logo'];
$logo_url = "images/". $estimate_logo;
?>
			  		<img src="<?php echo $logo_url; ?>" height= "80px" alt="Bid Estimate Software"/>
				  </a>

			</div>
			</div>	  
				  
				<div class="nav-collapse collapse navbar-inverse-collapse">
				
					<ul class="nav pull-right">

						
					</ul>
				</div><!-- /.nav-collapse -->
			</div>
		</div><!-- /navbar-inner -->
	</div><!-- /navbar -->

    <div class="container">
        <div class="row">
            <div class="col-md-4 col-md-offset-4">			
                <div class="login-panel panel panel-default">
				<?php if(isset($message)){
						
												echo '<div class="alert alert-danger">
												<strong>';
												echo $message;
												echo "</strong></div>";
												}
												 ?>
                    <div class="panel-heading">
                        <h3 class="panel-title">Sign In</h3>
					</div>
					
					
					

                    <div class="panel-body">
                        <form role= "form" action="login.php" method="post" name="login">
                            <fieldset>
                                <div class="form-group">
								<input class="form-control" name = "user_email" type="text" id="inputEmail" placeholder="Email" required = "required">
                                </div>
                                <div class="form-group">
								<input class="form-control" type="password" name= "password" id="inputPassword" placeholder="Password" required = "required">
                                </div>
                                
                                <input name="submit" class="btn btn-lg btn-success btn-block" type="submit" style = "border-color: #c12d2a" value="Login" />
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- jQuery -->
	<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')</script>
    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.3.min.js"></script>

    <!-- Metis Menu Plugin JavaScript -->
    <script src="js/metisMenu.min.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="js/sb-admin-2.min.js"></script>

</body>

</html>