<?php
	session_name('library');
	session_start();
	
	include("functions.php");

	head('Order Page');
?>

<body>
<div id="main">

	<?php
		include("header.php");
		include("menu.php");
	?>

<div id="entire_panel">
		<h1>Vypožičaj publikáciu</h1>
		
<?php
if(isset($_SESSION['uid'])) {			 
	if(isset($_POST['submit'])) {
	
		$link = connect_db();
		
		$sql = "SELECT * FROM users WHERE uid='" . $_SESSION['uid'] . "'";
		$result = mysql_query($sql, $link) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		
		$new_count = $row['actual_count'] + 1;
		
	
		$sql = "INSERT INTO book_orders SET uid='" . $_SESSION['uid'] . "', provided='0', book_id='" . $_SESSION['book_order'] . "'";
		mysql_query($sql, $link) or die(mysql_error());
		
		$sql2 = "UPDATE users SET actual_count='" . $new_count . "' WHERE uid='" . $_SESSION['uid'] . "' LIMIT 1";
		mysql_query($sql2, $link) or die(mysql_error());
		
		$sql3 = "UPDATE books SET in_offer='0' WHERE id_book='" . $_SESSION['book_order'] . "' LIMIT 1;";
		mysql_query($sql3, $link) or die(mysql_error());
		
		echo '<p id="bold">The order was added successfully.</p>';
	 
	} else {
	
		$link = connect_db();
		
		$book_id = $_GET['id'];
		protect($book_id);
		$_SESSION['book_order'] = $book_id;
		
		$sql = "SELECT * FROM books WHERE id_book='" . $book_id . "'";
		$result = mysql_query($sql, $link) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		
		$sql2 = "SELECT * FROM book_categories WHERE id_group='" .$row['id_group']. "'";
		$result2 = mysql_query($sql2, $link) or die(mysql_error());
		$row2 = mysql_fetch_assoc($result2);
		
		$sql3 = "SELECT * FROM users WHERE uid='" .$_SESSION['uid']. "'";
		$result3 = mysql_query($sql3, $link) or die(mysql_error());
		$row3 = mysql_fetch_assoc($result3);
		
		$msg="";
		
		if ($row['in_offer'] = 0) {
			$msg .= 'Zadaná publikácia je momentálne vypožičaná' . '<br />';
		}
		
		if ($row3['actual_count'] >= 5) {
			$msg .= 'Výpožička zamietnutá z dôvodu prekročenia limitu v počte vypožičaných kníh' . '<br />';
		}
		
		if((!$book_id) || (!is_numeric($book_id))) {
			echo 'Výpožička nebola zadávaná korektným spôsobom.';
		}
	
		if(empty($msg)){
			echo '<h4>Praješ si skutočne vypožičať nasledovnú knihu? </h4>';
			echo '<table border="1" class="center_page">';
			echo '<tr><th>NÁZOV</th><td id="bold">' . $row['name'] . '</td></tr>';
			echo '<tr><th>AUTOR</th><td id="bold">' . $row['author'] . '</td></tr>';
			echo '<tr><th>ROK VYDANIA</th><td id="bold">' . $row['year_published'] . '</td></tr>';				
			echo '<tr><th>ŽÁNER</th><td id="bold">' . $row2['name'] . '</td></tr>';
			echo '<tr><th colspan=2 class="button">';
			?>
			
			<form name=register method=post action="order.php">
				<input name="submit" type="submit" id="submit" value="Vypožičaj">
			</form>
			
			<?php	
			
			echo '</tr>';
			echo '</table><br /><br />';
			
			echo '<p>Momentálny počet vypožičaných publikácii: ' . $row3['actual_count'] . '</p><br />';
			$msg="";
		} else {
			echo '<p id="red">' . $msg . '</p>';
		}
		
		mysql_free_result($result);
		mysql_free_result($result2);
		mysql_free_result($result3);
		
		mysql_close($link);
		
	}
	
} else {
	echo '<p id="red">Pre vypožičanie publikácie musíte byť prihlásený do systému.</p>';
}   
		?>				
</div>

	<?php include("footer.php"); ?>

</div>
</body>
</html>
