<?php
	ob_start();
	session_start();
	
	require_once("class/connectdb.php");
    $conn = new DbConnect;
    $con = $conn->connect();
	
	//It will never let you open index(login) page if session is set
	if(isset($_SESSION['userId'])){
		header("location: upload_product");
		exit;
	}
	
	$output = '';
	
    if(isset($_POST['btn-login'])){
		require("class/clogin.php");
		
		$email = mysqli_real_escape_string($con, $_POST['email']);
		$pass = mysqli_real_escape_string($con, $_POST['password']);
		
		$password = hash('sha256', $pass); // password hashing using SHA256

		$user = new Login('users', $email, $password);

		if($user->getUser()){
			 $_SESSION['userId'] = $user->userId();
			 $_SESSION['username'] = $user->getFirstname();
			 header("location: upload_product");
		}else{
			 $output = '<div class="alert alert-danger alert-dismissible fade in" role="alert">
							<button type="button" class="close" data-dismiss="alert" aria-label="Close">
							</button>
							<span class="glyphicon glyphicon-info-sign"></span><strong>Invalid login Credential... </strong>
						</div>';
		}
	}
	
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Trustee Pharmacy Admin! | </title>

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="../vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- Animate.css -->
    <link href="../vendors/animate.css/animate.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="../build/css/custom.min.css" rel="stylesheet">
  </head>

  <body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>
      <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
            <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
			
			  <h1>Login Form</h1>
			  
			  <div class="form-group"> 
				<div class="col-md-12 col-sm-12 col-xs-12">
					<?php echo $output; ?>
				</div>
			  </div>
			  
              <div>
			    <input type="text" name="email" class="form-control" placeholder="Email" required="" />
              </div>
              <div>
			    <input type="password" name="password" class="form-control" placeholder="Password" required="" />
              </div>
			  
              <div>
                <button type="submit" class="btn btn-block btn-primary" name="btn-login">Sign In</button>
                <!-- <a class="reset_pass" href="#">Lost your password?</a> -->
              </div>

              <div class="clearfix"></div>

              <div class="separator">
                <p class="change_link">Forgot password?
                  <a href="forgot-password" class="to_register"> Click here..</a>
                </p>

                <div class="clearfix"></div>
                <br />

                <div>
                  <h1><i class="fa fa-paw"></i> Trustee Drugs & Medical Supplies.</h1>
                  <p>© <?php echo date('Y'); ?> All Rights Reserved.</p>
                </div>
              </div>
            </form>
          </section>
        </div>
      </div>
    </div>
  </body>
</html>
