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

	head('Accept Order - Management Section');
?>

<body>
<div id="main">

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

<div id="entire_panel">
		<h1>Vyraď výpožičku</h1>
		
<?php
if ($_SESSION['admin'] == 1) {			 
	if(isset($_POST['submit'])) {
	
		$link = connect_db();
		
		$date = date("y-m-d");	
		$date_back = date('y-m-d', strtotime('+3 months'));
		
		$sql = "UPDATE book_orders SET date_ordered='$date', date_back='$date_back', provided='1' WHERE id_order='" . $_SESSION['order_accepted'] . "'";
		$result = mysql_query($sql) or die(mysql_error());
		
		echo '<p id="bold">The order was accepted successfully.</p>';
	 
	} else {
	
		$link = connect_db();
		
		$order_id = $_GET['id'];
		protect($order_id);
		$_SESSION['order_accepted'] = $order_id;
		
		$sql = "SELECT * FROM book_orders WHERE id_order='" . $order_id . "'";
		$result = mysql_query($sql, $link) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		
		$sql2 = "SELECT * FROM books WHERE id_book='" . $row['book_id'] . "'";
		$result2 = mysql_query($sql2, $link) or die(mysql_error());
		$row2 = mysql_fetch_assoc($result2);
		
		$sql3 = "SELECT * FROM users WHERE uid='" . $row['uid'] . "'";
		$result3 = mysql_query($sql3, $link) or die(mysql_error());
		$row3 = mysql_fetch_assoc($result3);
		
		$msg="";
		
		if((!$order_id) || (!is_numeric($order_id))) {
			echo 'Výber výpožičky nebol zadávaný korektným spôsobom.';
		}
	
		if(empty($msg)){
			echo '<h4>Plánuješ akceptovať nasledovnú výpožičku? </h4>';
			echo '<table border="1" class="center_page">';
			echo '<tr><th>USERNAME</th><td id="bold">' . $row3['username'] . '</td></tr>';
			echo '<tr><th>DÁTUM VYPOŽIČANIA</th><td id="bold">' . $row['date_ordered'] . '</td></tr>';
			echo '<tr><th>DÁTUM VRÁTENIA</th><td id="bold">' . $row['date_back'] . '</td></tr>';
			echo '<tr><th>NÁZOV</th><td id="bold">' . $row2['name'] . '</td></tr>';
			echo '<tr><th>AUTOR</th><td id="bold">' . $row2['author'] . '</td></tr>';
			echo '<tr><th colspan=2 class="button">'; 
			?>
			
			<form name=register method=post action="accept_order.php">
				<input name="submit" type="submit" id="submit" value="Akceptuj">
			</form>
			
			<?php
			echo '</th></tr>';
			echo '</table>';
			
			$msg="";
		} else {
			echo '<p id="red">' . $msg . '</p>';
		}
		
		mysql_free_result($result);
		
		mysql_close($link);
		
	}
	
} else {
	echo '<p id="red">Na akceptovanie výpožičky nemáte príslušné právomoci.</p>';
}   
		?>				
</div>

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

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


