<?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'];
  $assign_id = $_POST['assign_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['dest_id'];
  //$assigned_to  = $_POST['returnedto_id'];
  //$remarks = addslashes($_POST['remarks']);
  $space = " ";
  $eta = date('Y-m-d', strtotime($eTa));
  $currencyId = $_POST['currency_id'];
  $userId = $_SESSION['user_id'];
  $perm_id = 95;
  
  $quotationDate = date("Y-m-d");
  
   $subTotalValue  = $_POST['subTotalValue'];
  
  $sql8123 = "SELECT * FROM quotation";
  $result8123 = $conn->query($sql8123);
  $num22 = mysqli_num_rows($result8123);
  //$invo_no = $count + 1;
  
  if($num22>=0 && $num22<9){
  $quot_no = $num22 + 1;
  $y = date("Y");
  $quotNo = "QUOTBRVSN"."/".$y."/".'000'.$quot_no;  
    }else if($num22>=9 && $num22<99){
   $quot_no = $num22 + 1;
   $y = date("Y");
  $quotNo = "QUOTBRVSN"."/".$y."/".'00'.$quot_no;  	 
	 }else if($num22>=99 && $num22<999){
   $quot_no = $num22 + 1;
   $y = date("Y");
  $quotNo = "QUOTBRVSN"."/".$y."/".'0'.$quot_no;  	 
	 }else{
	$quot_no = $num22 + 1;
	$y = date("Y");
  $quotNo = "QUOTBRVSN"."/".$y."/".$quot;  	 
	 }
				
				
  
  
  $sql812 = "SELECT vat_settings.rate FROM vat_settings";

				$result812 = $conn->query($sql812);
				//$data81 = $result81->fetch_row();
				
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
  
  
  $vat = $vat_rate/100 * $subTotalValue;
  $grand_total_Value = $subTotalValue + $vat;
	
  
    $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',dest_id='$destination',goods='$goods' WHERE file_id = {$file_id}";		

	$result = $conn->query($sql);
	
	if($result){
		
	$sql2 = "INSERT INTO quotation (quot_no,client_id,file_id,currency_id,quot_date,total_amount,vat,grand_total_amount,vat_rate) VALUES ('$quotNo','$client_id','$file_id','$currencyId','$quotationDate','$subTotalValue','$vat','$grand_total_Value','$vat_rate')";			
	$result2 = $conn->query($sql2);
	
	if($result2){
		
	$quot_id = $conn->insert_id;
		
		
		for($x = 0; $x < count($_POST['item_desc']); $x++) {		
	$invItemSql = "INSERT INTO quotation_items (quot_id,services_desc,unit_price,amount) 
				VALUES ('$quot_id','".$_POST['item_desc'][$x]."','".$_POST['rate'][$x]."','".$_POST['totalValue'][$x]."')";

	$result11 = $conn->query($invItemSql);
	
	
	
	
	}
	
	
	
	//$sql26 = "INSERT INTO assignstatus (client_id,file_id,assigned_to,assigned_by,permission_id,status,action,remarks) VALUES ('$client_id','$file_id','$assigned_to','$userId','$perm_id','0','returned','$remarks')";			
	//$result26 = $conn->query($sql26);
	
	$sql29 = "UPDATE assignstatus SET status = '1' WHERE id = {$assign_id}";		

	$result29 = $conn->query($sql29);
		
		
	if($result29){
		
		ob_start();
		require("../customerFile.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 = 'CustomerFile.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 = 'Bright Vision';   //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 = 'Customer File';   //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 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 Opened and Sent To Client Email Successfully with attachment";
                       //header("Location: ../openFile.php");
					   header("Location: ../updateCustFile.php?i=$file_id&assignId=$assign_id");
                             exit();   	  
 }
		
		
		
				//$_SESSION['success_message'] = "Customer File Has Been Updated Successfully";
					   //header("Location: ../updateCustFile.php?i=$file_id&assignId=$assign_id");
                             //exit(); 
							 
							 
	}							 
                 
	}
	
	}
	

}