<?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');
	
	$db = mysql_select_db('equipment', $equipConn) or die ("Unable to select equipment database.");	
	    	
	//format date 2003-05-06 13:20:59
	$datetimenow = date('Y-m-d')." ".strftime("%H:%M:%S");
	
	if(isset($_POST['personnel']) && isset($_POST['pc_serial']) && isset($_POST['m_serial']) && isset($_POST['p_serial']) && isset($_POST['vdjacks']) && isset($_POST['voicejacks']))
	{
		//get date sent from show_assignequipt.php
		$pid = $_POST['personnel'];
		$pc_serial = $_POST['pc_serial'];
		$mserial = $_POST['m_serial'];
		$pserial = $_POST['p_serial'];
		$vdjack_id = $_POST['vdjacks'];
		$voicejack_id = $_POST['voicejacks'];
		
		$displayblock = "<table border='1' cellspacing='2' cellpadding='2'>
						   <tr>
							 <td align='center'>Detail ID</td>
							 <td align='center'>Employee ID</td>
							 <td align='center'>PC Serial</td>
							 <td align='center'>Monitor Serial</td>
							 <td align='center'>Printer Serial</td>
							 <td align='center'>Data Jack ID</td>
							 <td align='center'>Voice Jack ID</td>
						   </tr>";
		//sql statement to check if equipment is already in use
		$sql_chk_puter = "SELECT * FROM equipment_detail WHERE computer_serial='$pc_serial' ";
		
		$sql_chk_mon = "SELECT * FROM equipment_detail WHERE monitor_serial='$mserial' ";
		
		$sql_chk_puter_result = mysql_query($sql_chk_puter, $equipConn) or die ("Unable to exercute query.<p>$sql_chk_puter</p>");
		$sql_chk_mon_result = mysql_query($sql_chk_mon, $equipConn) or die ("Unable to exercute query.<p>$sql_chk_mon</p>");
		
		if (mysql_fetch_row($sql_chk_puter_result) > 0)
		{
			$h2 = "Computer has already been assigned!<br>Please Choose a different computer";
			$sql_detail = "SELECT * FROM equipment_detail WHERE computer_serial='$pc_serial'";
			$sql_detail_result = mysql_query($sql_detail, $equipConn);
			while($rowPC = mysql_fetch_array($sql_detail_result))
			{
				$edid = $rowPC['detail_id'];
				$ekey = $rowPC['personnel_id'];
				$pckey = $rowPC['computer_serial'];
				$mkey = $rowPC['monitor_serial'];
				$pkey = $rowPC['printer_serial'];
				$vdjkey = $rowPC['jacks_id'];
				$voicekey = $rowPC['voicejacks'];
										
				$displayblock .= "<tr>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_relationdetail.php?edid=$edid&uaction=display'>$edid</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addemployee.php?pid=$ekey&uaction=edit'>$ekey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_pcspecs.php?computer_serial=$pckey'>$pckey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addmonitor.php?monitor_serial=$mkey&uaction=edit'>$mkey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addprinter.php?printer_serial=$pkey&uaction=edit'>$pkey</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addpdjack.php?jack_id=$vdjkey&uaction=edit'>$vdjkey</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addvjack.php?jack_id=$voicekey&uaction=edit'>$voicekey</a></td>
								  </tr>";					
			}
		}
		elseif (mysql_fetch_row($sql_chk_mon_result) > 0)
		{
			$h2 = "Monitor has already been assigned!<br>Please choose a different monitor.";
			$sql_detail = "SELECT * FROM equipment_detail WHERE monitor_serial='$mserial'";
			$sql_detail_result = mysql_query($sql_detail, $equipConn);
			while($rowMon = mysql_fetch_array($sql_detail_result))
			{
				$edid = $rowMon['detail_id'];
				$ekey = $rowMon['personnel_id'];
				$pckey = $rowMon['computer_serial'];
				$mkey = $rowMon['monitor_serial'];
				$pkey = $rowMon['printer_serial'];
				$vdjkey = $rowMon['jacks_id'];
				$voicekey = $rowMon['voicejacks'];
				
				$displayblock .= "<tr>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_relationdetail.php?pid=$edid&uaction=display'>$edid</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addemployee.php?pid=$ekey&uaction=edit'>$ekey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_pcspecs.php?computer_serial=$pckey'>$pckey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addmonitor.php?monitor_serial=$mkey&uaction=edit'>$mkey</a></td>
				  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addprinter.php?printer_serial=$pkey&uaction=edit'>$pkey</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addpdjack.php?jack_id=$vdjkey&uaction=edit'>$vdjkey</a></td>
				  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addvjack.php?jack_id=$voicekey&uaction=edit'>$voicekey</a></td>
								  </tr>";
			}
		}
		else
		{
			$h2 = 'Equipment is Added to Equiptment Detail Table';
			
			//computer and monitor hasn't been assigned add to equipment detail table
			$sql_link_equipt = "INSERT INTO equipment_detail (detail_id, personnel_id, computer_serial, monitor_serial, printer_serial, jacks_id, voice_id) VALUES ('', '$pid', '$pc_serial', '$mserial', '$pserial', '$vdjack_id', '$voicejack_id')";
			
			if($sql_result = mysql_query($sql_link_equipt, $equipConn)) //
			{				
				//log user access
				$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql_link_equipt', '$datetimenow')";
				mysql_query($sql_useraccess, $equipConn);
			
				$sql_detail2 = "SELECT * FROM equipment_detail WHERE computer_serial='$pc_serial'";
				$sql_detail2_result = mysql_query($sql_detail2, $equipConn);
				while($rowPC = mysql_fetch_array($sql_detail2_result))
				{
					$edid = $rowPC['detail_id'];
					$ekey = $rowPC['personnel_id'];
					$pckey = $rowPC['computer_serial'];
					$mkey = $rowPC['monitor_serial'];
					$pkey = $rowPC['printer_serial'];
					$vdjkey = $rowPC['jacks_id'];
					$voicekey = $rowPC['voice_id'];
										
					$displayblock .= "<tr>
					  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_relationdetail.php?edid=$edid&uaction=display'>$edid</a></td>
					  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addemployee.php?pid=$ekey&uaction=edit'>$ekey</a></td>
					  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_pcspecs.php?computer_serial=$pckey'>$pckey</a></td>
					  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addmonitor.php?monitor_serial=$mkey&uaction=edit'>$mkey</a></td>
					  					<td><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addprinter.php?printer_serial=$pkey&uaction=edit'>$pkey</a></td>
					  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addpdjack.php?jack_id=$vdjkey&uaction=edit'>$vdjkey</a></td>
					  					<td align='center'><a href='http://ul.uc.edu/uclit/protected/itstaff/show_addvjack.php?jack_id=$voicekey&uaction=edit'>$voicekey</a></td>
									  </tr>";					
				}	
			}
			else //error while adding 
			{
				echo "<strong><font color='red'>". mysql_errno() ." ..--.. ". mysql_error() ."</font></strong>";
				exit;
			}			
		}
	}
	echo "  <h2><i>$h2</i></h2>
		    <br />
		    $displayblock
		  </table>
		  <br />
		  <a href='http://ul.uc.edu/uclit/protected/itstaff/show_allequiptdetail.php'>List All Equipment Details</a>";
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
?>