<?php

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


include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.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);
}
$fromdate   =$_POST["fromdate"];
$todate     =$_POST["todate"];

$querylog="SELECT ifnull(FS_CompleteTicket,0) as FS_CompleteTicket 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);
}

$querydate="select search_startingdate from setup";
$resultdate=mysql_query($querydate,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdate)){
	extract($rows);
}	

$MainCompanyName=explode('_',$_SESSION['dbName']); 
$QueryDir = " Select Date_Format(StartFinancialDate,'%d/%m/%Y') 
			From ".$_SESSION['dbLabel'].".Directories ". 	
			" Where  Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			" and Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
//echo $QueryDir;			
$ResultDir=mysql_query($QueryDir,$connection) or die(mysql_error());   
$RowsDir=mysql_fetch_row($ResultDir);
$StartFinancialDate = $RowsDir[0];


if ($fromdate == '') {
if ($search_startingdate == '-1') {
	$fromdate = $StartFinancialDate;
}

else if($search_startingdate == '0') {
	$fromdate = date('01/m/Y');
}

else if($querydate > '0') {
	$added_timestamp = strtotime('-'.$search_startingdate.' month', time());

	$fromdate = date('01/m/Y', $added_timestamp); 
}
}

if ($todate == '')
	$todate =  date('d/m/Y');

if ($perpage == '' ) $perpage = $config['trowlimit'][0];


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ifNull(Setup_MultiLangPrint,0) as Setup_MultiLangPrint from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

$MainCompanyName=explode('_',$_SESSION['dbName']);

$PProjectCode = "".$PProjectCode;


if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search Ticket Support</title>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

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

function ShowlistProblemType(){
	var tsProbType = encodeURIComponent(document.getElementById('tsProbType').value);
	window.open('List_ProblemTicketType.php?frm=2&PProblemType='+tsProbType,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function ShowlistSupportUser(){
	var tsSupTeam = encodeURIComponent(document.getElementById('tsSupTeam').value);
	window.open('List_SupportUsers.php?frm=1&PUser='+tsSupTeam,'Users','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProject(str){

  window.open('List_SorProject.php?frm=6&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function kd(e)
{
	x=document.search;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnFind.click();
		return false;
    }
    return true;
}

function RefreshPage()
{
   document.search.startrange.value = 0;	
   document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.startrange.value = 0;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	let result=totalrows % perpage;
	if (result == 0) result = perpage;
	let startrange=totalrows-result;
	document.search.startrange.value = startrange;
	document.search.submit();
}

function SetNextPage(startrange,perpage){ 
	document.search.startrange.value = startrange;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.startrange.value = startrange;
	document.search.submit();
}

function findRecord(){
	document.search.startrange.value 	= '';
	document.search.perpage.value 		= '';
	document.search.submit();
}

function ClearPage(){
	document.search.startrange.value 	= '';
	document.search.perpage.value 		= '';
	document.search.PProjectCode.value 	= '';
	document.search.fromdate.value 		= '';
	document.search.todate.value 		= '';
	document.search.tsProbType.value 	= '';
	document.search.tsProbTypeId.value	= '';
	document.search.tsSupTeam.value 	= '';
	document.search.tsSupTeamId.value 	= '';
	document.search.tsStatus.value 		= '';
	document.search.submit();
	
}

function printPage(exp){
	let PProjectCode 	= document.search.PProjectCode.value;
	let fromdate 		= document.search.fromdate.value;
	let todate 			= document.search.todate.value;
	let tsProbTypeId 	= document.search.tsProbTypeId.value;
	let tsSupTeamId 	= document.search.tsSupTeamId.value;
	let tsStatus 		= document.search.tsStatus.value;
	
	let str = 'PProjectCode='+PProjectCode+'&fromdate='+fromdate+'&todate='+todate+'&tsProbTypeId='+tsProbTypeId+'&tsSupTeamId='+tsSupTeamId+'&tsStatus='+tsStatus;
	if (exp == 1)
		str = 'Exp_TicketSupport.php?'+str;
	else
		str = 'Rep_TicketSupport.php?'+str;
	
	window.open(str, 'Report');
}

function confirmCompletion(vId){
	if(confirm("Are you sure This Ticket is Completed?")){
		document.search.action = 'Form_TicketSupport_Action.php?actionmode=completeTicket&ticketId='+vId;
		document.search.submit();	
	}
}
</script>

<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;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action="" >
<input type='hidden' name='startrange' id='startrange' value='$startrange' >
HERE;

	
$querysearch = "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, ticketSupport_CounterNo, ticketSupport_SolutionDesc, ticketVisit_Engineer, ticketSupport_status, if (ticketSupport_status = 'CLOSED', 1, 0) as isClosed
		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 date_format(ticketSupport_date,'%Y-%m-%d') between '".explodeword($fromdate)."' and '".explodeword($todate)."'
			and if('$PProjectCode'='', 1=1, ticket_support.project_code = '$PProjectCode') 
			and if('$tsProbTypeId' = '', 1=1, ticket_support.ticketProblem_id = '$tsProbTypeId')	
			and if('$tsSupTeamId' = '', 1=1, ticketSupport_userId = '$tsSupTeamId')	
			and if('$tsStatus' = '', 1=1, ticketSupport_status = '$tsStatus')	
		order by date_format(ticketSupport_date,'%Y-%m-%d %H:%i') desc "; //echo $querysearch;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
	 

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:1210px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Ticket Support
			</span></td></tr>
			

			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
		 
echo"<table border='0' cellspacing='1' style='table-layout:fixed'>
	  <col width=80>
	  <col width=170>
	  <col width=100>
	  <col width=170>
	  <col width=80>
	  <col width=100>
	  <col width=90>
	  <col width=350>
	  <col width=80>
	
	  <tr class='Search-header-bg'>";
	  

	echo" <td align='left'><span class='style4'>Branch</span></td>
	      <td align='left'><span class='style4'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' style='font-size:12px; width:150px; height:20px;' maxlength='20' onkeydown=\"return kd(event)\"  /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";

	  
	echo" <td align='left' colspan='3'><span class='style4'>From&nbsp;</span><input type='text' name='fromdate' id='fromdate' value='$fromdate' maxlength='10' size='9' onkeydown=\"return kd(event);\">
			<A HREF=\"#\" onClick=\"displayCalendar(document.search.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
			&nbsp; &nbsp;<span class='style4'>To&nbsp;</span><input type='text' name='todate' id='todate' value='$todate' maxlength='10' size='9' onkeydown=\"return kd(event);\">
			<A HREF=\"#\" onClick=\"displayCalendar(document.search.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
		</td>";

  
echo "<td align='left' colspan='1'><span class='style4'>Problem Type</span></td>";
echo" 	<td align='left' colspan='2'><input type='text' name='tsProbType' id='tsProbType' value='$tsProbType' readonly style='font-size:12px; width:120px; height:20px;'>
			<img src='img/btnopen.jpg' width='15' height='15' title='Select Ticket Type' onmouseover=\"style.cursor='hand';\" onclick=\"ShowlistProblemType();\">
			<input type='hidden' name='tsProbTypeId' id='tsProbTypeId' value='$tsProbTypeId'>
		</td>
	</tr>";

echo "<tr class='Search-header-bg'>
		<td><span class='style1'>Support Team</span></td>
		<td><input type='text' name='tsSupTeam' id='tsSupTeam' value='$tsSupTeam' style='font-size:12px; width:150px; height:20px;' maxlength='20' readonly />
			<input type='hidden' name='tsSupTeamId' id='tsSupTeamId' value='$tsSupTeamId'>
			<img src='img/btnopen.jpg' weight='15' height='15' onclick=\"ShowlistSupportUser();\" title='Select Support' onmouseover=\"style.cursor='hand';\">	
		</td>	
		<td><span class='style1'>Status</span></td>
		<td align='center'><select name='tsStatus' id='tsStatus' onchange=\"findRecord();return false;\" style='font-size:12px; width:120px; height:20px;'>
				<option value='' ".($tsStatus==''?'selected':'')."></option>
				<option value='PENDING' ".($tsStatus=='PENDING'?'selected':'').">Pending</option>
				<option value='CLOSED' ".($tsStatus=='CLOSED'?'selected':'').">Closed</option>
				<option value='COMPLETED' ".($tsStatus=='COMPLETED'?'selected':'').">Final Completion</option>
			</select>
		</td>
		<td colspan='4'>
			<button type='button' id='btnFind' name='btnFind' onclick=\"findRecord();return false;\" title='Search Records'><span><span>Find</span></span></button>
			<button type='button' id='btnClear' name='btnClear' onclick=\"ClearPage();return false;\" title='Cancel Search'><span><span>Cancel</span></span></button>
			<button type='button' id='btnPrint' name='btnPrint' onclick=\"printPage('0');return false;\" title='Print Page'><span><span>Print</span></span></button>
			<button type='button' id='btnExcel' name='btnExcel' onclick=\"printPage('1');return false;\" title='Excel'><span><span>Excel</span></span></button>
		</td>
	</tr>";
	
echo "</table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>"; 
echo" </tr>";

	  echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1025px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:300px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\">";
              
            $perpages = $config['trowlimit'];
			foreach ($perpages as $val){
			echo" <option value = '$val' "; if ($perpage ==$val) echo "selected"; 
			echo" >$val</option> ";
			}
	echo'	
				 </select></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";			

	
	
	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:1150px;'>
			<table border='0'  cellpadding='2' cellspacing='1' style='width:1500px;' >";
			
	if ($FS_CompleteTicket == 1)
		echo "<col width=50>";	
	echo "	  <col width=100>
			  <col width=100>
			  <col width=150>
			  <col width=120>
			  <col width=150>
			  <col width=200>
			  <col width=100>
			  <col width=100>
			  <col width=120>
			  <col width=100>
			  <col width=100>
			  <col width=120>
			  <col width=200>";
	  

	  
	if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='18' class='searchmessage'>".$message."</td>
			</tr>";
	}
	
	 echo"<tr class='Search-Label-bg' height='25px'>";
	 if ($FS_CompleteTicket == 1)
		echo "	<td align='left' ></td>";
	echo "	<td align='left' ><span class='style4'><b>Date</b></span></td>
			 <td align='left' ><span class='style4'><b>Time</b></span></td>
			 <td align='left' ><span class='style4'><b>Branch</b></span></td>
			 <td align='left' ><span class='style4'><b>Status</b></span></td>
			 <td align='left' ><span class='style4'><b>Problem Type</b></span></td>
			 <td align='left' ><span class='style4'><b>Problem Description</b></span></td>
			 <td align='left' ><span class='style4'><b>Max Date	</b></span></td>
			 <td align='left' ><span class='style4'><b>Max Time	</b></span></td>
			 <td align='left' ><span class='style4'><b>Support Team</b></span></td>
			 <td align='left' ><span class='style4'><b>Counter No</b></span></td>
			 <td align='left' ><span class='style4'><b>SLA Hour</b></span></td>
			 <td align='left' ><span class='style4'><b>Engineer</b></span></td>
			 <td align='left' ><span class='style4'><b>Solution</b></span></td>
	 </tr>";
		



$i = 0;
while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
		
	switch ($ticketSupport_status) {
		case 'PENDING':
			$status = 'Pending';
			break;
		case 'CLOSED':
			$status = 'Closed';
			break;
		case 'COMPLETED':
			$status = 'Final Completion';
			break;
	}	
	
	echo"<tr $Color> ";
	 if ($FS_CompleteTicket == 1){
		 echo "	<td align='center'>";
		 if ($isClosed == 1)
		 echo "<img src='img/confirmorder.png' title='Ticket Completed' onmouseover=\"style.cursor='hand';\" onclick=\"confirmCompletion('$ticketSupport_id');\" style='width: 15px; height: 15px; cursor: pointer;' >";
		echo "</td>";
	 }
	 
	echo "	<td align='left'>$ticketDate</td>
			<td align='left'>$ticketTime</td>	
			<td align='left'>$branch</td>	
			<td align='left'>$status</td>	
			<td align='left'>$ticketProblem_type</td>	
			<td align='left'>$ticketSupport_ProblemDesc</td>	
			<td align='right'>$MaxDate</td>	
			<td align='right'>$MaxTime</td>	
			<td align='left'>$TeamName</td>	
			<td align='right'>$ticketSupport_CounterNo</td>	
			<td align='right'>$ticketProblem_SLAHours</td>	
			<td align='left'>$ticketVisit_Engineer</td>	
			<td align='left'>$ticketSupport_SolutionDesc</td>	
		</tr>";
	
}
							

echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";		
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:920px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

          

		  

print <<<HERE

</form>
</body>

HERE;
function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
?>