<?php
	//headers
	include ('/var/www/html/uclit/protected/common/login/check.php');
	include ('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_arlstats.php');

	$loggedinuser = $_SESSION['username'];
	$status = $_GET['status'];
	//echo "LOGGED IN USER: $loggedinuser";

	// USE equipment database
	$db = mysql_select_db('uclit_db', $uclit_dbConn) or die ("Unable to select uclit_db database.");

	//Personnel Names
	$sql_personnel = "SELECT personnel_id, last_name, first_name, middle_name FROM personnel ORDER BY last_name";
	$employee_result = @mysql_query($sql_personnel, $uclit_dbConn) or die ("Unable to query: $sql_personnel");


	//USE arlstats database
	$db = mysql_select_db('arlstats', $arlConn) or die ("Unable to select arlstats database.");
	//Material Types
	$sql_material = "SELECT MID, Name,Priority, USERNAME FROM Material_Types ORDER BY Priority";
	$material_result = @mysql_query($sql_material, $arlConn) or die ("Unable to query: $sql_material");

	//Calendar
	$sql_calendar = "Select QID, Month, date_month, date_year From Calendar Where (date_year='2008' or date_year='2009') Order By Calendar.date_year Asc, Calendar.date_month Asc";
	$calendar_result = @mysql_query($sql_calendar, $arlConn) or die ("Unable to query: $sql_calendar");
	
	//Locations
	function location_query()
	{
		global $arlConn;
		global $location_result;
	
	 	$sql_location = "SELECT * from Location Order By Priority Asc";
	 	$location_result = @mysql_query($sql_location, $arlConn) or die ("unable to query: $sql_location");
	}
	location_query();
	
	// Catalog Types
	function catalog_query() 
	{
		global $arlConn;
		global $catalog_result;
	
	  	$sql_location = "SELECT * from Catalog_Types";
	  	$catalog_result = @mysql_query($sql_location, $arlConn) or die ("unable to query: $sql_location");
    }
	catalog_query();
	
	// UID checking
	$sql_uidcheck = "SELECT * from Location";
	$uid
	
	// Begin HTML HEADER output
?>
<h1>Enter Collection Statistics</h1>
<form action="menu_preview.php" method="post">
  <table width='90%' border='1' cellpadding='1'>
    <tr>
      <td>
        <select name="month">
<?php
	while($cal_row = @mysql_fetch_array($calendar_result))
	{	
		$qid = $cal_row['QID'];
		$qmonth = $cal_row['date_month'];
		$qyear = $cal_row['date_year'];
		$qname = $cal_row['Month']; 
		$sel_m = date('m');
		$sel_y = date('Y');
		$sel_d = date('d');
		//echo "<option value='$qid'>$qname</option>";
		
		echo "<option value='$qid'";
		if ("$status" == "preview")
		{
			$prev_q = $_POST[month];
			if ($qid == $prev_q)
				{echo "selected";}
		}
		else
		
		if ($sel_d <='7')
		{
		$sel_m = $sel_m-1;
		if ($sel_m == '0'){
			$sel_y = $sel_y-1;
			$sel_m = '12';
			}
		}
		if ($sel_m == $qmonth && $sel_y == $qyear)
		{
		if ("$status" != "preview")
		{echo "selected";}
		}		
		echo">$qname</option>";
		
	}
				
?>
        </select>
      </td>

<!--  <td>
        <select name="employee">
<?php
//personnel_id, last_name, first_name,
	while($row = @mysql_fetch_array($employee_result))
	{	
		$personnel_id = $row['personnel_id'];
		$last_name = $row['last_name'];
		$first_name = $row['first_name'];
		$middle_name = $row['middle_name'];
		echo "<option value='$personnel_id'>$last_name, $first_name</option>";
	}
?>
        </select>
      </td>
-->
      <td>
        <select name="material">
<?php
	while($row = @mysql_fetch_array($material_result))
	{	
		$mid = $row['MID'];
		$mname = $row['Name']; 
		$muserstring = $row['USERNAME'];
		$muserarray = split("\+", $muserstring);
		
		echo "<option value='$mid' ";
		if ("$status" == "preview")
		{
			$prev_m = $_POST[material];
			if ($mid == $prev_m)
				{echo "selected";}
		}
		else
		if (in_array($loggedinuser, $muserarray))
			if ("$status" != "preview")
			{echo "selected";}

		echo">$mname</option>";
	}
?>
        </select>
      <td>
	  <?php
	  if ("$status" == "preview")
		{
		$prev_s = $_POST[stype];
		echo"<input type=\"radio\" name=\"stype\" value=\"1\" "; if ($prev_s == "1")
				{echo "checked";} echo"/> Current ";
        
        echo"<input type=\"radio\" name=\"stype\" value=\"2\" "; if ($prev_s == "2")
				{echo "checked";} echo"/> Retro ";
       
        echo"<input type=\"radio\" name=\"stype\" value=\"3\" "; if ($prev_s == "2")
				{echo "checked";} echo"/> Retro GovDocs";
       
		}
		else
	  	echo"<input type=\"radio\" name=\"stype\" value=\"1\" checked />
        Current 
        <input type=\"radio\" name=\"stype\" value=\"2\" />
        Retro 
        <input type=\"radio\" name=\"stype\" value=\"3\" />
        Retro GovDocs";
	    ?>
	 
<!--    <input type="radio" name="stype" value="4" />
        ARL-->
	  </td>
    </tr>
  </table>
<?php 

// STATUS check
	
	if ("$status" == "saved")  
		echo "<strong><font color='#0000FF'>VALUES SAVED</font></strong><br>"; 
	else
	{ 
	}
	echo "<p>Enter Values Below, then press 'Preview' &nbsp;</p>";
?>

<!-- Catalog Type - Location Table -->
  <table width='90%' border='1' cellpadding='1'>
    <tr> 
	  <td width='15%'>&nbsp;</td>
<?php //table header
	while($l_row = @mysql_fetch_array($catalog_result))
    {
	   	$name = $l_row['Name'];
	   	$l_description = $l_row['Description'];

      	echo "<td width='12%'>$name</td>"; 
	} 
catalog_query();  //rebuild catalog query row
?>
    </tr>

<?php //outer loop for each row of the table
	while($row = @mysql_fetch_array($location_result))
	{
	  	$lid = $row['LID'];
	  	$location_name = $row['Name'];
	  	$location_description = $row['Description'];
      	$userstring = $row['UID'];
	  	$short_description = $row['ShortDesc'];
	  	//echo $userstring;
	  	$userarray = split("\+",$userstring);
	  	//echo "array: $userarray[0]";

		//start login user if 
		if (in_array($loggedinuser,$userarray)) 
		{ 

			echo "<tr> 
      				<td nowrap>$location_name (<font color='#FF0000'>$short_description</font>)</td>";
	  
 			//inner loop for each column of the row
	  		while($row2 = @mysql_fetch_array($catalog_result))
	  		{
				//echo "boola";
				$cid=$row2['CID'];

	  			echo "<td><input name='$location_name$cid' type='text' value=";
				if ("$status" == "preview")
				{
				$entry = $_POST[$location_name.$cid];
				 
				if ($entry)
				echo "'$entry' size='5'></td>";
				else
				echo"'0' size='5'></td>";			
				}
				else
				echo"'0' size='5'></td>"; 	
	  		}
?>
    </tr>

<?php 
		} //end login user if loop
	catalog_query();
	}
	echo "<tr>
		<td width='15%'>&nbsp;</td>";
 	//table header
	while($l_row = @mysql_fetch_array($catalog_result))
    {
	   	$name = $l_row['Name'];
	   	$l_description = $l_row['Description'];
      	echo "<td width='12%'>$name</td>";
	}
?>
	</tr>	
</table>	<br />
	<input type="submit" name="Submit" value="Preview Values">
	</form>
<p>&nbsp;</p>
<?php
	include ('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
?>