<?php
session_start();
	/*
	BooKa.php
	This is the "main" file or "index" file
	*/

if(file_exists("./cfg/lock.lock") == false){
	
	header('Location: ./firstrun.php');
	
}

include "./inc/dbConnect.php";
include "./inc/head.php";

if($_SESSION[logedin]){

?>
	<p><img src="./img/Logo.png" alt="BooKa´s logo" /></p>
	
	<div class="menu">
		 <h1> Logedin <a href="./logout.php">[logout]</a> </h1>
		 <hr />
		 <p>
		 You are logged in.<br />
		 continue to <a href="dbEditor.php"> dbEditor </a> to start booking peopel!
		 </p>
		 <hr />
		 <?php if($_GET[showadminpage]=="true"){ ?><h2> User registration </h2> <?php }?>
		 <p>
		 	
		 	<?php
		 	if($_SESSION[admin]){ //this section prpbobly needs re work?
		 		if($_GET[showadminpage]=="true"){
		 			echo "<a href='BooKa.php?showadminpage=false'>[hide adminpage]</a><br /> \n";
		 		}
		 		if($_GET[showadminpage]=="false" || $_GET[showadminpage] == null){
		 			echo "<a href='BooKa.php?showadminpage=true'>[Show adminpage]</a><br /> \n";
		 		}
		 	} ?>
		 </p>
		 	<?php
		 	if($_SESSION[admin] == true && $_GET[showadminpage]== "true" ){
		 	?>
		 	
		 	<form action="BooKa.php?showadminpage=true" method="post" >
		 		<p>
		 		<input type="hidden" name="userreg" value="true" />
		 		<label for="username">Name of the new user</label><input type="text" name="username" /><br />
		 		<label for="password1">password of the new user</label><input type="password" name="password1" />
		 		<label for="password2">password again</label><input type="password" name="password2" /><br />
		 		<label for="isadmin">is this user a admin?</label> <input type="checkbox" name="isadmin" /><br />
		 		
		 		
		 			<?php
		 			//ifs and buts goes here
		 			if($_POST[userreg]){
		 				
		 				
		 				$canreg = true;
		 				
		 				$query = "SELECT * FROM users WHERE username = '{$_POST[username]}';";
	
						$userinfo = $db->query($query);
	
						$newinfo = $userinfo->fetch();
		 				
		 				if($newinfo[username] == $_POST[username]){
		 					
		 					echo "<span class='warning'> username is already in use </span> <br />";
		 					$canreg = false;
		 				}
		 				
		 				if($_POST[username] == null){
		 					echo "<span class='warning'>you need to enter a new username </span> <br />";
		 					$canreg = false;
		 				}
		 				
		 				if($_POST[password1] == null){
		 					
		 					echo "<span class='warning'>you need to have a password <br /> </span>";
		 					$canreg = false;
		 				}
		 				
		 				if($_POST[password2] != $_POST[password1]){
		 					
		 					echo "<span class='warning'>passwords deos not match </span> <br />";
		 					$canreg = false;
		 				}
		 				sleep(.5);//to prevent $conreg being true whun it should be false 0_o?
		 				if($canreg){
		 					if($_POST[isadmin]){
		 						$group = 1;
		 					}else {
		 						
		 						$group = 2;
		 						
		 					}
		 					$username1 = $_POST[username];
		 					$password1 = md5($_POST[password1]);
		 					
		 					$db->queryExec("BEGIN;
								INSERT INTO users (username, password, 'group')
								VALUES('{$username1}' , '{$password1}', '{$group}');
							COMMIT;") or die("could not do query");
		 					echo $db->error;
		 					echo "Registration of user {$_POST[username]} complite!";
		 				}
		 			}
		 			?>
		 		<input type="submit" /><br />
		 		</p>
		 	</form>
		 	
		 	<?php 	
		 		if($_SESSION[admin]){ //this section prpbobly needs re work?
		 			if($_GET[showrmuser]=="true"){
		 				echo "<a href='BooKa.php?showadminpage=true&amp;showrmuser=false'>[Hide remove user page]</a><br /> \n";
		 			}
		 			if($_GET[showrmuser]=="false" || $_GET[showrmuser] == null){
		 				echo "<a href='BooKa.php?showadminpage=true&amp;showrmuser=true'>[Show remove user page]</a><br /> \n";
		 			}
		 		}
		 		if($_GET[showrmuser]=="true"){
		 			
		 			//table with user id, user name and a remove button goes here...
		 			?>
		 			<h2>DO NOT REMOVE THE LAST ADMIN PL0X!</h2>
		 			<table> <!-- muckup tabell -->
						<tr> <!-- row one -->
							<td> uid </td>
							<td> User Name </td>
							<td> Admin </td>
							<td> remove </td>
						</tr> <!-- end of row one -->
						<?php
						
						$result = $db->query("SELECT * FROM users") or die("could not do query!");
						foreach($result as $row){
						?>
						<tr> <!-- Row two and up -->
							<td> <?php echo $row[id]?> </td>
							<td> <?php echo $row[username]?> </td>
							<td> <?php
								if($row[group] == 1){
									echo "Yes";
								} else {
									echo "no";
								}
								
							?> </td>
							<td>
								<form action="removeUser.php" method="post">
									<p>
										<input type="submit" value="remove" />
										<input type="hidden" value="<? echo $row[id]?>" name="id"/>
									</p>
								</form>
							</td>
						</tr> <!-- end of row two and up -->
						
						<?php
						}
						?>
					</table>
					 


			<?php
		 		}
		 	}
		 	?>
	</div>
<?php
} else {
?>
	<div class="head"><img src="./img/Logo.png" alt="BooKa´s logo" /></div>
	
	<div class="menu">
		 <h1> Login </h1>
		 <hr />
		 <form action="login.php" method="post">
		 	<p>
		 		User name: <input type="text" name="userName" /> <br/>
		 		Pass word:  <input type="password" name="userPassword" /> <br />
		 		<input type="hidden" name="login" value="true" />
		 		<input type="submit" name="null" value="Login" />
		 	</p>
		 </form>
		 <p>
		 	All registrations are performed by admin.
		 </p>
	</div>
	
<?php
}

include "./inc/foot.php";

?>
