<?php

session_start();


if($_SESSION['dbName']==""){
header("location:../index.php");
}
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("config.inc.php");
include("function.inc.php");
include("Form_GridTransport.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);
}

$mode	 			= $_GET['mode'];				
$modedt 			= $_GET['modedt'];
$changeTyreId 		= $_GET['changeTyreId']; 		if($changeTyreId == '') 	$changeTyreId 		= $_POST['changeTyreId'];
$tyreChangeDate 	= $_GET['tyreChangeDate']; 		if($tyreChangeDate == '') 	$tyreChangeDate 	= $_POST['tyreChangeDate'];
$tyreChangeRemark 	= $_GET['tyreChangeRemark'];	if($tyreChangeRemark == '') $tyreChangeRemark 	= $_POST['tyreChangeRemark'];
$tyreType 			= $_GET['tyreType'];
$tyreSerialNo 		= $_GET['tyreSerialNo'];
$tyreBrand 			= $_GET['tyreBrand'];
$tyreEstDist 		= $_GET['tyreEstDist'];
$vehicleId 			= $_GET['vehicleId'];
$driverId 			= $_GET['driverId'];
$tyreRemarkdt 		= $_GET['tyreRemarkdt'];
$changeTyreDtId 	= $_GET['changeTyreDtId'];
$Dt_Type		 	= $_GET['Dt_Type'];


$ndate = explode('/', $tyreChangeDate);
$tyreChangeDate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
$errorExist = 0;

if ($tyreEstDist == '') $tyreEstDist =0;
if ($driverId == '') $driverId = "NULL";



if ($mode == 'insert' && $changeTyreId == ''){
	$queryins = "insert into changeTyre (changeTyre_Date, changeTyre_remark) values ('".$tyreChangeDate."', ".GetSQLValueString($tyreChangeRemark,'text').");";
	$resins = mysql_query($queryins,$connection);
	$message = mysql_error();
	if ($message != '') $errorExist = 1;
	$changeTyreId = mysql_insert_id();
} 

if ($mode == 'edit' && $changeTyreId != ''){
	$queryup = "update changeTyre set changeTyre_Date = '".$tyreChangeDate."', changeTyre_remark = ".GetSQLValueString($tyreChangeRemark,'text')."
				where changeTyre_id = $changeTyreId ";
	$resup = mysql_query($queryup,$connection);
	$message = mysql_error();
	if ($message != '') $errorExist = 1;
}

if ($changeTyreId!= '' && $errorExist == 0 && ($modedt == 'insertdt' || $modedt == 'editdt' )){
	if ($modedt == 'insertdt' && $changeTyreDtId == ''){
		$queryinsdt = "insert into changeTyreDt (changeTyre_id, tyre_type, tyre_serialNo, tyre_brand, tyre_estimatedDist, vehicle_id, vehicle_driver, changeTyreDt_remark,
					changeTyreDt_type) values 
					('".$changeTyreId."', ".GetSQLValueString($tyreType,'text').", ".GetSQLValueString($tyreSerialNo,'text').", ".GetSQLValueString($tyreBrand,'text').",
					".$tyreEstDist.", ".$vehicleId.", ".$driverId.", ".GetSQLValueString($tyreRemarkdt,'text').", ".GetSQLValueString($Dt_Type,'text')."  );";

		$resinsdt = mysql_query($queryinsdt,$connection);
		$message = mysql_error();	
	} else if ($modedt == 'editdt' && $changeTyreDtId != ''){
		$queryupdt = "update changeTyreDt set 
							tyre_type = ".GetSQLValueString($tyreType,'text').", 
							tyre_serialNo = ".GetSQLValueString($tyreSerialNo,'text').", 
							tyre_brand = ".GetSQLValueString($tyreBrand,'text').", 
							tyre_estimatedDist = ".$tyreEstDist.", 
							vehicle_id = ".$vehicleId.", 
							vehicle_driver = ".$driverId.", 
							changeTyreDt_remark = ".GetSQLValueString($tyreRemarkdt,'text')."
					where changeTyreDt_id = $changeTyreDtId;";
		echo $queryupdt;
		$resupdt = mysql_query($queryupdt,$connection);	
		$message = mysql_error();	
	}

	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo $changeTyreId;
	echo "[BRK]";
	getChangeTyreDetails($changeTyreId,$Dt_Type,$connection);
	exit;
}

if ($modedt == 'deletedt' && $changeTyreDtId != ''){

	$querydel = "delete from changeTyreDt where changeTyreDt_id = $changeTyreDtId ";
	$resdel = mysql_query($querydel, $connection);
	$message = mysql_error();	
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo $changeTyreId;
	echo "[BRK]";
	getChangeTyreDetails($changeTyreId,$Dt_Type,$connection);
	exit;

}

if ( $mode == 'deleteRecord' && $changeTyreId != ''){
	$del = "delete from changeTyreDt where changeTyre_id = $changeTyreId ";
	$res = mysql_query($del, $connection);
	if ($res){
		$del = "delete from changeTyre where changeTyre_id = $changeTyreId ";
		$res = mysql_query($del, $connection);	
		$message = mysql_error();
	} else $message = mysql_error();

	header("Location: ./Form_SearchChangedTyres.php?message=$message");
	exit;
}
$message = ereg_replace(' ','%20',$message);

mysql_close($connection);

if($message == '')
header("Location: ./Form_ChangingTyre.php");
else
header("Location: ./Form_ChangingTyre.php?message=$message");
exit;

?>

