<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}


if($_POST['newmenu']){
	$sql = "insert into menu_master (menu_name) values ('".mysql_real_escape_string($_POST['newmenu'])."')";
	mysql_query($sql);
	echo "<font color='red'><center>Menu added successfully.</center></font>";
}

$query = "Select number_of_menus from storePrefs" ;
$rt=mysql_query($query);
while($nt=mysql_fetch_array($rt)){ 
	$subscribed_menus = $nt['number_of_menus'];
}

$query = "Select * from menu_master ";
if(!empty($_GET[delmenukey])){$query .=" where recordkey <> $_GET[delmenukey]";}
$query .=" order by menu_name" ;
$rt=mysql_query($query);
$menus_in_use = mysql_num_rows($rt);

//allow creating new menu if not over subscription limit
if($subscribed_menus > $menus_in_use){
	echo '<div align="center"><form action="' . $_SERVER['PHP_SELF'] . '?a=addsize" method="post">
	Add new menu (name): <br><input type="text" name="newmenu" size=20 maxlength=20>
	<input id="save-button" type="submit" name="save" value="Create this Menu"></form></div>';	
}

if(empty($_POST)){
	//form not submitted...check url for querystring parameters	
	if(isset($_GET['addcat']) && isset($_GET['menu'])){
		$sql = "insert into menu_cat_master (menukey, categorykey) values('$_GET[menu]','$_GET[addcat]')";
		if(mysql_query($sql)){
			echo "<font color='red'><center>Category added successfully.</center></font>";
		}
	}
	if(isset($_GET['remcat']) && isset($_GET['menu'])){
		$sql = "delete from menu_cat_master where menukey='$_GET[menu]' AND categorykey='$_GET[remcat]'";
		if(mysql_query($sql)){
			echo "<font color='red'><center>Category removed successfully.</center></font>";
		}
	}
	if(isset($_GET['delmenukey'])){
		//first remove categories from this menu	
		$sql = "delete from menu_cat_master where menukey='$_GET[delmenukey]'";
		if(mysql_query($sql)){
			//echo "<font color='red'><center>Category removed successfully.</center></font>";
			$removed=1;
		}
		//now remove this menu
		$sql = "delete from menu_master where recordkey='$_GET[delmenukey]'";
		if(mysql_query($sql) && ($removed==1)){
			//reset the session menu id, then refresh page
			$_SESSION['selected_menu']="";
			echo "<script>location.href='menu_admin.php</script>";

		} else {
			echo "<font color='red'><center>There was a problem deleting the menu.</center></font>";
		}
	}	
}

//if different menu selected
if($_POST['selected_menu']){
	$_SESSION['selected_menu']=$_POST['selected_menu'];
}

//if changes submitted
if($_POST['save_changes']){
	if($_POST['menu_name']<>""){
		$active_date = new DateTime(mysql_real_escape_string($_POST['active_date']));
		$inactive_date = new DateTime(mysql_real_escape_string($_POST['inactive_date']));
		$active_date = date_format($active_date, 'Y-m-d');
		$inactive_date = date_format($inactive_date, 'Y-m-d'); 
		$qry = "update menu_master set menu_name = '".mysql_real_escape_string($_POST['menu_name'])."'";
		$qry .= ", active_date = '$active_date'";
		$qry .= ", inactive_date = '$inactive_date'";
		$qry .= ", active_time_of_day = '".mysql_real_escape_string($_POST['active_time_of_day'])."'";
		$qry .= ", inactive_time_of_day = '".mysql_real_escape_string($_POST['inactive_time_of_day'])."'";
		if(!empty($_POST['ordering_enabled'])){$ordering_enabled = 1;} else {$ordering_enabled=0;}
		$qry .= ", ordering_enabled = '$ordering_enabled'";
		if(!empty($_POST['disabled'])){$disabled = 1;} else {$disabled=0;}
		$qry .= ", disabled = '$disabled'";
		
		$qry .= " where recordkey='$_POST[menu_id]'";
		mysql_query($qry);
		echo "<font color='red'><center>Changes saved successfully.</center></font>";
	}
}



echo "<div id='mm-top-left'><form action='' method='POST'><select name='selected_menu'>";
while($nt=mysql_fetch_array($rt)){ 
	//show all existing menus 
	echo "<option value='$nt[recordkey]'";
	if($nt['recordkey']==$_POST['selected_menu'] || $nt['recordkey']==$_SESSION['selected_menu']){echo " selected='selected'";}
	echo">$nt[menu_name]</option>";
	if(empty($_SESSION['selected_menu'])){$_SESSION['selected_menu']=$nt['recordkey'];}
	
}
echo"</select> <input id='save-button' type='submit' value='Manage'></form>";

//echo "selected: ".$_SESSION['selected_menu'];

if(isset($_SESSION['selected_menu'])){
	$query = "Select * from menu_master where recordkey='$_SESSION[selected_menu]'" ;
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		
		echo "<form action='' method='POST'>";
		echo "Menu ID: $nt[recordkey] <input type='hidden' name='menu_id' value=$_SESSION[selected_menu]><br>
		<span class='left-label'>Name: </span><input type='text' name='menu_name' value='$nt[menu_name]'><br>
		<span class='left-label'>Active date: </span><input type='text' id='datepicker1' name='active_date' value='$nt[active_date]'><br>
		<span class='left-label'>Inactive date: </span><input type='text' id='datepicker2' name='inactive_date' value='$nt[inactive_date]'><br>
		<span class='left-label'>Active time: </span><input type='text' name='active_time_of_day' value='$nt[active_time_of_day]'><br>
		<span class='left-label'>Inactive time: </span><input type='text' name='inactive_time_of_day' value='$nt[inactive_time_of_day]'><br>
		<br />
		<span class='left-label_long'>Allow online ordering: </span><input type='checkbox' name='ordering_enabled'";
		if(!empty($nt[ordering_enabled])){echo "checked=checked";}
		echo "><br>";
		echo "<span class='left-label_long'>Disable this menu: </span><input type='checkbox' name='disabled'"; if(!empty($nt[disabled])){echo "checked=checked";}
		echo "><br>";
		echo "<input id='save-button' type='submit' name='save_changes' value='Save'></form>"; 
		echo ' &nbsp; &nbsp;<input id="save-button" type="button" onclick="ConfirmDelete()" value="Delete this Menu">';
		echo '</div>';
		
	}
	
	//show categories assigned to this menu
	echo "<div id='mm-top-right'><span class='mm-cat-title'>Categories in Menu</span><br>";
	$query = "Select RecordKey, CategoryName from menuCategory where RecordKey in (select categorykey as RecordKey from menu_cat_master where menukey='$_SESSION[selected_menu]')" ;
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		echo "<p>";
		echo $nt['CategoryName']." <a href='?remcat=$nt[RecordKey]&menu=$_SESSION[selected_menu]' title='remove from menu' alt='remove from menu'><font color='red'>(X)</font></a></p>";
	}
		
	//show categories not assigned to this menu
	$pcat = "'Pizza Toppings','Pizza Sauce','Pizza Crust'";
	echo "<br><span class='mm-cat-title'>Categories not in Menu <em>(click category to add)</em></span><br>";
	$query = "Select RecordKey, CategoryName from menuCategory where categoryName not in ($pcat) AND
	RecordKey NOT IN (select categorykey as RecordKey from menu_cat_master where menukey='$_SESSION[selected_menu]')" ;
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		echo "<p><a href='?addcat=$nt[RecordKey]&menu=$_SESSION[selected_menu]' title='add to menu' alt='add to menu'>".$nt['CategoryName']."</p>";
	}
			
}
echo "</div>";
echo "</div>";

?>


<script type="text/javascript">
      function ConfirmDelete()
      {
            if (confirm("Delete Menu?"))
                 location.href='<?php echo "menu_admin.php?delmenukey=$_SESSION[selected_menu]"; ?>';
      }
</script>
<script>
  $(document).ready(function() {
    $("#datepicker1").datepicker();
  });
  $(document).ready(function() {
    $("#datepicker2").datepicker();
  });
</script>
