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

$mainmode 			= $_POST['mainmode'];  if ($mainmode == '') $mainmode 	= $_GET['mainmode'];
$message 			= $_POST['message'];
$tvId 				= $_POST['tvId'];		if ($tvId == '') 	$tvId 		= $_GET['tvId'];
$tvDate 			= $_POST['tvDate'];
$tvprojectCode 		= $_POST['tvprojectCode'];
$tvSupTeamId 		= $_POST['tvSupTeamId'];
$tvRemark 			= $_POST['tvRemark'];
$tvStatus 			= $_POST['tvStatus'];
$tvContactPerson 	= $_POST['tvContactPerson'];
$tvApproved 		= $_POST['tvApproved'];
$tvClosed 			= $_POST['tvClosed'];
$image 				= $_POST['image'];
$tvType 			= $_POST['tvType'];

$message = '';
$ErrorExists = 0;

if ($tvApproved == '') $tvApproved = 0;
if ($tvClosed == '')   $tvClosed = 0;

$ndate = explode('/', $tvDate);
$tvDate = $ndate[2]."-".$ndate[1]."-".$ndate[0]." ".date('H:i:s');

if ($mainmode == 'insert' || $mainmode == 'edit'){
	$checkProj = "select 1 from project where project_code = ".GetSQLValueString($tvprojectCode,'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' && $tvId == '' && $ErrorExists == 0){
	$queryins 	= "insert into ticket_visit (ticketVisit_date, ticketVisit_userId, ticketVisit_contactPerson, project_code, ticketVisit_remark, ticketVisit_closed, 
					ticketVisit_status, ticketVisit_approved, ticketVisit_type, user_account) 
					values 
					(".GetSQLValueString($tvDate,'text').", ".GetSQLValueString($tvSupTeamId,'int').", ".GetSQLValueString($tvContactPerson,'text').", ".GetSQLValueString($tvprojectCode,'text')." ,
					".GetSQLValueString($tvRemark,'text').", $tvClosed, ".GetSQLValueString($tvStatus,'text').", $tvApproved, ".GetSQLValueString($tvType,'text').",'".$_SESSION['useraccount']."')"; 
	//echo $queryins;exit;
	$resins 	= mysql_query($queryins, $connection);
	$message = mysql_error();
	
	if ( $resins && $tvType == '1' && $tvprojectCode != ''){
		$queryPreventive = "update project set project_preventifDate = DATE_ADD(date_format(project_preventifDate,'%Y-%m-%d'), interval ifnull(project_periodMonth,0) month)
							where project_code = '$tvprojectCode' ";
							
		$resrPreventive = mysql_query($queryPreventive, $connection);					
		$message = mysql_error();					
	}
	
}
else if ($mainmode == 'edit' && $tvId != '' && $ErrorExists == 0){
	$queryup 	= "update ticket_visit set 
						ticketVisit_date = ".GetSQLValueString($tvDate,'text').", 
						ticketVisit_userId = ".GetSQLValueString($tvSupTeamId,'int').", 
						ticketVisit_contactPerson = ".GetSQLValueString($tvContactPerson,'text').", 
						project_code = ".GetSQLValueString($tvprojectCode,'text').", 
						ticketVisit_remark = ".GetSQLValueString($tvRemark,'text').",
						ticketVisit_closed = $tvClosed, 
						ticketVisit_status = ".GetSQLValueString($tvStatus,'text').", 
						ticketVisit_approved = $tvApproved,
						ticketVisit_type = ".GetSQLValueString($tvType,'text')."
					where ticketVisit_id = $tvId "; //echo $queryup;exit;
	$resup 		= mysql_query($queryup, $connection);
	$message 	= mysql_error();	

}
else if ($mainmode == 'delete' && $tvId != '' && $ErrorExists == 0){
	$querydel 	= "delete from ticket_visit where ticketVisit_id = $tvId ";
	$resdel		= mysql_query($querydel, $connection);
	$message 	= mysql_error();
	
}

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