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

	//local variables
	$complete = $_POST['completebox'];
	$help_catagory = $_POST['help_catagory'];
	$help_user_id = $_POST['per'];
	$help_user = $_POST['username'];
	$help_dept = $_POST['help_dept'];
	$help_desc = $_POST['help_desc'];
	$help_res = $_POST['help_res'];
	$help_phone = $_POST['help_phone'];
	$help_email = $_POST['email'];
	if ($help_dept != "" and $help_user != "" and  $help_desc != "")
	{	
		$db_name = "helpcalls";
		$db_name2 = "equipment";
		$table_name = "log";
		$table_name2 = "personnel";
		$help_tech = "";
		$today=date("Y-m-d");

		//get user's name
		$db = mysql_select_db("uclit_db", $uclit_dbConn) or die("Couldn't select database. uclit_db");
		if($help_user_id != "")
		{
			$query_name = "SELECT first_name, last_name, e_mail FROM personnel WHERE personnel_id = '$help_user_id'";
			$sql_result = mysql_query($query_name, $helpConn) 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'];
				$help_email = $row['e_mail'];
			}
		}
		else
			$help_user = "Student/Other";
		
		switch($help_catagory)
		{
			case "Equipment Services":  
				$help_tech = "Paul Dozier";
				$email = "dozierpl@uc.edu";
				break;
		   
		   case "Application Services": 
		   		$help_tech = "Thomas Scherz";
				$email = "scherztc@ucmail.uc.edu";
				break;
		   
		 		
		   case "Telco Services":
				$help_tech = "Erma Fritsche";
				$stage1 = "$today";
				$email = "fritscej@ucmail.uc.edu";
				break;
				
		   case "Web Services":
				$help_tech = "Lisa Pons";
				$stage1 = "$today";
				$email= "ponslm@ucmail.uc.edu";
				break;
				
		  	
			
			case "Database Services": 
				$help_tech = "Jim Campbell";
				$email = "campbejo@ucmail.uc.edu";
				break;
		
			case "System Services": 
				$help_tech = "Dennis Gavin";
				$email = "dennis.gavin@uc.edu";
				break;
		
			case "User Services":
				$help_tech = "Michael Mahoney";
				$email = "mahoneme@ucmail.uc.edu";
				break;
		}
		
		if($completed == "false")
		{//send email to tech, staff, and student
			//emailing tech
			$mail_to = $email;
			$mail_subj = "Library Technology Services (Help Call Posted)";
			$mail_text = "You have new help call.  Please check your help call at: http://ul.uc.edu/departments/libit/help/staff/show_tech.php. Thanks you very much!\n\nLibrary 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");
			$sentmsgTech2 = "true";
			
			if($help_email != "")
			{//sending a repond email to the student/other as the selected user
				$mail_to = $help_email;
				$mail_subj = "Library Technology Services (Phone Ticket Posted)";
				$mail_text = "We received your help call as follow:\n$help_desc\n\nA technician will contact you ASAP. Thanks you very much!\n\nLibrary 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");
				$sentmsgStudent = "true";
			}
			else
			{//sending email to staff
				//get email from staff
				$db2 = mysql_select_db('uclit_db', $uclit_dbConn) or die("Couldn't select database. uclit_db");
				$query_id = "SELECT e_mail FROM personnel WHERE personnel_id = '$help_user_id'";
				$sql_result = mysql_query($query_id, $uclit_dbConn) or die("Couldn't execute query. $query_id");
				$email_staff = "";
				while ($row = mysql_fetch_array($sql_result))
				{
					$email_staff = $row['e_mail'];
				}
				if($email_staff != "")
				{
					$mail_to = $email_staff;
					$mail_subj = "Library Technology Services (Phone Ticket Posted)";
					$mail_text = "We received your help call as follow:\n$help_desc\n\nA technician will contact you ASAP. Thanks you very much!\n\nLibrary 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");
					$sentmsgStaff = "true";
				}
				else
				{
					$notSentmsgStaff = "true";
				}
			}
		}//end check not completed phone ticket
		else
		{//begin completed phone ticket, respond to only help user
			$mail_to = $help_email;
			$mail_subj = "Library Technology Services";
			$mail_text = "We received your help call as follow:\n$help_desc\nResolution:  $help_res\n. Thanks you very much!\n\nLibrary 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");
			$completedUser = "true";
		}//end completed phone ticket			
		$db = mysql_select_db('helpcalls', $helpConn) or die("Couldn't select database helpcalls.");
		if($help_user_id != "") 
		{//begin insert record if user is a student
			if($complete != "true")
			{//
				$sql = "INSERT INTO log
					(Completed, Help_Catagory, Help_User, Help_User_ID, Help_Dept, Help_Desc, Help_Tech, Date_Arrived, Help_Phone, Email)
					VALUES 
					('0','$help_catagory', '$help_user','$help_user_id','$help_dept','$help_desc','$help_tech','$today',
						'$help_phone','$help_email')";
			}
			else
			{//completed with a 1
					$sql = "INSERT INTO log 
					(Completed, Help_Catagory, Help_User, Help_User_ID, Help_Dept, Help_Desc, Help_Tech, Date_Arrived, Date_Completed, Help_Phone, Email)
					VALUES 
					('1', '$help_catagory', '$help_user','$help_user_id','$help_dept','$help_desc','$help_tech','$today', '$today',
						'$help_phone','$help_email')";

			}
		}//end insert student
		else
		{//begin insert staff
			if($complete != "true" )
			{
				$sql = "INSERT INTO log
					(Completed, Help_Catagory, Help_User, Help_User_ID, Help_Dept, Help_Desc, Help_Tech, Date_Arrived, Help_Phone, Email)
					VALUES 
					('0','$help_catagory', '$help_user','0','$help_dept','$help_desc','$help_tech','$today',
						'$help_phone','$help_email')";
			}
			else
			{//completed with a 1
				$sql = "INSERT INTO log
					(Completed, Help_Catagory, Help_User, Help_User_ID, Help_Dept, Help_Desc, Help_Tech, Date_Arrived, Date_Completed,Help_Phone, Email)
					VALUES 
					('1' ,'$help_catagory', '$help_user','0','$help_dept','$help_desc','$help_tech','$today','$today',
						'$help_phone','$help_email')";
			}
		}//end insert staff
		$datetimenow = date('Y-m-d')." ".strftime("%H:%M:%S");
		$result = mysql_query($sql, $helpConn) or die("Couldn't execute query.");
		$sql_viewed = "VIEWED  help_id='$help_id',  help_catagory='$help_catagory',  help_desc='$help_desc',  help_user='$help_user',  help_dept='$help_dept',  help_phone='$help_phone' ";
		//log user access
		$sql_useraccess = "INSERT INTO HelpcallsUserAccessLog (id, username, useraction, datetimestamp) VALUES ('', '$suser', '$sql_viewed', '$datetimenow')";
		mysql_query($sql_useraccess, $helpConn);
		$sql_useraccess = "INSERT INTO HelpcallsUserAccessLog (id, username, useraction, datetimestamp) VALUES ('', '$suser', '$sql', '$datetimenow')";
		mysql_query($sql_useraccess, $helpConn);
	}
	else
	{//begin roll back to previous page
		echo "<script language=javascript>
			history.back(-1);
		</script>";

	exit;
	}//end roll back to previous page
?>
	<td width=490 valign='top'> <h2>Phone ticket successfully added to log</h2>
    <hr /> 
<?php
	echo "Help Catagory: $help_catagory <br />
		  Help User: $help_user<br />
		  Help Department:$help_dept <br />
		  Help Description:$help_desc <br />";
		
	if($complete == "true")
	{//display this when ticket completed right away
		echo "Help Resolution:$help_res <br />";
	}
	echo "Help Email:$help_email";
	
	if($complete == "true")
	{//display when ticket completed right away
		echo "<strong>(email sent)</strong><br />Phone Tickect Completed.<br />";
	}
	else
	{
		if($sentmsgStudent == "true")
			echo "<strong>(email sent)</strong>";
		if($sentmsgStaff == "true")
			echo "<strong>(email sent)</strong>";
		if($noSentmsgStaff == "true")
			echo "<strong>(email unvaliable)</strong>";
		echo "<br />Technician: $email";
		if($sentmsgTech2 == "true")
			echo "<strong>(email sent)</strong>";
		echo "<br /><br /><br />";
	}
?>
      <a href="http://ul.uc.edu/uclit/protected/itstaff/phone/show_addphone.php">Add Another Phone Ticket</a> 
    </td>
</table>
<?php
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php');
?>    