<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<?php
include("functions.php");
include("login.php");

	//verifying  session for adm
	session_start();
	if (isset($_SESSION['loginADM']))
	{
		$username = $_SESSION['loginADM'];
	}
	else{
		header("location: loginADM.php");
	}


	//processing form in case of submit button pressed to UPDATE FOOD
if (isset($_POST['submitInformation'])){
	$FdGrp_Cd = sanitizeMySQL($_POST['FdGrp_Cd']);
	$Long_Desc = sanitizeMySQL($_POST['FdGrp_Cd2']);
	$n203 = sanitizeMySQL($_POST['n203']);
	$n204 = sanitizeMySQL($_POST['n204']);
	$n205 = sanitizeMySQL($_POST['n205']);
	$n255 = sanitizeMySQL($_POST['n255']);
	$n291 = sanitizeMySQL($_POST['n291']);
	
	if ($n203 == ""){
	$n203 = 0; 
	}
	if ($n204 == ""){
	$n204 = 0; 
	}
	if ($n205 == ""){
	$n205 = 0; 
	}
	if ($n255 == ""){
	$n255 = 0; 
	}
	if ($n291 == ""){
	$n291 = 0; 
	}
	
	//updating information
	$query = 'UPDATE `food_des` SET `203` = '.$n203.' , `204` = '.$n204.' , `205` = '.$n205.' , `255` = '.$n255.' , `291` = '.$n291.' WHERE NDB_No = "'.$Long_Desc.'" ';	
	$result = mysqli_query($db_connection, $query);
	if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
	echo "<script language='javascript'>alert('Update completed');</script>";
}

	//initializing variables
	$categorie = "0100";
	$food = "1001";

//getting the categorie selected
if(isset($_POST["FdGrp_Cd"])){
	$categorie = $_POST["FdGrp_Cd"];
}

//getting the food selected
if(isset($_POST["FdGrp_Cd2"])){
	$food = $_POST["FdGrp_Cd2"];
}	
?>
<script language="javascript" type="text/javascript">

//update the form when a categorie is pressed
function enviar() {
	document.informationForm.submit();
}

//set class name to focus (normal style to describe non-error)
function setStyleFocus(element)
{
	element.className="focus";
}

</script>
<link rel="stylesheet" type="text/css" href="format.css"></head>
<body >

<!--The upper div creates the upper green block in the top of the webpage  -->
<div  id="upper" style="position:fixed;  top: 0; left:0;">

    <!--creates the 'Logout' button -->
    <div style="position:relative;text-align:left;float: left;"> <input type="button" value="Logout" onclick="location.href='logout.php'" /> </div>
    <!--creates the 'Next Page and Register new Food' buttons -->
    <div align="right" style="position:relative;text-align:right;float:right;"><input name="nextPage" type="button" value="Register new ADM" onclick="location.href='registerADM.php'" style=" margin-right:25px"/><input name="updateFood" type="button" value="Register new Food" onclick="location.href='indexADM.php'" style=" margin-right:25px"/></div>

</div>

<!--The information div creates the green block in the center of the webpage  -->
	<div id="information" style=" width: 60%">
	  <form id="informationForm" name="informationForm" method="post" action="">
  <b> Update Food Information</b><br />
	      <br />
        <label for="FdGrp_Cd">Food Group<br />
        </label>
        
        <select size="1" onfocus="setStyleFocus(this)"  name="FdGrp_Cd" id="FdGrp_Cd" style="width:99%;height:100%" onchange="enviar()">
		<?php
		
		//creating options with group categories
		$query = 'SELECT * FROM fd_group ';
		$result = mysqli_query($db_connection, $query);
		if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
		while($row = mysqli_fetch_assoc($result))
		{
				echo '<option  value="'.$row['FdGrp_CD'].'" onclick="enviar()" >'.$row['FdGrp_Desc'].'</option>';
		}
		?>
        
        </select>
        <br /><br />

        <label for="FdGrp_Cd2">Food Description<br />
        </label>
        <select size="1" onfocus="setStyleFocus(this)"  name="FdGrp_Cd2" id="FdGrp_Cd2" style="width:99%;height:100%" onchange="enviar()">
          <?php
		  
		  	//creating options with foosd in the categorie selected
			$query = 'SELECT * FROM food_des WHERE FdGrp_Cd = "'.$categorie.'" ';
			$result = mysqli_query($db_connection, $query);
			if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
			while($row = mysqli_fetch_assoc($result))
			{
					echo '<option  value="'.$row['NDB_No'].'"  onclick="enviar()">'.$row['Long_Desc'].'</option>';
			}
		?>
        </select>
<br />
        <br />
        Nutricional information<br /><br />

        <table align="center">
        <?php
			
			//getting food information
			$query = 'SELECT * FROM food_des WHERE NDB_No = "'.$food.'" ';
			$r = mysqli_query($db_connection, $query);
			if (!$r) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
			$r = mysqli_fetch_assoc($r);
		
			//getting nutrients information
			$query = 'SELECT * FROM nutr_def WHERE Nutr_No = 203 OR Nutr_No = 204 OR Nutr_No = 205 OR Nutr_No = 255 OR Nutr_No = 291';
			$result = mysqli_query($db_connection, $query);
			if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
			//creating input boxes
			while($row = mysqli_fetch_assoc($result))
			{
				
				$value=$r[$row['Nutr_No']];
				
				echo '<tr><td align="right" >'.$row['NutrDesc'].' ('.$row['Units'].')</td>
					  <td><input type="number" step="any" value="'.$value.'"  id=n'.$row['Nutr_No'].' name=n'.$row['Nutr_No'].' ></td>';
				$row = mysqli_fetch_assoc($result);	 
				
				if($row!=''){ 
				
				$value=$r[$row['Nutr_No']];
			    echo  '<td align="right" >'.$row['NutrDesc'].' ('.$row['Units'].')</td>
					  <td><input type="number" step="any" value="'.$value.'"   id=n'.$row['Nutr_No'].' name=n'.$row['Nutr_No'].' ></td></tr>';
				}
			}
		?>
        </table>
        <br /><br />

<input type="submit" name="submitInformation" onfocus="setStyleFocus(this)"  id="submitInformation" value="Save"   style="width:46%"/>
<br />
      </form>
	</div>
</body>
</html>
<?php 

echo "<script  language='javascript'> 
	document.getElementById('FdGrp_Cd').value = '".$categorie."';
	document.getElementById('FdGrp_Cd2').value = '".$food."';

</script>"

?>