<?php 
session_start();
include("header.php");
if(trim($_SESSION['accountName'])=="" &&  trim($_SESSION['accountID'])=="" && trim($_SESSION['pgid'])!="i3g3e3nious7")
{
	echo '<script>';
	echo 'location.replace("index.php");';
	echo '</script>';
}
else
{

$errmsg = "";
$menuID=0;

if(trim($_GET['id'])!="")
	$menuID=$_GET['id'];
elseif(trim($_POST['menuID'])!="")
	$menuID=$_POST['menuID'];

$action=$_POST['action'];
$menuName=$_POST['menuName'];
$details=$_POST['details'];
$mCode=$_POST['mCode'];
$orderBy=$_POST['orderBy'];
$mtyp=$_POST['mtyp'];

if(strtoupper($action) == "UPDATE")
{
	if(trim($menuName)=="")
		$errmsg = $errmsg." Menu Name field is empty<br/>";
	
	if(trim($mCode)=="")
		$errmsg = $errmsg . "Menu Code field is empty<br/>";
	
	if(trim($orderBy)=="")
		$errmsg = $errmsg . "Order By field is empty<br/>";
	
	if(trim($errmsg)=="")
	{
		$strSql = mysql_query("update tr_menu set menu_code='".$mCode."',menu_name='".$menuName."',menu_details='".$details."',menu_type='".$mtyp ."',orderby='".$orderBy."' where menu_id='".$menuID."'");
		
		$errmsg = "You have successfully updated your Menu.";
	}
}
else
{
	$strSql =mysql_query("SELECT a.ingredient_id,b.* FROM tr_menu b left join tr_ingredient a on b.menu_id=a.menu_id where b.menu_id='".$menuID."'");
	$rs= @mysql_fetch_array($strSql);
	$rcnt=mysql_num_rows($strSql);
	If($rcnt>0)
	{
		$menuID=$rs['menu_id'];
		$menuName=$rs['menu_name'];
		$details=$rs['menu_details'];
		$mCode=$rs['menu_code'];
		$orderBy=$rs['orderby'];
		$mtyp=$rs['menu_type'];
		$ingrDesc="<a href='ingredientNew.php?id=".$menuID."'>Create New Ingredient</a>";

		if(trim($rs['ingredient_id'])!="")
			$ingrDesc="<a href='IngredientUpdate.php?id=".$rs['ingredient_id']."'>Update Ingredient</a>";		
	}
	
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14 black"><u><b>Update Menu <?=$menuName?></b></u></td></tr>
	<tr><td width="200" class="fs12" valign="top"><?php include("sidemenu.php");?></td>
		<td width="700" class="fs12">
			<table width="680" cellpadding="1" cellspacing="1" border="0">
			<tr><td colspan="4"><hr /></td></tr>
		<?php	if(strtoupper(substr(trim($errmsg),0,8)) == "YOU HAVE")
		{
				echo "<tr valign='top' align='center'><td colspan='4'>".$errmsg."</td></tr>";
		?>
				<meta http-equiv="Refresh" content="3;url=menus.php?mt=<?=$mtyp?>"><br/><br/><br/><br/><br/>
		<?php
		}
		else
		{
			?>
				<form action="menuUpdate.php?id=<?=$menuID?>" method="POST" name="frm">
				<input type="hidden" name="menuID" value="<?=$menuID?>" />
				<tr><td></td><td class="errmsg" colspan="3"><?=$errmsg?></td></tr>
				<tr><td height="10" width="17%"></td><td width="33%" class="fs10"><i>* required fields</i></td><td height="10" width="15%"></td><td width="35%"></td></tr>
				<tr><td align="right"><b>* </b>MENU NAME:&nbsp;</td><td><input class="formInput" type="text" name="menuName" value="<?=$menuName?>" /></td>
					<td align="right">DETAILS:&nbsp;</td><td rowspan="4"><textarea name="details" class="formtextarea"><?=$details?></textarea></td></tr>
				<tr><td align="right"><b>* </b>MENU CODE:&nbsp;</td><td><input class="formInputS" maxlength="2" type="text" name="mCode" value="<?=$mCode?>"/></td><td></td></tr>
				<tr><td align="right"><b>* </b>ORDER BY:&nbsp;</td><td><input class="formInputS" type="text" name="orderBy" value="<?=$orderBy?>"/></td><td></td></tr>
				<tr><td align="right">MENU TYPE:&nbsp;</td>
					<td><select name="mtyp" class="formYear">
					<?php
					$strSql = mysql_query("select mtype_id,menu_type from ma_menutype where status=1");
					$rcnnt=mysql_num_rows($strSql);
					If($rcnnt>0)
					{
						while($rs=@mysql_fetch_array($strSql))
						{
							if($mtyp==$rs['mtype_id'])
							echo "<option value='".trim($rs['mtype_id'])."' selected >".$rs['menu_type']."</option>";
							else
							echo "<option value='".trim($rs['mtype_id'])."' >".$rs['menu_type']."</option>";
						
						}
					}
					else
					{
						echo "<option value='0'>--Select--</option>";
					}
					?>
				</select></td><td></td></tr>
				<tr valign="top"><td colspan="3" align="center"><?=$ingrDesc?></td>
				<td><input class="fs12" type="submit" name="action" value="UPDATE" /></td></tr>
				</form>
				<?php
				}
				?>
				<tr><td colspan="4"><hr /></td></tr>
			</table></td></tr>
</table>
<?php
include("footer.php");
}
?>