<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

?>

<style type="text/css">
.waiting { cursor: wait; }
</style>

<style>
.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
#echo ".";

session_start();
include 'myfunctions.php';

//echo $_SESSION['command'];  

$tblName="";
$IDc=0;
$AddRecFields="";
$AddRecValues="";
$UpdateRec="";
$FormName="";
$PageName="";
$CloseType="Return";
$SendFrom="";
$SendTo="";
$SendToCC="";
$SendToBCC="";
$Subject="";
$Message="";
$Attacments="";

   foreach($_POST as $key => $value):
       // $html .= $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=gconvertfield($value,0);}
		if ($key=='PageName'){$PageName=$value;}
		if ($key=='CloseType'){$CloseType=$value;}
		
		if ($key=='SendFrom'){$SendFrom=$value;}
		if ($key=='SendTo'){$SendTo=$value;}
		if ($key=='SendToCC'){$SendToCC=$value;}
		if ($key=='SendToBCC'){$SendToBCC=$value;}
		if ($key=='Subject'){$Subject=$value;}
		if ($key=='Message'){$Message=$value;}
		
		if ($key=='Attacments'){$Attacments=$value;}
		
   endforeach;

//  echo " ============== 1 =======  $tblName";

//  echo " ========================  " . $PageName . " ======================== <br> ";

// if ($IDc==0){ echo "nea eggrafi".'<br>'; }	
 
if ($tblName==''){exit();}
  $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 . ", ";
	}	
}

	echo $IDc . " ---- " . $AddRecValues;
	
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;

	echo  $sql;
	
	if($conn->query($sql)===TRUE) {
	   $IDc = $conn->insert_id;
	   echo ".";
	   # 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;	
		if ($conn->query($sql) === TRUE) {
			#       echo "Record updated successfully";	
			} else {
				 echo "Error updating record: " . $conn->error;
			}	
} 

# echo  '<br> SQL:<br>' . $sql .'<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>";
 
$conn->close();
 
//echo "SendTo=" . $SendTo. "    Subject=" . $Subject. "   Message=" . $Message.   "<br><br>";

// ========================================================
//
// ========================================================
//$path = 'C:\gcs2000\Pel2012\logos\OfficeManagerLogo.2024.png';
//$type = pathinfo($path, PATHINFO_EXTENSION);
//$data = file_get_contents($path);
//$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

$img_file = 'C:\gcs2000\Pel2012\logos\OfficeManagerLogo.2024.png';
$imgData = base64_encode(file_get_contents($img_file));
// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;
echo '<img src="'.$src.'">';

$mail = new PHPMailer;

$mail->setLanguage('el', 'PHPMailer/language/');

$mail->IsSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'visualsoft.gr';                 // Specify main and backup server
$mail->Port = 465;                                    // Set the SMTP port
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'vaggelis.t@visualsoft.gr';                // SMTP username
$mail->Password = '0~Cu6wm7';                  // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted

$mail->addCC($SendToCC, '');
//$mail->addBCC('bcc1@example.com', 'Alex');
// Adding more BCC recipients
//$mail->addBCC('bcc2@example.com', 'Anna');
//$mail->addBCC('bcc3@example.com', 'Mark');

// Email content

//$mail->addEmbeddedImage('C:\gcs2000\Pel2012\logos\AADE.jpg', 'image_cid');
//$mail->addAttachment('C:\gcs2000\Pel2012\PDFs\Δ.ΑΠΟΣΤΟΛΗΣ No. 00002 (ΣΧΟΛ. ΕΠΙΤΡ. 2ου ΔΗΜΟΤΙΚΟΥ ΣΧΟΛΕΙΟΥ ΑΛΕΞ-ΠΟΛΗΣ).pdf', 'invoice1.pdf');

$mail->addEmbeddedImage('C:/gcs2000/Pel2012/logos/AADE.jpg', 'image_cid');
$mail->addAttachment('C:/gcs2000/Pel2012/PDFs/Δ.ΑΠΟΣΤΟΛΗΣ No. 00002 (ΣΧΟΛ. ΕΠΙΤΡ. 2ου ΔΗΜΟΤΙΚΟΥ ΣΧΟΛΕΙΟΥ ΑΛΕΞ-ΠΟΛΗΣ).pdf', 'invoice1.pdf');

$mail->isHTML(true); // Set email format to HTML
$mail->CharSet = "UTF-8";	
$mail->From = 'vaggelis.t@visualsoft.gr'; // vaggelis.t@visualsoft ;
$mail->FromName = "Visualsoft Office Manager";
$mail->AddAddress($SendTo, "Vaggelis");  // Add a recipient 
//$mail->AddReplyTo("replies@example.com", "Replies for my site")
$mail->AddEmbeddedImage('C:\gcs2000\Pel2012\logos\OfficeManagerLogo.2024.png', 'logoimg', 'logo.jpg');

$mail->Subject = $Subject;
$mail->Body    =  "
           <html>
                <head>
				<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
				</head>
            <body>
                <b>Θέμα: </b> $Message <br>
                <b>web: </b>  www.visualsoft.gr <br>
                <b>email: </b>  info@visualsoft.gr <br>
				<br>
				<img src='$src'>
				<br>
				<img src=\"cid:logoimg\" />
			
            </body>
			" ;
			
// move_uploaded_file($file_tmp,"uploads/".$file_name);
 
//$mail->addAttachment($file1Path, $file1Name);
//$mail->addAttachment($file2Path, $file2Name);
//$mail->addAttachment($file3Path, $file3Name);

$Msg="";

 
if(!$mail->Send()) {
   $_SESSION['MailSendError'] = $mail->ErrorInfo ;
    echo "<script> history.go(-1);location.reload();</script>";
   exit;
}

 echo"<br>";
 
echo "<script> history.go(-1);location.reload();</script>";	
 
//  echo $CloseType  . "<br><br>";
//  echo  '<br> SQL:<br>' . $sql .'<br><br><br> CloseType=' . $CloseType;
?>


</body> 

 