<?php
use Dompdf\Dompdf;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;	   
require __DIR__.'/vendor/autoload.php'; 
session_start();
require_once '../../Server/connection.php';
if($_POST){	
  $file_id = $_POST['file_id'];
  $client_id = $_POST['client_id'];
  $entryId = $_POST['entryId'];
  $entryNameId = $_POST['entryNameId'];
  $b_lNo = $_POST['b_lNo'];
  $tra_RefNo = $_POST['tra_RefNo'];
  $consignee = $_POST['consignee'];
  $goods = $_POST['goods'];
  $vesselFlight = $_POST['vesselFlight'];
  $sline = $_POST['sline'];
  $eTa = $_POST['eta'];
  $destination = $_POST['destination'];
  $file_status = $_POST['file_status'];
  $exp_status  = $_POST['exp_status'];
  $upl_status  = $_POST['upl_status'];
  $message = addslashes($_POST['message']);
  $space = " ";
  $eta = date('Y-m-d', strtotime($eTa));
  $link = "<a href='https://cfms.kreenlogistics.co.tz/'>cfms</a>";	
  
  if($file_status=='Yes'){
		$st = 1;
		$fileCloseDate = date("Y-m-d");
	}else{
		$st = 0;
		$fileCloseDate = '0000-00-00';
	 }
	 
	
  
    $sql = "UPDATE client_files SET client_id = '$client_id',entryId = '$entryId',entryName_id='$entryNameId',b_lNo='$b_lNo',tra_RefNo='$tra_RefNo',consignee='$consignee',vessel_flight='$vesselFlight',sline='$sline',eta='$eta',destination='$destination',goods='$goods',file_closeDate='$fileCloseDate',fileStatus='$st',expStatus='$exp_status',uplStatus='$upl_status' WHERE file_id = {$file_id}";		

	$result = $conn->query($sql);
	
	if($result){
		
		
		 if($upl_status=='Yes' && $exp_status=='YES'){
			 
		for($x = 0; $x < count($_POST['standardexp_id']); $x++) {			
		$removeInvSql = "DELETE FROM client_fileexpenses WHERE file_id = {$file_id}";
		$result1 = $conn->query($removeInvSql);
              }
	
	 //if($result1) {
		 
		 for($x = 0; $x < count($_POST['standardexp_id']); $x++) {		
	$invItemSql = "INSERT INTO client_fileexpenses (file_id,standardexp_id,amount,chargeable_to,invoice,debitNote) 
				VALUES ('$file_id','".$_POST['standardexp_id'][$x]."','".$_POST['amount'][$x]."','".$_POST['chargeable_to'][$x]."','".$_POST['invoice'][$x]."','".$_POST['debitNote'][$x]."')";

	$result11 = $conn->query($invItemSql);
	
	
		
	}
	
		//if($result11){
		
		ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;
		
		$dompdf = new DOMPDF();
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();
		
		 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO client_docs (file_id,fileName) 
				VALUES ('$file_id','$location')";

		$resFSql1 = $conn->query($fSql);
  $mail->AddAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]['name'][$key]);
	          }
 
 $mail->Subject = 'Client File With Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = $message;
 //$mail->Body  = $message.$space.$link.$space."which is".$space.$mailto.$space."and your password to view your files and documents.";
 $mail->Body  = $message.$space."Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$mailto.$space."and your password to view your files and documents.";

 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Client File Has Been Updated and Sent To Client Email Successfully with attachment";
					   header("Location: ../editClientFile.php?i=$file_id");
                             exit();   	  
               } 
		
	          //}
	          //}
	          }else if($upl_status=='No' && $exp_status=='NO'){
				  
				  
				ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();

        //NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;		
		
		$dompdf = new DOMPDF();
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();
		
		 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 $mail->Subject = 'Client File And Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = $message;
 //$mail->Body  = $message.$space.$link.$space."which is".$space.$mailto.$space."and your password to view your files and documents.";
 $mail->Body  = $message.$space."Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$mailto.$space."and your password to view your files and documents.";

 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Client File Has Been Updated and Sent To Client Email Successfully with attachment";
					   header("Location: ../editClientFile.php?i=$file_id");
                             exit();   	  
               }  
				  
			  
			  }else if($upl_status=='No' && $exp_status=='YES'){
				  
				for($x = 0; $x < count($_POST['standardexp_id']); $x++) {			
		$removeInvSql = "DELETE FROM client_fileexpenses WHERE file_id = {$file_id}";
		$result1 = $conn->query($removeInvSql);
              }
	
	 //if($result1) {
		 
		 for($x = 0; $x < count($_POST['standardexp_id']); $x++) {		
	$invItemSql = "INSERT INTO client_fileexpenses (file_id,standardexp_id,amount,chargeable_to,invoice,debitNote) 
				VALUES ('$file_id','".$_POST['standardexp_id'][$x]."','".$_POST['amount'][$x]."','".$_POST['chargeable_to'][$x]."','".$_POST['invoice'][$x]."','".$_POST['debitNote'][$x]."')";

	$result11 = $conn->query($invItemSql);
	
	}
	
		//if($result11){
		
		ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;
		
		$dompdf = new DOMPDF();
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();
		
		 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 
 foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO client_docs (file_id,fileName) 
				VALUES ('$file_id','$location')";

		$resFSql1 = $conn->query($fSql);
  $mail->AddAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
	          }
 
 
 
 $mail->Subject = 'Client File And Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = $message;
 //$mail->Body  = $message.$space.$link.$space."which is".$space.$mailto.$space."and your password to view your files and documents.";
 $mail->Body  = $message.$space."Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$mailto.$space."and your password to view your files and documents.";

 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Client File Has Been Updated and Sent To Client Email";
					   header("Location: ../editClientFile.php?i=$file_id");
                             exit();   	  
               } 
		
	          //}
	          //}  
				  
				  
			  }else{
		
		
		ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;
		
		$dompdf = new DOMPDF();
		
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();
		
		 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO client_docs (file_id,fileName) 
				VALUES ('$file_id','$location')";

		$resFSql1 = $conn->query($fSql);
  //$mail->AddStringAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
  $mail->AddAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
  
  //$mail->AddStringAttachment(chunk_split(base64_encode($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key])));
  
  
  
	          }
 
 
 
 $mail->Subject = 'Client File And Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = $message;
 //$mail->Body  = $message.$space.$link.$space."which is".$space.$mailto.$space."and your password to view your files and documents.";
 $mail->Body  = $message.$space."Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$mailto.$space."and your password to view your files and documents.";

 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Client File Has Been Updated and Sent To Client Email";
					   header("Location: ../editClientFile.php?i=$file_id");
                             exit();   	  
               } 
		
	          }
	           
				  
				  
				  
			  
		
		
	
	}else{
					  
				echo 'No';  
					  
				  }
	

}