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


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) 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_TicketSupportApp as loginaccess, 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");

$actionmode 	= $_GET['actionmode'];
$tsProbSLA 		= $_GET['tsProbSLA'];

$mainmode 		= $_POST['mainmode'];  
$message 		= $_POST['message'];
$tsId 			= $_POST['tsId'];		
$tsDate 		= $_POST['tsDate'];			if ($tsDate == '') $tsDate = $_GET['tsDate'];
$tsTime 		= $_POST['tsTime'];			if ($tsTime == '') $tsTime = $_GET['tsTime'];
$tsprojectCode 	= $_POST['tsprojectCode'];
$tsProbTypeId 	= $_POST['tsProbTypeId'];
$tsProbDesc 	= $_POST['tsProbDesc'];
$tsMaxDate 		= $_POST['tsMaxDate'];
$tsMaxTime 		= $_POST['tsMaxTime'];
$tsSupTeamId 	= $_POST['tsSupTeamId'];
$tsCountNo 		= $_POST['tsCountNo'];
$tsStatus 		= $_POST['tsStatus'];
$tsSolDesc 		= $_POST['tsSolDesc'];
$image 			= $_POST['image'];
$tsEngineer		= $_POST['tsEngineer'];

$message = '';

$ErrorExists = 0;

$supportDate = $tsDate." ".$tsTime.":00";

if ($tsMaxDate != ''){
	$supportMaxDate = $tsMaxDate;
	if ($tsMaxTime != '')
		$supportMaxDate .= " ".$tsMaxTime.":00";
	else
		$supportMaxDate .= " 00:00:00";
}
 
if ($actionmode == 'closeTicket'){
	
	$query = "select ticketSupport_id
			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 date_format(ticketSupport_date,'%Y-%m-%d %H:%i') desc "; 
	$result = mysql_query($query, $connection);
	while ($row = mysql_fetch_array($result)){
		extract($row);
		
		$chkRecord = 'chkRecord'.$ticketSupport_id;
		if($$chkRecord == '') $$chkRecord = 0;
		
		if ($$chkRecord == 1){
			$up = "update ticket_support set ticketSupport_status = 'CLOSED', ticketSupport_ClosedDate = now() where ticketSupport_id = $ticketSupport_id ";
			$res = mysql_query($up, $connection);
		}
	}
	
	
	header("Location: ./Form_ReportTicketing.php");
	exit;	
}	

if ($actionmode == 'completeTicket'){
	$ticketId = $_GET['ticketId'];
	
	$up = "update ticket_support set ticketSupport_status = 'COMPLETED', ticketSupport_CompletedDate = now() where ticketSupport_id = $ticketId ";
	$res = mysql_query($up, $connection);
	
	echo "<form name='complateAction' method='POTS' action='Form_TicketSupportSearch.php'> 
		<input type='hidden' name='PProjectCode' value='$PProjectCode'>
		<input type='hidden' name='fromdate' value='$fromdate'>
		<input type='hidden' name='todate' value='$todate'>
		<input type='hidden' name='tsProbType' value='$tsProbType'>
		<input type='hidden' name='tsSupTeam' value='$tsSupTeam'>
		<input type='hidden' name='tsStatus' value='$tsStatus'>	
		</form>";
	echo "<script>
		document.complateAction.submit();
		</script>";
	exit;	
}

if ($actionmode == 'CalculateMaxDate'){
	if ($tsProbSLA == '') $tsProbSLA = 0;
	
	$query = "SELECT date_format(DATE_ADD('$supportDate', INTERVAL ($tsProbSLA*60) MINUTE), '%Y-%m-%d'),date_format(DATE_ADD('$supportDate', INTERVAL ($tsProbSLA*60) MINUTE), '%H:%i');"; 
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);
	$maxDate = $row[0];
	$maxTime = $row[1];
	echo "[BRK]";
	echo $maxDate;
	echo "[BRK]";
	echo $maxTime;
	echo "[BRK]";	
	exit;	
}
 
if ($mainmode == 'insert' || $mainmode == 'edit'){
	$checkProj = "select 1 from project where project_code = ".GetSQLValueString($tsprojectCode,'text')." ";
	$resProj = mysql_query($checkProj, $connection);
	$rwProj = mysql_num_rows($resProj);
	if ($rwProj == 0){
		$message = 'Insert/Edit Not Allowed! Please check For Branch Code!';
		$ErrorExists = 1;
	}
}

if ($mainmode == 'insert' && $tsId == '' && $ErrorExists == 0){
	$queryins 	= "insert into ticket_support (ticketSupport_date, project_code, ticketProblem_id, ticketSupport_ProblemDesc, ticketSupport_MaxDate,
					ticketSupport_userId, ticketSupport_SolutionDesc, ticketSupport_CounterNo, ticketSupport_status, ticketVisit_Engineer, user_account) 
					values 
					(".GetSQLValueString($supportDate,'text').", ".GetSQLValueString($tsprojectCode,'text').", ".GetSQLValueString($tsProbTypeId,'int').", ".GetSQLValueString($tsProbDesc,'text').",
					".GetSQLValueString($supportMaxDate,'text').", ".GetSQLValueString($tsSupTeamId,'int').", ".GetSQLValueString($tsSolDesc,'text').", ".GetSQLValueString($tsCountNo,'double').",
					".GetSQLValueString($tsStatus,'text').", ".GetSQLValueString($tsEngineer,'text').", '".$_SESSION['useraccount']."')"; //echo $queryins;exit;
	$resins 	= mysql_query($queryins, $connection);
	$message = mysql_error();
	
}
else if ($mainmode == 'edit' && $tsId != '' && $ErrorExists == 0){
	$queryup 	= "update ticket_support set 
						ticketSupport_date = ".GetSQLValueString($supportDate,'text').", 
						project_code = ".GetSQLValueString($tsprojectCode,'text').", 
						ticketProblem_id = ".GetSQLValueString($tsProbTypeId,'int').", 
						ticketSupport_ProblemDesc = ".GetSQLValueString($tsProbDesc,'text').", 
						ticketSupport_MaxDate = ".GetSQLValueString($supportMaxDate,'text').",
						ticketSupport_userId = ".GetSQLValueString($tsSupTeamId,'int').", 
						ticketSupport_SolutionDesc = ".GetSQLValueString($tsSolDesc,'text').", 
						ticketSupport_CounterNo = ".GetSQLValueString($tsCountNo,'double').", 
						ticketSupport_status = ".GetSQLValueString($tsStatus,'text').",
						ticketVisit_Engineer = ".GetSQLValueString($tsEngineer,'text')."
					where ticketSupport_id = $tsId "; //echo $queryup;exit;
	$resup 		= mysql_query($queryup, $connection);
	$message 	= mysql_error();	

}
else if ($mainmode == 'delete' && $tsId != '' && $ErrorExists == 0){
	$querydel 	= "delete from ticket_support where ticketSupport_id = $tsId ";// echo $querydel;exit;
	$resdel		= mysql_query($querydel, $connection);
	$message 	= mysql_error();
	
}

$mainmode 	= '';
$tsId	= '';
echo "<form name='ticketsupAction' method='POST' action='Form_TicketSupport.php'>
	<input type='hidden' name='mainmode' value='$mainmode'>
	<input type=hidden name='message' value='$message'>
	<input type=hidden name='tsId' value='$tsId'>
	</form>";
echo "<script> 
		document.ticketsupAction.submit();
	 </script>";
		
?>