<?php
include 'session.php';
    if(isset($_POST["imageData"])) {
        $pdfData="";
        $imageData = $_POST["imageData"];
        $name = $_GET['file'];
        $path = $_GET['path'];
        $id = $_GET['id'];
		$status = $_GET['status'];
        $menuId = $_GET['menuId'];
        
		if($name!='taxReceipt' && $name!='SMCtaxReceipt'){
			if(isset($_GET['upload_type'])){
				if($_GET['upload_type']=="all"){
					$pdfData = base64_decode($imageData);
				}
			}else{
				$imageData = str_replace('data:image/png;base64,', '', $imageData);
				$imageData = str_replace(' ', '+', $imageData);
				$imageData = base64_decode($imageData);
				$filename = 'page.jpg';
				file_put_contents($filename, $imageData);
			
				include_once './generate_pdf/mpdf60/mpdf.php';
				$mpdf = new mPDF();
				$pdf = new mPDF('', 'A4', 0, '', 0, 0, 0, 0, 0, 0);
				$pdf->WriteHTML("<center><img src='".$filename."' style='margin:0;padding:0;'/></center>");
				$pdfData = $pdf->Output('', 'S');
			}
			
		}else{
			$pdfData = base64_decode($imageData);
		}
        
        
    
        $destination_folder = $path;
        if (!file_exists($destination_folder)) {
            mkdir($destination_folder, 0777, true);
        }
        $destination_file = $destination_folder . '/' . $name . '.pdf';
        file_put_contents($destination_file, $pdfData);
        $filename = $name . '.pdf';
        if($menuId!='null')
        {
            $bdd=new DB();
            $q="INSERT INTO `customer_menu` (`customer_menu_id`, `customer_id`, `menu_id`, `menu_option_id`, `filename`, `added_by`) VALUES (NULL, {$id}, {$_GET['menuId']}, '', '{$filename}', '{$_SESSION['admin_id']}')";
            
            $row=$bdd->getOne($q);
            
            $bdd->disconnect();
        }
        else 
        { 
            include "connect.php";
            echo $filename;
            if($filename=='taxReceipt.pdf'){
                $q='UPDATE customer SET taxReceipt = "taxReceipt.pdf" where customer_id ='.$id;
            }else if($filename=='SMCtaxReceipt.pdf'){
                $q='UPDATE customer SET SMCtaxReceipt = "SMCtaxReceipt.pdf" where customer_id ='.$id;
            }else{
                $q='UPDATE customer SET crtm = "crtm.pdf" where customer_id ='.$id;
            }
            mysqli_query($con,$q);
        }
        echo 'File uploaded successfully';
		if($name=='SMCtaxReceipt'){
			header("Location:customer?status=$status");
		}else{
			header("Location: ".$_SERVER['HTTP_REFERER']);
		}
        //header("Location: ".$_SERVER['HTTP_REFERER']);
        exit;
    }
?>