<style type="text/css">
.waiting { cursor: wait; }
 
.loader {
  border: 16px solid #f3f3f3; border-radius: 50%; border-top: 16px solid #3498db;
  width: 120px;  height: 120px;
  -webkit-animation: spin 2s linear infinite; /* Safari */
  animation: spin 2s linear infinite;
/* ==== CENTER PAGE ====*/  
  margin: 0; position: absolute; top: 50%; left: 50%;
  -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%);
  top: 40%;  -ms-transform: translateY(-30%); transform: translateY(-30%); 
}

/* Safari */
@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
 
<div class="loader"></div>

<body >

<?php
 
session_start();
include 'myfunctions.php';
 
$tblName="";
$IDc=0;
$AddRecFields="";
$AddRecValues="";
$UpdateRec="";
$FormName="";
$PageName="";
$CloseType="GoBack";

   foreach($_POST as $key => $value):
	    // echo $key .' : ' . $value.'<br>';
		$value=str_replace("'", "\'", $value);
		$value=str_replace('"', '\"', $value);
		$frmValues[$key]= $value;
		if ($key=='tblName'){$tblName=$value;}
		if ($key=='IDc'){$IDc=$value;}
		if ($key=='PageName'){$PageName=$value;}
		if ($key=='CloseType'){$CloseType=$value;}
   endforeach;

// echo " ============== 1";
//   echo " ========================  " . $tblName . " IDc : " . $IDc . " ======================== <br> ";
// if ($IDc==0){ echo "nea eggrafi".'<br>'; }	
 
if ($tblName==''){exit();}
//	$sql = 'SHOW COLUMNS FROM '. $tblName;
//	$sql = 'SELECT * FROM '. $tblName;
//   dbConnect();
//       $result = mysqli_query($conn, $sql);
//     $num_rows = mysqli_num_rows($result);
//       $fields = mysqli_fetch_fields($result);
 
		  $sql = 'SHOW COLUMNS FROM '. $tblName;
       $result = mysqli_query($conn,$sql);
 while ($array = mysqli_fetch_array($result)){
      $fldName=$array['Field'];
	  $columns[] = $array['Field'];
      $fldTypes[$fldName] = $array['Type'];   	  
	}
 
 $IDcField = $columns[0]; # === first Field Counter =====
 $fldTypes = array_slice( $fldTypes, 1 ); # remove first name
 
# MAKE FIELDS FOR ADD OR UPDATE
 foreach(array_keys($fldTypes) as $fldName){		  
 	if(array_key_exists($fldName, $frmValues)) {
		 $typ1 = $fldTypes[$fldName];
	   $mytyp1 = myFieldType($typ1);
        $myVal = $frmValues[$fldName];
 // echo $fldName . ' ('.$mytyp1.') = '. $myVal .'<br>';
	    if($mytyp1=="String"){$quote="'";} else {$quote="";}
	    if($mytyp1=="Number" ){
//		  	   echo $fldName . ' ('.$mytyp1.') = '. $myVal .'<br>';
			    $myVal= str_replace(',','.',$myVal);
				$myVal=gConvertField($myVal,0);} 
	    if($mytyp1=="Date") {
		   if($myVal==''){$myVal='null';}else{
				   $quote="'"; // $xx=$myVal;
                   $myVal=formatMyDate($myVal, "GrToMySql");
			     }
			   }
		$AddRecFields = $AddRecFields . $fldName . ", ";
		$AddRecValues = $AddRecValues . $quote . $myVal . $quote . ", ";
		$UpdateRec = $UpdateRec . $fldName . "=" . $quote . $myVal . $quote . ", ";
	}	
}


if ($IDc==0){
# ============== 	
#     RecADD 
# ==============	
  if($AddRecFields!=''){ $AddRecFields='(' . substr($AddRecFields, 0, -2) . ')' ;}
  if($AddRecValues!=''){ $AddRecValues='(' . substr($AddRecValues, 0, -2) . ')' ;}
  $sql = "INSERT INTO ".$tblName. " " .$AddRecFields." VALUES ".$AddRecValues;

	if($conn->query($sql)===TRUE) {
	   $IDc = $conn->insert_id;
	   echo ".";
	   setcookie($IDcField, $IDc ); 
	   # echo "Record added successfully : " . $IDc . "<br>"; 
	} else {
	   echo "Error added record: " . $conn->error;
	}
	
#  echo  '<br> SQL:<br>' . $sql .'<br';
 
}
else
{
# ============== 	
#     Update 
# ==============	
 
  if($UpdateRec!=''){ $UpdateRec= substr($UpdateRec, 0, -2) ;}	
  $sql = "UPDATE ".$tblName." SET ".$UpdateRec." WHERE ".$IDcField."=".$IDc;	
  
 // echo  '<br> SQL:<br>' . $sql .'<br';

	if ($conn->query($sql) === TRUE) {
	#       echo "Record updated successfully";	
	} else {
	     echo "Error updating record: " . $conn->error;
	}	
} 

# echo  '<br> SQL:<br>' . $sql .'<br';
 
# echo "<br><br>" . $sql ."<br><br>";
# echo "AddRecFields=".$AddRecFields . "<br><br>";
# echo "AddRecValues=". $AddRecValues . "<br><br>";
# echo "UpdateRec=". $UpdateRec  . "<br><br>";

# setcookie('IDc', $IDc, time() + (10), "/"); // +(10)=10 sec  : 86400 = 1 day
# setcookie($IDcField, $IDc ); // +(10)=10 sec  : 86400 = 1 day
 
# echo "========= " . strtoupper($tblName). " , " .$IDc. " =========== " . (strtoupper($tblName)=="TAMEIO") ."<br><br>";
 
if (strtoupper($tblName)=="TAMEIO") {
	$IDTameiou = $IDc;	$frmValues['IDTameiou']= $IDc;
  $myRecTypeID = $frmValues['IDRecType'];
    $isReserve = TameioRecTypesGet($myRecTypeID, 'isReserve');
    Apothematiko_Update($frmValues, $IDTameiou, $isReserve );
}
# ============================= 
#    Apothematiko_Update
# =============================
function Apothematiko_Update($frmValues, $IDTameiou, $isReserve) {
$AddRecFields="";
$AddRecValues="";
$UpdateRec="";

$conn = dbConnect();

// echo "<br>";

if ($isReserve==0){
	$sql = "DELETE FROM tameio_apothematiko WHERE IDTameiou=" . $IDTameiou ;
    $conn->query($sql);
	return ;
}

echo "<br>";
   
    $sql = "SHOW COLUMNS FROM tameio_apothematiko" ;
 $result = mysqli_query($conn,$sql);
 
 while ($fld_array = mysqli_fetch_array($result)){
      $fldName2=$fld_array['Field'];
      $fldTypes2[$fldName2] = $fld_array['Type'];
  //   echo $fldName2 . " : " . $fldTypes2[$fldName2] . " = " . $frmValues[$fldName2] ."<br>";
 	if(array_key_exists($fldName2, $frmValues)) {
			 $typ1 = $fldTypes2[$fldName2];
		   $mytyp1 = myFieldType($typ1);
			$myVal = $frmValues[$fldName2];
	    if($mytyp1=="String"){$quote="'";} else {$quote="";}
	    if($mytyp1=="Number" ){
			    $myVal= str_replace(',','.',$myVal);
				$myVal=gConvertField($myVal,0);} 
	    if($mytyp1=="Date") {
		   if($myVal==''){$myVal='null';}else{
				   $quote="'";  
                   $myVal=formatMyDate($myVal, "GrToMySql");
			     }
			   }
		$AddRecFields = $AddRecFields . $fldName2 . ", ";
		$AddRecValues = $AddRecValues . $quote . $myVal . $quote . ", ";
		$UpdateRec = $UpdateRec . $fldName2 . "=" . $quote . $myVal . $quote . ", ";
	}	
 }
 
# echo "<br><br>";
# echo " AddRecFields : " . $AddRecFields . "<br>";
# echo " AddRecValues : " . $AddRecValues . "<br>";
# echo " UpdateRec : " . $UpdateRec . "<br>";
# echo "<br><br>";

# ====================================================
   $num_rows=0;
   $sql = 'Select * FROM tameio_apothematiko where IDTameiou=' . $IDTameiou ;
   $num_rows=gconvertfield(RecordsCount($conn,$sql),0);
 
if ($num_rows==0){
	# ============== 	
	#     RecADD 
	# ==============	
	  if($AddRecFields!=''){ $AddRecFields='(' . substr($AddRecFields, 0, -2) . ')' ;}
	  if($AddRecValues!=''){ $AddRecValues='(' . substr($AddRecValues, 0, -2) . ')' ;}
	  $sql = "INSERT INTO tameio_apothematiko " .$AddRecFields." VALUES ".$AddRecValues;
	} else {
	# ============== 	
	#   Rec Update 
	# ==============	 
	if($UpdateRec!=''){ $UpdateRec= substr($UpdateRec, 0, -2) ;}	
	$sql = "UPDATE tameio_apothematiko SET ". $UpdateRec ." WHERE IDTameiou=" . $IDTameiou;	
}

$conn->query($sql); 

//echo " ============== APOTHEMATIKO ============= <br>" ;
//echo " sql : " . $sql . "<br>";
//echo "<br><br>";	 
//echo " ========== APOTHEMATIKO telos =========== <br>" ;

}

## Close connection  ## 
 
$conn->close();
echo '<br>';  
 
// echo $CloseType  . "<br><br>";
//  echo  '<br> SQL:<br>' . $sql .'<br><br><br> CloseType=' . $CloseType;
?>


</body> 

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

var closetype='<?php echo $CloseType ?>';
//window.parent.document.getElementById('favouritesTab');
window.history.back();
//window.opener.reload();
location.reload();

if (closetype=="Close") {
	     // window.history.go(-1);
		//	location.reload();
		//  window.opener.location.reload();
		//  window.close();
	
	} else if(closetype=="Return") {
		window.history.go(-1);
// 	return;
	// window.history.go(-2);
	  //refreshParent;
	//  location.reload();
	  	  
	} else {
	//  window.history.back();
	//  location.reload();
	}
	
</script>