<?php session_start(); 
// Change this to your connection info.
 $DATABASE_HOST = 'localhost';
  $DATABASE_USER = 'newjerusalemonli_phplogin'; $DATABASE_PASS = 'newjerusalemonli_phplogin';
  $DATABASE_NAME = 'newjerusalemonli_phplogin'; // Try and connect using the info above. 
  $con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
 if ( mysqli_connect_errno() ) {
     // If there is an error with the connection, stop the script and display the error.
 exit('Failed to connect to MySQL: ' . mysqli_connect_error()); }


 // Now we check if the data from the login form was submitted, isset() will check if the data exists. 
 if ( !isset($_POST['username'], $_POST['password']) ) { 
    // Could not get the data that should have been sent.
     exit('Please fill both the username and password fields!'); }
// to prevent sql injection, we prepare our sql
if ($stmt = $con->prepare('SELECT id, password1, level1 FROM accounts1 Where username = ?')) {
    //bind parameters(s =string, i = int, b = blob, etc)
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    // storer the result so we can check if the account is exists in the database
    $stmt->store_result();

   
}


if ($stmt->num_rows > 0) {
	$stmt->bind_result($id, $password1, $level1);
	$stmt->fetch();
	// Account exists, now we verify the password.
	// Note: remember to use password_hash in your registration file to store the hashed passwords.

	// login for admin
	if (password_verify($_POST['password'], $password1) && $level1 == 'tithes') {
		// Verification success! User has logged-in!
		// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1;
		$_SESSION['tithes'] = $level1 = 'tithes';

		//echo 'Welcome back, ' . htmlspecialchars($_SESSION['level'], ENT_QUOTES) . '!';
       header('location: ../frontpage/tithesfront.php');}

	   
// login for checkin
	   if (password_verify($_POST['password'], $password1) && $level1 == 'checkin') {
		// Verification success! User has logged-in!
		// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1 ;
		$_SESSION['checkin'] = $level1 = 'checkin';

		 // echo 'Welcome back, ' . htmlspecialchars($_SESSION['level'], ENT_QUOTES) . '!';
       header('location: ../frontpage/checkinfront.php');}

	   // login for pasteur
	   if (password_verify($_POST['password'], $password1) && $level1 == 'pasteur') {
		// Verification success! User has logged-in!
		// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1;
		$_SESSION['pasteur'] = $level1 = 'pasteur';

		 // echo 'Welcome back, ' . htmlspecialchars($_SESSION['level'], ENT_QUOTES) . '!';
       header('location: ../frontpage/pasteurAccess.php');}

	   // login for senior paster
	   if (password_verify($_POST['password'], $password1) && $level1 == 'senior') {
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1;
		$_SESSION['senior'] = $level1 = 'senior';

		//header('location: ../frontpage/seniorPasteurAccess.php');
		header('location: ../includes/mgtconfidential.php');
	   }

	   // login for children ministry
	   if (password_verify($_POST['password'], $password1) && $level1 == 'children') {
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1;
		$_SESSION['children'] = $level1 = 'children';

		header('location: ../frontpage/jquery/index.php');
	   }
	   
	   
	    // login for front desk
	   if (password_verify($_POST['password'], $password1) && $level1 == 'frontdesk') {
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['name'] = $_POST['username'];
		$_SESSION['id'] = $id;
		$_SESSION['level'] = $level1;
		$_SESSION['frontdesk'] = $level1 = 'frontdesk';

		header('location: ../frontpage/jquery2/index.php');
	   }

	   




	} else {
		// Incorrect password
		echo 'Incorrect username and/or password!  ';
		
		echo '  If you were granted access, check your username and password and enter it again';
		echo '';

		Echo ' Or contact the church management team to reset your password';
		
	}


// $stmt->close();