<?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
{



$detID=0;
$menuSel=0;
$itemSel=0;
$snakSel=0;
$drinkSel=0;
$studentID=0;

if(trim($_GET["oID"])!="")
{
	$detID=$_GET["oID"];
}
else if(trim($_POST["detID"])!="")
{
	$detID=$_POST["detID"];
}

if(trim($_GET["sID"])!="")
	$studentID=$_GET["sID"];
if(trim($_POST["menuSel"])!="")
	$menuSel=$_POST["menuSel"];
if(trim($_POST["itemSel"])!="")
	$itemSel=$_POST["itemSel"];
if(trim($_POST["snakSel"])!="")
	$snakSel=$_POST["snakSel"];
if(trim($_POST["drinkSel"])!="")
	$drinkSel=$_POST["drinkSel"];

$byear = date('Y');
$bmonth = date('m');
$bday = date('d');
$toDate = $byear+ 1;
$frDate = $toDate - 20;
$action=$_POST["action"];

if(trim($_POST["byear"])!="")
	$byear = $_POST["byear"];
if(trim($_POST["bmonth"])!="")
	$bmonth =$_POST["bmonth"];
if(trim($_POST["bday"])!="")
	$bday = $_POST["bday"];

if(strtoupper($action) == "UPDATE")
{
	$sDate = $byear."-".$bmonth."-".$bday." 00:00:00";
	$strInput = "item_ordered=".$menuSel.",";
	$strInput = $strInput."service_date='".$sDate."',";
	$strInput = $strInput."item_type=".$itemSel.",";
	$strInput = $strInput."item_snack=".$snakSel.",";
	$strInput = $strInput."item_drink=".$drinkSel;
	$strSql = @mysql_query("update tr_orderdetails set ".$strInput." where detail_id=".$detID);
	
	$errmsg = "You have successfully updated your Student Order.";
}
else
{
	
	$strSql11 =@mysql_query("select detail_id,service_date,item_ordered,item_type,item_snack,item_drink from tr_orderdetails where detail_id=".$detID);
	$cntr=@mysql_num_rows($strSql11);
	
	if($cntr>0)
	{
		$rsf=@mysql_fetch_array($strSql11);

		$detID = $rsf["detail_id"];
		$menuSel = $rsf["item_ordered"];
		$itemSel = $rsf["item_type"];
		$snakSel = $rsf["item_snack"];
		$drinkSel =$rsf["item_drink"];
		if(trim($rsf["service_date"])!="")
		{
			/*byear = Year(cDate(rs("service_date")))
			bmonth = Month(cDate(rs("service_date")))
			bday = Day(cDate(rs("service_date")))*/

			$dt=explode("-",substr($rsf['service_date'],0,10));

			$byear=$dt[0];
			$bmonth=$dt[1];
			$bday=$dt[2];


			/*$byear=date('Y',strtotime($rsf['service_date']));
			$bmonth=date('m',strtotime($rsf['service_date']));
			$bday=date('d',strtotime($rsf['service_date']));*/
				
		}
	}
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14"><u><b>Student Order Update</b></u></td></tr>
	<tr><td width="200" class="fs12" valign="top">
	<?php
	include("sidemenu.php");
	?>
	</td>
		<td width="700" class="fs12" valign="top">
			<table width="680" cellpadding="1" cellspacing="1" border="0"><?php
			if(strtoupper(substr(trim($errmsg),0,8))== "YOU HAVE")
			{
				echo "<tr><td colspan='2'><hr /></td></tr>";
				echo "<tr valign='top' align='center'><td colspan='4'>".$errmsg."</td></tr>";
				?>
				<meta http-equiv="Refresh" content="3;url=student.php?sID=<?=$studentID?>"><br/><br/><br/><br/><br/>
			<?php
			}
			else
			{
				?>
			<form action="orderStudent.php?oID=<?=$detID?>&sID=<?=$studentID?>" method="post" name="frm">
				<input type="hidden" name="detID" value="<?=$detID?>" />
				<tr><td width="40%" height="10"></td><td width="60%" height="10"></td></tr>
				<tr valign="top"><td colspan="2"><hr /></td></tr>
				<tr><td align="right">DATE ORDERED:&nbsp;</td>
				
					<td><select name="byear" class="formYear">
					<?php
					
					for($i=$frDate;$i<=$toDate;$i++)
					{
						if($byear==$i)
						{
							echo '<option value='.$i.' selected>'.$i.'</option>';
						}
						else
						{
							echo '<option value='.$i.'>'.$i.'</option>';
						}
					}
					?>
					</select>&nbsp;
						<select name="bmonth" class="formDDMM">
						<?php
					
					for($i=1;$i<=12;$i++)
					{
						if($bmonth==$i)
						{
							echo '<option value='.$i.' selected>'.$i.'</option>';
						}
						else
						{
							echo '<option value='.$i.'>'.$i.'</option>';
						}
					}
					?>
					</select>&nbsp;
						<select name="bday" class="formDDMM">
					<?php
					for($i=1;$i<=31;$i++)
					{
						if($bday==$i)
						{
							echo '<option value='.$i.' selected>'.$i.'</option>';
						}
						else
						{
							echo '<option value='.$i.'>'.$i.'</option>';
						}
					}
					?>
				</select></td></tr>
				<tr><td align="right">ITEM ORDERED:&nbsp;</td>
					<td><select name="menuSel" class="formSelect">
					<?php
					$strSql1 =@mysql_query("select menu_id,menu_code,menu_name from tr_menu where status=1 and menu_type=1");
					$cntr1=@mysql_num_rows($strSql1);
					if($cntr1>0)
					{
						while($rs1=@mysql_fetch_array($strSql1))
						{
							

							if($menuSel==$rs1["menu_id"])
							{
								echo '<option value='.$rs1["menu_id"].' selected>'.$rs1['menu_name'].'('.$rs1['menu_code'].')</option>';
							}
							else
							{
								echo '<option value='.$rs1["menu_id"].'>'.$rs1['menu_name'].'('.$rs1['menu_code'].')</option>';

							}
							
						}
					}
					?>
					</select></td></tr>
				<tr><td align="right">ITEM TYPE:&nbsp;</td>
					<td><select name="itemSel" class="formSelect">
					<?php
					$strSql2 =@mysql_query("select itemtype_id,item_type from ma_itemtype where status=1 order by orderby");
					$cntr2=@mysql_num_rows($strSql2);
					if($cntr2>0)
					{
						while($rs2=@mysql_fetch_array($strSql2))
						{
							
							if($itemSel==$rs2['itemtype_id'])
							 echo "<option value='".$rs2['itemtype_id']."' selected>".$rs2['item_type']."</option>";
						  else
							 echo "<option value='".$rs2['itemtype_id']."' >".$rs2['item_type']."</option>";
						}
					} 
					?></select></td></tr>
				<?php
				if($snakSel!='0')
				{
				?>
				<tr><td align="right">SNACKS:&nbsp;</td>
					<td><select name="snakSel" class="formSelect">
					<?php
					$strSql3 =@mysql_query("select menu_id,menu_code,menu_name from tr_menu where status=1 and menu_type=2");
					$cntr3=@mysql_num_rows($strSql3);
					echo "<option value='0'>--Any Snack--</option>";
					if($cntr3>0)
					{
						while($rs3=@mysql_fetch_array($strSql3))
						{
							

							if($snakSel==$rs3['menu_id'])
							 echo "<option value='".$rs3['menu_id']."' selected>".$rs3['menu_name']."(".$rs3['menu_code'].")</option>";
						  else
							 echo "<option value='".$rs3['menu_id']."' >".$rs3['menu_name']."(".$rs3['menu_code'].")</option>";
						}
					}
					?>
					</select></td></tr>
				<?php
				}
				if($drinkSel!='0')
				{
				?>
				<tr><td align="right">DRINKS:&nbsp;</td>
					<td><select name="drinkSel" class="formSelect">
					<?php
					$strSql4 =@mysql_query("select menu_id,menu_code,menu_name from tr_menu where status=1 and menu_type=3");
					$cntr4=@mysql_num_rows($strSql4);
					echo "<option value='0'>--Any Drinks--</option>";
					if($cntr4>0)
					{
						while($rs4=@mysql_fetch_array($strSql4))
						{
							
							if($drinkSel==$rs4['menu_id'])
							 echo "<option value='".$rs4['menu_id']."' selected>".$rs4['menu_name']."(".$rs4['menu_code'].")</option>";
						  else
							 echo "<option value='".$rs4['menu_id']."' >".$rs4['menu_name']."(".$rs4['menu_code'].")</option>";
						}
					}
					?></select></td></tr>
				<?php
				}
				?>
				<tr><td></td><td><input class="fs12" type="submit" name="action" value="UPDATE" /></td></tr>
			</form>
			<?php
			}
			?>
				<tr><td colspan="2"><hr /></td></tr>
				<tr><td colspan="2">&nbsp;</td></tr>
			</table></td></tr>
</table>
<?php
include("footer.php");
}
?>