<?php
	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_equip.php');

	//Select a database
	$db = mysql_select_db('equipment', $equipConn) or die("Couldn't select equipment database");
		
	$sql_comptype = "SELECT DISTINCT computer_type FROM computers ORDER BY computer_type";
	$comptype_result = mysql_query($sql_comptype, $equipConn) or die ("Unable to query: $sql_comptype");
?>
	<h1>Query Computer Builds</h1><br />
	<form name='computer_build' method='post' action='computer_build.php'>
	<table border='1'>
	  <tr>
		<td nowrap rowspan='2'>Computer Build:<br />
		  <select name='build'>
		  	<option value='0' selected>Select...</option>
<?php
			while($comp_row = mysql_fetch_array($comptype_result))
			{	
				$comp = $comp_row['computer_type'];
				echo "<option value='$comp' >$comp</option>";
			}
?> 
		  </select>
		</td>
		<td nowrap>
			<input type='checkbox' name='public' value='false'>Public
		</td>
		<td nowrap>
			<input type='checkbox' name='staff' value='false'>Staff
		</td>
		<td nowrap>
			<input type='checkbox' name='specialservices' value='false'>Special Services
		</td>
		<td nowrap>
			<input type='checkbox' name='student' value='false'>Student
		</td>
	</tr>
	<tr>	
		<td nowrap>
			<input type='checkbox' name='servicedesk' value='false'>Service Desk
		</td>
		<td nowrap>
			<input type='checkbox' name='info' value='false'>InfoCommons
		</td>
		<td nowrap>
			<input type='checkbox' name='printing' value='false'>Printing
		</td>
		<td nowrap>
			<input type='checkbox' name='other' value='false'>Other
		</td>
	  </tr>
	  <tr>
	  	<td colspan='6' align='center'>
			<input type='hidden' name='ch' value='0'>	
			<input type='submit' name='send' value='Computer Build' onClick='CheckInput()'>	
		</td>
	  </tr>
	</table>
</form>

<?php
//this code happens after submited information from the form
	if($_POST['ch'] != "0" || $_POST['ch'] != null || !isset($_POST['ch']));
	{
		$arr = array(8);
		$mydata = array(8);
		$ch = $_POST['ch'];
		$b = $_POST['build'];
		$mydata[0] = $_POST['student']; // data from post array
		$mydata[1] = $_POST['public'];
		$mydata[2] = $_POST['staff'];
		$mydata[3] = $_POST['info'];
		$mydata[4] = $_POST['other'];
		$mydata[5] = $_POST['servicedesk'];
		$mydata[6] = $_POST['specialservices'];
		$mydata[7] = $_POST['printing'];
		
		$arr[0] = "STUDENT";
		$arr[1] = "PUBLIC";
		$arr[2] = "STAFF";
		$arr[3] = "INFOCOMMONS";
		$arr[4] = "OTHER";
		$arr[5] = "SERVICE DESK";
		$arr[6] = "SPECIAL SERVICES";
		$arr[7] = "PRINTING";
?>
		<table border='1'>
		<tr>
			<td nowrap align='center'><strong>Department</strong></td>
			<td nowrap align='center'><strong>Personnel</strong></td>
			<td nowrap align='center'><strong>Room</strong></td>
			<td nowrap align='center'><strong>Catagory</strong></td>
			<td nowrap align='center'><strong>Station ID</strong></td>
			<td nowrap align='center'><strong>CPU Speed</strong></td>
		</tr>
<?php
		for($a = 0; $a<8; $a++)
		{
			if($mydata[$a] == "true")
			{
				if($b != 0)
					$sql_com = "SELECT workstation_id, computer_serial, cpu_type_speed FROM computers WHERE computer_type='$b' and catagory='$arr[$a]'";
				else
					$sql_com = "SELECT workstation_id, computer_serial, cpu_type_speed FROM computers WHERE catagory='$arr[$a]'";	
				if($mydata[$a] == "true" && $a == 4)
					$sql_com = "SELECT workstation_id, computer_serial, cpu_type_speed FROM computers WHERE catagory= ''";		
				$com_result = @mysql_query($sql_com, $equipConn) or die ("Unable to query: $sql_com");
				$cs = "";
				$cst = "";	
				$st = "";
				$per = "";
				$fn = "";
				$ln = "";
				$did = "";
				$dname = "";
				$room = "";
				while($c = mysql_fetch_array($com_result))
				{
					$cs = $c['computer_serial'];
					$cst = $c['cpu_type_speed'];
					$st = $c['workstation_id'];
					//get info from computer serial num usin equipment_detail table
					$sql_per = "SELECT computer_serial, personnel_id FROM equipment_detail WHERE computer_serial='$cs'";
					$per_result = @mysql_query($sql_per, $equipConn) or die ("Unable to query: $sql_per");
					while($c2 = @mysql_fetch_array($per_result))
					{
						$per = $c2['personnel_id'];
					}
					//get personnel info
					$db = mysql_select_db('uclit_db', $uclit_dbConn) or die("Couldn't select uclit_db database");
					$sql_per_info = "SELECT personnel.first_name,personnel.last_name,personnel.room, personnel.dept_id, departments.dept_name FROM personnel, departments WHERE personnel_id='$per' AND personnel.dept_id = departments.dept_id";
					$per_info_result = mysql_query($sql_per_info, $uclit_dbConn) or die ("Unable to query: $sql_per_info");
					while($c3 = mysql_fetch_array($per_info_result))
					{
						$fn = $c3['first_name'];
						$ln = $c3['last_name'];
						$did = $c3['dept_id'];
						$dname = $c3['dept_name'];
						$room = $c3['room'];
					}
					echo "<tr>";
								if($dname != "")
									echo "<td>$dname</td>";
								else
									echo "<td>&nbsp</td>";	
								if($fn != "" || $ln != "")	
									echo "<td>$fn $ln</td>";
								else
									echo "<td>&nbsp</td>";		
								if($room != "")	
									echo "<td>$room</td>";
								else
									echo "<td>&nbsp</td>";		
								if($arr[$a] != "")	
									echo "<td>$arr[$a]</td>";
								else
									echo "<td>&nbsp</td>";		
								if($st != "")	
									echo "<td>$st</td>";
								else
									echo "<td>&nbsp</td>";		
								if($cst != "")	
									echo "<td>$cst</td>";
								else
									echo "<td>&nbsp</td>";		
					echo "</tr>";
					
				}//end while loop for per_info
			}//end if statement
		}//end for loop
		echo "</table>";
	}
	include ('/var/www/html/uclit/protected/common/css_login/css_foot2.php');
?>
<script type='text/javascript'>
	var x = document.computer_build;
	x.ch.value = '0';
	x.other.value = 'false';
	x.student.value = 'false';
	x.staff.value = 'false';
	x.printing.value = 'false';
	x.specialservices.value = 'false';
	x.servicedesk.value = 'false';
	x.info.value = 'false';
	x.public.value = 'false';

	function CheckInput()
	{
		var x = document.computer_build;
		var count = 0;
		if(x.staff.checked == true)
		{
			x.staff.value = "true";
			count++;
		}
		if(x.student.checked == true)
		{
			x.student.value = "true";	
			count++;
		}
		if(x.public.checked == true)
		{
			x.public.value = "true";	
			count++;
		}
		if(x.specialservices.checked == true)
		{
			x.specialservices.value = "true";	
			count++;
		}
		if(x.servicedesk.checked == true)
		{
			x.servicedesk.value = "true";
			count++;
		}
		if(x.info.checked == true)
		{
			x.info.value = "true";	
			count++;
		}
		if(x.other.checked == true)
		{
			x.other.value = "true";	
			count++;
		}
		if(x.printing.checked == true)
		{
			x.printing.value = "true";	
			count++;
		}
		if(x.build[0].selected && count == 0)
		{
			window.alert("Please select a computer build type\nor Check at least one Catagory");
		}
		else
		{
			x.ch.value = "1";
			x.submit();
		}
	}
</script>