<?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_help.php');
		
	//Select a database
	$db = mysql_select_db('uclit_db', $uclit_dbConn) or die("Couldn't select uclit_db database ");

	$help_id = $_POST['help_id'];
	$help_user_id = $_POST['help_user_id'];
	$help_user = $_POST['help_user'];
	$help_tech = $_POST['technician'];
	$help_catagory = $_POST['help_catagory'];
	$help_dept = $_POST['depttext'];
	$help_desc = $_POST['desctext'];
	$help_email = $_POST['user_email'];
	
	$last_name = explode(" ", $help_tech);

	$today=date("Y-m-d");
	
	//get user's name
	if($help_user_id != "" && $help_user_id != '0')
	{
		$query_name = "SELECT first_name, last_name, e_mail FROM personnel WHERE personnel_id = '$help_user_id'";
		$sql_result = mysql_query($query_name, $uclit_dbConn) or die("Couldn't execute query. $query_name");
		$help_user = "";
		while ($row = mysql_fetch_array($sql_result))
		{
			$help_user = $row['first_name'];
			$help_user .= " ";
			$help_user .= $row['last_name'];
			$pers_email = $row['e_mail'];
		}
	}
	else
	{
		$help_user = "Student/Other";			
	}
	
	// switch to choose the correct email address per help call category
	switch($help_catagory)
	{
			case "Equipment Services":
				$tech_email = "dozierpl@uc.edu";
				$staff_tech = "Paul Dozier";
				break;
		   
		   case "Application Services":
				$tech_email = "scherztc@ucmail.uc.edu";
				$staff_tech = "Thomas Scherz";
				break;
				
		   case "Web Services":
				$tech_email= "ponslm@ucmail.uc.edu";
				$staff_tech = "Lisa Pons";
				break;
						
			case "Database Services":
				$tech_email = "hillerkj@ucmail.uc.edu; campbejo@ucmail.uc.edu";
				$staff_tech = "Karl Hiller";
				break;
		
			case "Telco Services":
				$tech_email = "fritscej@ucmail.uc.edu";
				$staff_tech = "Erma Fritsche";
				break;				
			
			case "System Services":
				$tech_email = "gavinds@ucmail.uc.edu";
				$staff_tech = "Dennis Gavin";
				break;				
			
			case "User Services":
				$tech_email = "mahoneme@ucmail.uc.edu";
				$staff_tech = "Michael Mahoney";
				break;					
			
			case "":
				$tech_email = "scherztc@ucmail.uc.edu";
				$staff_tech = "Thomas Scherz";
				break;
	}
	// check if a technician is selected
	if ($help_catagory != '' && $help_tech == '')
	{	
		// update log with help_catagory, help_tech, and help_user_id
		$db = mysql_select_db('helpcalls', $helpConn) or die("Couldn't select helpcalls database");
		$query = "UPDATE log
				  SET Help_Catagory='$help_catagory', Help_Tech='$staff_tech'
				  WHERE Help_ID='$help_id'";
		$query_result = mysql_query($query, $helpConn) or die("Couldn't execute query. $query");
		
		// email tech with helpcall
		$mail_to = $tech_email;
		$mail_subj = "Library Technology Services (Help Call Posted)";
		$mail_text = "$staff_tech You have new help call.  Please check the database for it. 
			&nbsp;Thanks a lot!\n\nLibrary Information Technology Services.";
		$mail_header = "From: libhelp@uc.edu\r\n";
		$mail_header .= "X-Mailer: server\r\n";
		$mail_header .= "Reply To: libhelp@uc.edu\r\n";
		$sendmail_param = "libhelp@uc.edu";
	
		mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
	
		$sentmsgTech = "true";
	}
	// check if both a technician and a category were selected
	elseif ($help_catagory != '' && $help_tech != '')
	{
		// query database for staff name and retrieve email.
		$db = mysql_select_db('uclit_db', $uclit_dbConn) or die ("Couldn't select uclit_db database");
		$query = "SELECT e_mail, first_name, last_name
				  FROM personnel 
				  WHERE first_name = '$last_name[0]' AND last_name = '$last_name[1]'";
		$sql_result = mysql_query($query, $uclit_dbConn) or die("Couldn't execute query. $query");
		while ($row = mysql_fetch_array($sql_result))
		{
			$tech_email = $row['e_mail'];		
			$tech = $row['first_name'];
			$tech .= " ";
			$tech .= $row['last_name'];
		}
		// update log with help_catagory, help_tech, and help_user_id
		$db = mysql_select_db('helpcalls', $helpConn) or die("Couldn't select helpcalls database");
		$query = "UPDATE log
				  SET Help_Catagory='$help_catagory', Help_Tech='$tech'
				  WHERE Help_ID='$help_id'";
		$query_result = mysql_query($query, $helpConn) or die("Couldn't execute query. $query");
		
		// email staff with helpcall
		$mail_to = $tech_email;
		$mail_subj = "Library Technology Services (Help Call Posted)";
		$mail_text = "$tech You have new help call.  Please check the database for it. 
			&nbsp;Thanks a lot!\n\nLibrary Information Technology Services.";
		$mail_header = "From: libhelp@uc.edu\r\n";
		$mail_header .= "X-Mailer: server\r\n";
		$mail_header .= "Reply To: libhelp@uc.edu\r\n";
		$sendmail_param = "libhelp@uc.edu";
	
		mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
	
		$sentmsgTech = "true";
	}	
	
	echo "<b>Help Catagory</b>: $help_catagory <br />
		<b>Help User</b>: $help_user<br />
		<b>Help Department</b>: $help_dept <br />
		<b>Help Description</b>: $help_desc <br />";

	if ($help_tech != '')
		echo "<br /><b>Technician</b>: $help_tech <br />";
	else
		echo "<br /><b>Technician</b>: $staff_tech <br />";
	if ($sentmsgTech == "true")
		echo "<b>Tech Email</b>: $tech_email<strong>(email sent)</strong>";
	echo "<br /><br />";	

	echo "<a href='http://ul.uc.edu/uclit/protected/itstaff/sort_helpcalls.php'>Select another Helpcall</a>";
		
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
?>