<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FS_TicketReport as loginaccess, ifnull(FS_CloseTicket,0) as FS_CloseTicket, user_account, Language from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

$query_navigator = "SELECT LANGUAGE,
       ClearPage,
       Excel,
       Save,
       Print,
       Addd,
       SEARCH,
       Pending,
       CopyJV,
       PaymentRequests,
       ExportExcel
FROM ".$_SESSION['dbLabel'].".mainform_label
WHERE LANGUAGE = '".$Language."'";
$result_navigator = mysql_query($query_navigator, $connection);
$row_navigator    = mysql_fetch_array($result_navigator);
extract($row_navigator);

	
//Read From Setup Table							
$querysetup= " select grid_color1, grid_color2,maxperpage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
	

if ($startrange=='') 	$startrange = 0;
if ($perpage == '' ) 	$perpage = $config['trowlimit'][0];
print <<<HERE

<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<title>Report For Ticketing</title>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style1 {color: #999999; font-weight: bold; }
.styled {font-size:11px;}

</style>


<script type="text/javascript" language="javascript">

setInterval(function(){window.location.reload(true)},60000);

function printPage(exp){
	let str = '';
	
	if(exp==1)
		str = 'Rep_Ticketing.php?export='+exp;
	else 
		str = 'Rep_Ticketing.php?export='+exp;
	window.open(str);
}

function setChkd(vId){
	if (document.getElementById('chkRecord'+vId).checked)
		document.getElementById('chkRecord'+vId).value = 1;
	else 
		document.getElementById('chkRecord'+vId).value = 0;
}

function closeTicket(){
	document.ticketrep.action = 'Form_TicketSupport_Action.php?actionmode=closeTicket';
	document.ticketrep.submit();	
}
</script>

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
</head>
<body >
<form name='ticketrep' method='post' enctype='multipart/form-data'>


HERE;

	


echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title' style='text-transform: uppercase'>Report For Ticketing</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";


echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>";
echo"		<button type='button' id='btnExcel' name='btnExcel' onclick=\"printPage('1'); return false;\" title='Excel'><span><span>$Excel</span></span></button>";


echo"		<button type='button' id='btnPrint' name='btnPrint' onclick=\"printPage('0');return false;\" title='Print Page'><span><span>$Print</span></span></button>";
if($FS_CloseTicket == 1)
echo"		<button type='button' id='btnClose' name='btnClose' onclick=\"closeTicket();return false;\" title='Close Ticket'><span><span>Close Ticket</span></span></button>";

echo"   </td><td align='left' width='240px'></td><td align='left'><span class='style1'><b>Results/page</b> <select name='perpage' id='perpage'  onchange=set_limit();>";
	foreach ($config['trowlimit'] as $value) {
		echo "<option value='$value' ".($value==$perpage?'selected':'')."  >$value</option>";
	}
	
echo "</select></td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>";		
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";
		
	echo"   <tr bgcolor='#F7F7F7'><td valign='top'  align='left'>";	  
echo"<table border='0' style='table-layout:fixed' align='left' valign='top' width='1100px'>
      <col width=1100px>";

echo "<tr><td><div style='width:1100px;overflow-x:scroll;overflow-y:scroll;height:500px;'><table width=1660px >";

if($FS_CloseTicket == 1) echo "<col width=90px>";
echo "<col width=80px><col width=80px><col width=80px>
<col width=150px>
<col width=120px>
<col width=230px><col width=90px><col width=90px><col width=120px><col width=90px><col width=90px>
	<col width=150px><col width=200px><col width=90px>";
	
echo"<tr height='30px' style='background-color:darkgray;'> ";
if($FS_CloseTicket == 1) echo "<td align='left' ><span class='style1'><b>Close Ticket</b></td>";
echo "<td align='left' ><span class='style1'><b>Date</b></td>
	 <td align='left' ><span class='style1'><b>Time</b></td>
	 <td align='left' ><span class='style1'><b>Remain in HR</b></td>
	 <td align='left' ><span class='style1'><b>Branch</b></td>
	 <td align='left' ><span class='style1'><b>Problem Type</b></td>
	 <td align='left' ><span class='style1'><b>Problem Description</b></td>
	 <td align='left' ><span class='style1'><b>Max Date	</b></td>
	 <td align='left' ><span class='style1'><b>Max Time	</b></td>
	 <td align='left' ><span class='style1'><b>Support Team</b></td>
	 <td align='left' ><span class='style1'><b>Counter No</b></td>
	 <td align='left' ><span class='style1'><b>SLA Hour</b></td>
	 <td align='left' ><span class='style1'><b>Engineer</b></td>
	 <td align='left' ><span class='style1'><b>Solution</b></td>
	 <td align='left' ><span class='style1'><b>Real Time </b></td>
	 
	</tr>";

$query = "select ticketSupport_id, date_format(ticketSupport_date,'%d/%m/%Y') as ticketDate, date_format(ticketSupport_date,'%H:%i %p') as ticketTime, ticket_support.project_code as branch, project_name,
		ticketProblem_type, ticketSupport_ProblemDesc, ticketProblem_SLAHours, date_format(ticketSupport_MaxDate,'%d/%m/%Y') as MaxDate, date_format(ticketSupport_MaxDate,'%H:%i %p') as MaxTime,
		username as TeamName, ifnull(ticketProblem_showCounter,0), ticketSupport_CounterNo, ticketSupport_SolutionDesc, ticketVisit_Engineer, date_format(now(), '%H:%i %p') as nowDate,
		TIMESTAMPDIFF(hour,now(), ticketSupport_MaxDate) as remainingTime, TIMESTAMPDIFF(minute,now(), ticketSupport_MaxDate) as remainingMinute
		from ticket_support
		left join ticket_problem on ticket_problem.ticketProblem_id = ticket_support.ticketProblem_id
		left join user_support on user_id = ticket_support.ticketSupport_userId 
		left join project on project.project_code = ticket_support.project_code 
		where ticketSupport_status = 'PENDING' 
		order by remainingMinute,date_format(ticketSupport_date,'%Y-%m-%d %H:%i') desc "; 
		
//echo $query;//exit;		
		
$result = mysql_query($query, $connection);
$totalrows = mysql_num_rows($result);
$query .= " limit ".$startrange.",".$perpage;	

while ($row = mysql_fetch_array($result)){
	extract($row);
	
	
	if ($remainingMinute<0)	
	$queryColor = "select color_code from Ticket_ColorSetup where setup_fromTime=0";
    else $queryColor = "select color_code from Ticket_ColorSetup where '$remainingMinute' between setup_fromTime and setup_toTime ";
	
	//echo $queryColor;exit;
	
	$resColor = mysql_query($queryColor, $connection);
	$rowColor = mysql_fetch_row($resColor);
	$color = $rowColor[0];
	
	$chkRecord = 'chkRecord'.$ticketSupport_id;
	if($$chkRecord == '') $$chkRecord = 0;
	
	echo"<tr height='20px' bgcolor='$color'> ";
	
	if($FS_CloseTicket == 1)
	echo " <td align='center' ><span class='styled'><input type='checkbox' name='$chkRecord' id='$chkRecord' value='".$$chkRecord."' onclick=\"setChkd('$ticketSupport_id');\"></td>";

	echo " <td align='left' ><span class='styled'>$ticketDate</td>
		 <td align='left' ><span class='styled'>$ticketTime</td>
		 <td align='right' ><span class='styled'>$remainingTime</td>
		 <td align='left' ><span class='styled'>$branch</td>
		 <td align='left' ><span class='styled'>$ticketProblem_type</td>
		 <td align='left' ><span class='styled'>$ticketSupport_ProblemDesc</td>
		 <td align='right' ><span class='styled'>$MaxDate</td>
		 <td align='right' ><span class='styled'>$MaxTime</td>
		 <td align='left' ><span class='styled'>$TeamName</td>
		 <td align='right' ><span class='styled'>$ticketSupport_CounterNo</td>
		 <td align='right' ><span class='styled'>$ticketProblem_SLAHours</td>
		 <td align='left' ><span class='styled'>$ticketVisit_Engineer</td>
		 <td align='left' ><span class='styled'>$ticketSupport_SolutionDesc</td>
		 <td align='right' ><span class='styled'>$nowDate</td>
		 
		</tr>";
		
}

	
echo"<tr height='20'><td colspan='14'>&nbsp;</td></tr>";

echo "</table></div></td></tr>";

echo"</table></td></tr><tr><td colspan='3' valign='top'>";


echo"<tr><td >&nbsp;</td><td align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr>";
echo "</table></td></tr>";	
echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	

print <<< HERE
      
	  </table>
HERE;


print <<<HERE
</form>
</body>

HERE;

?>