<?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){
	
      $inv_id = $_POST['inv_id'];
	  //$due_date = $_POST['dueDate'];
	  //$dueDate = date('Y-m-d', strtotime($due_date));
	  //$due_Date = date('d-m-Y', strtotime($due_date));
	  //$mailfrom = 'kreen@gmail.com';
			
		//use template		
		ob_start();
		require("invoice2.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		$token = $invNo.rand(10,9999);
        //$link = "<a href='localhost/iws/confirmInvoice.php?key=".$inv_id."&token=".$token."'> Click This link to Confirm invoice</a>";		

		
		$dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientInvoice.pdf';	
		$pdfoutput = $dompdf->output();
		//$dompdf->stream("FomuYaMaombiYaMkopo.pdf");
		
		
     
 $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 Invoice';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = 'Please attachment of your pdf invoice'.'&nbsp&nbsp'.$link;
 $mail->Body  = 'Please attachment of your pdf invoice';
 
 
 if($mail->Send())        //Send an Email. Return true on success or false on error
 {
	 
  $result22=$conn->query("UPDATE invoices SET inv_status=1 WHERE inv_id = '$inv_id'");

  if($result22){
	  
  
  $_SESSION['success_message'] = "Invoice has been sent to client email successfully";
  header("Location: viewInvoices.php");
  exit();
  
     }
  
 }else{
	echo 'Error'; 
 }
 


} // /if $_POST
// echo json_encode($valid);