<?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_comments.php');
	include('/var/www/html/uclit/protected/connections/db_pers.php');
	include('/var/www/html/uclit/protected/connections/db_login.php');
	
	$today = "";
	$today = date("Y-m-d");
	$loggedinuser = $_SESSION['username'];

	$sql_login = "SELECT username, personnel_id FROM users WHERE username = '$loggedinuser' ";
	$db = mysql_select_db('login', $loginConn) or die ("Unable to select login database.");
	$login_result = mysql_query($sql_login, $loginConn) or die ("Unable to query: $sql_login");
	
	while($log_row = mysql_fetch_array($login_result))
	{	
		$pid = $log_row['personnel_id'];
			
		$db = mysql_select_db('uclit_db', $uclit_dbConn) or die ("Unable to select uclit_db database.");
		$sql_logindept = "SELECT personnel_id, last_name, first_name, middle_name, dept_id 
						  FROM personnel 
						  WHERE personnel_id = '$pid' 
						  ORDER BY last_name";
		$logindept_result = mysql_query($sql_logindept, $uclit_dbConn) or die ("Unable to query: $sql_logindept");
		
		while($row = mysql_fetch_array($logindept_result))
 			{
 				$dept_id = $row['dept_id'];
			}
		
		$sql_dept = "SELECT dept_id, dept_name
						  FROM departments 
						  WHERE dept_head_id = '$pid' 
						  ORDER BY dept_id";
		$dept_result = mysql_query($sql_dept, $uclit_dbConn) or die ("Unable to query: $sql_dept");
		
		while($row = mysql_fetch_array($dept_result))
 			{
 				$dept_id = $row['dept_id'];
				$dept_name = $row['dept_name'];
				$deptstring .="$dept_id+";
				$deptnamestring .="$dept_name+";
			}	
				$deptstring = substr_replace($deptstring,"",-1);
				$deptarray = split("\+",$deptstring);
				$deptnamestring =substr_replace($deptnamestring,"",-1);
				$deptnamearray = split("\+",$deptnamestring);
	}
			
			$number = count($deptarray);
				$x = 0;
				while ($x < $number) {
				$where .= "lib='$deptarray[$x]' OR ";
				++$x;
				}
				$where = substr_replace($where,"",-4);
				
	//Select a database
	$db = mysql_select_db('comments', $commentsConn) or die("Couldn't select database. $db_comments");
	
	//SQL Select statement
//	$query1= "Select comments.comments.id AS id, comments.comments.lib AS lib, comments.comments.visit_date AS visit_date, comments.comments.comment_date AS comment_date, comments.comments.patron_type AS patron_type, comments.comments.rating AS rating, comments.comments.`comment` AS comment, comments.comments.comment_type AS comment_type, comments.comments.`status` AS status, comments.comments.assigned_personnel AS personnel, comments.comments.email_check AS email_check, comments.comments.name AS name, comments.comments.email AS email, uclit_db.departments.dept_id AS dept_id, uclit_db.departments.dept_name AS dept_name From uclit_db.departments Inner Join comments.comments ON uclit_db.departments.dept_id = comments.comments.lib WHERE ($where) ORDER BY comment_date Desc
//";
$query1= "Select comments.comments.id AS id2, comments.comments.lib AS lib, comments.comments.visit_date AS visit_date, comments.comments.comment_date AS comment_date, comments.comments.patron_type AS patron_type, comments.comments.rating AS rating, comments.comments.`comment` AS `comment`, comments.comments.comment_type AS comment_type, comments.comments.`status` AS `status`, comments.comments.assigned_personnel AS personnel, comments.comments.email_check AS email_check, comments.comments.name AS name, comments.comments.email AS email, uclit_db.departments.dept_id AS dept_id, uclit_db.departments.dept_name AS dept_name, comments.followup_action.id, comments.followup_action.`desc` AS StatusDesc, uclit_db.personnel.personnel_id, uclit_db.personnel.last_name AS LastName, uclit_db.personnel.first_name AS FirstName
From uclit_db.departments Inner Join comments.comments ON uclit_db.departments.dept_id = comments.comments.lib Inner Join comments.followup_action ON comments.followup_action.id = comments.comments.`status` Inner Join uclit_db.personnel ON uclit_db.personnel.personnel_id = comments.comments.assigned_personnel WHERE comments.followup_action.id = 3 ORDER BY comment_date Desc";

	$sql_export = "
	Select comments.comments.id AS id2, comments.comments.visit_date AS visit_date, comments.comments.comment_date AS comment_date, comments.comments.patron_type AS patron_type, comments.comments.`comment` AS `comment`, comments.comments.email_check AS email_check, comments.comments.name AS name, comments.comments.email AS email, uclit_db.departments.dept_name AS dept_name, comments.followup_action.`desc` AS StatusDesc, uclit_db.personnel.last_name AS LastName, uclit_db.personnel.first_name AS FirstName
From uclit_db.departments Inner Join comments.comments ON uclit_db.departments.dept_id = comments.comments.lib Inner Join comments.followup_action ON comments.followup_action.id = comments.comments.`status` Inner Join uclit_db.personnel ON uclit_db.personnel.personnel_id = comments.comments.assigned_personnel 
	WHERE comments.followup_action.id = 3 ORDER BY comment_date Desc";
	$result1 = mysql_query($query1, $commentsConn) or die("Couldn't execute query.<p>$query1</p>");
	while ($row = mysql_fetch_array($result1)) 
	{
		$ID=$row['id2'];
		$lib = $row['lib'];
		$deptname =$row['dept_name'];
		$email = $row['email'];
		$name = $row['name'];
		$email_check = $row['email_check'];
		$assigned_personnel = $row['assigned_personnel'];
		$lastname = $row['LastName'];
		$firstname = $row['FirstName'];
		$statusdesc = $row['StatusDesc'];
		$status = $row['status'];
		$comment_type = $row['comment_type'];
		$comment = $row['comment'];
		$rating = $row['rating'];
		$visit_date = $row['visit_date'];
		$comment_date = $row['comment_date'];
		$patron_type = $row['patron_type'];
		

		$display_block1 .="<tr>
			   <td align='center'><a href='http://ul.uc.edu/uclit/protected/admin/comments/edit_comment.php?idnum=$ID'><img src='/uclit/images/edit.gif'border='0'></a></td>
			   <td>$deptname</td>
			   <td>$visit_date</td>
			   <td>$comment</td>
			   <td>$statusdesc</td>
			   <td>$lastname, $firstname</td>
			   
			  </tr>";
		
	}

echo"
<H1 align='center'>UnAssigned Comments for All Departments</H1>

<table width=95% border=1>
<tr>
          <td align=center><b>Edit</b></td>
		  <td align=center><b>Department</b></td>
		  <td align=center><b>Date Visited</b></td>
		  <td align=center><b>Description</b></td>
		  <td align=center><b>Status</b></td>
		  <td align=center><b>Person Assigned</b></td>	
</tr>
$display_block1
</table> 
<br>
</table>";
?>
<script type="text/javascript">
<!-- Begin
if (window.print) 
{
	document.write('<form action="0">'
	+ '<input type=button name=print value="Print This Page" '
	+ 'onClick="javascript:window.print()"></form>');
}
// End -->
</script>
<table border="0">
	<tr></tr><tr></tr>
  <tr border="0"><td><FORM id='form2' name='form2' method='Post' ACTION='http://ul.uc.edu/uclit/protected/assocdean/comments/export.php'>
	  <INPUT name='current_status' type='hidden' id='current_status' value='<?php echo"$sql_export"; ?>' />
			<INPUT name='Submit' type='submit' id='Submit' value='Export to Excel' /></FORM></td></tr></table>
<?php
 	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
?>