<?php
include 'session.php';
date_default_timezone_set("Asia/Kolkata");
if(isset($_POST['btnsub'])){
	$bdd=new DB();
	$handle = opendir(dirname(realpath(__FILE__)).'/ScanedImagesTemp/');
	$scan = array();
	$myObj = new stdClass();
	$cnt=1;
	while($file = readdir($handle)){
		if(strlen($file) > 7 ){
			if(strpos($file, 'not-applicable')!== false) {
			}else{
				//$scan[] = $file;
				//echo "<br>File2:".$file;
				$myObj->$cnt = $file;
				$cnt++;
			}
		}
	}
	$myJSON = json_encode($myObj);
	$base64_str=base64_encode($myJSON);
	$output=shell_exec("python compress.py " .$base64_str);
	//echo "output:".$output;
	sleep(5);
	//if($output=="done"){
		$id = $_REQUEST['id'];
		if(count($_POST["docName"]) !== count(array_unique($_POST["docName"]))){
			echo "<script>alert('Duplicate Name Found!!!!');location.href='scanImage.php?id=".$id."'</script>";	
		}
				
		$q = "SELECT COUNT(*) FROM menu";
		$num_rows = $bdd->getOne($q);
		echo $num_rows['COUNT(*)'];

		$pathA = $_REQUEST['pathA'];
		
		$flag =$_REQUEST['flag'];
	  
		echo "<pre>";print_r($_POST);echo "</pre>";
		print_r($_POST['docName']);
		for($i = 0; $i < $num_rows['COUNT(*)']; $i++){
			if(($_POST['docName'][$i])!=NULL){
			echo "<br/>";
			echo $i;
			echo "<br/>";
			//foreach($_POST['txt'][$i]['img'] as $img){ echo $img;}
			// exit;
		
			if(isset($_POST['convert'][$i]) && $_POST['convert'][$i] == 'jpg'){
				// echo '<br>';
				$destinationFilePath = $pathA.'/' . $_POST['docName'][$i]. '.jpg';
				//print_r($_POST['txt'][$i]['img']);
				foreach($_POST['txt'][$i]['img'] as $img){
					echo $i."-".$img;
					$filePath =  "ScanedImagesTemp/".$img;
					// print_r($filePath);
					copy($filePath, $destinationFilePath);
				}
			}else if(isset($_POST['convert'][$i]) && $_POST['convert'][$i] == 'pdf'){
					// echo $filePath =  $_POST['txt'][$i]['img'];
					// echo '<br>';
					$destinationFilePath = $pathA.'/' . $_POST['docName'][$i]. '.pdf';
					include_once './generate_pdf/mpdf60/mpdf.php';
					$mpdf = new mPDF();
					// $uploaded = [];//New empty array for holding uploaded images
					// foreach ($_POST['txt'][$i]['img'] as $key => $tmp_name) {
					//   $uploaded[] = file_get_contents($filePath);
					$pdf = new mPDF();
				
					foreach($_POST['txt'][$i]['img'] as $img){
						//  echo "<center><img src='ScanedImagesTemp/".$img."' /></center><pagebreak>";   
					$pdf->WriteHTML("<center><img src='ScanedImagesTemp/".$img."' /></center>");
					}  
					$pdf->Output($destinationFilePath,'F');
					// }
				}else{
					echo 'please select convert type';
				}
				$q="INSERT INTO `customer_menu` (`customer_menu_id`, `customer_id`, `menu_id`, `menu_option_id`, `filename`, `added_by`) VALUES (NULL, {$id}, {$_POST['menu_id'][$i]}, '', '{$_POST['docName'][$i]}.{$_POST['convert'][$i]}', '{$_SESSION['admin_id']}')";
				//echo $q;
				$row=$bdd->getOne($q);
				// print_r($row); // aa formate ma showw karvanuc chhe
			}
		}
		$filesa=glob('ScanedImagesTemp/*');
		foreach($filesa as $fils){
			if(is_file($fils)){
				unlink($fils);
			}else{
				echo 'photos is not deleted from ScanedImagesTemp';
			}
		}
		
		if($_POST['hidden_compare_data']!=""){
			$date=date('Y-m-d');
			//$bdd->execute("UPDATE customer SET compare_data='".$_POST['hidden_compare_data']."' WHERE customer_id='$id'");
			$string=addslashes($_POST['hidden_compare_data']);
			$bdd->execute("INSERT INTO `compare_data` (`customer`, `date`, `data`) VALUES ({$id},'$date', '{$string}')");
		}
		
		//Get address proof from customer directory
		$cust_files = array();
		$cust_files = glob($pathA."/*.*");
		$address_proof_name="";
		foreach($cust_files as $item){
			$full_filename=explode(".",basename($item))[0];
			if (strpos($full_filename,"AddressProof") !== false) {
				$address_proof_name=str_replace('AddressProof', '', $full_filename);
			}
		}
		
		//get data from json_decode
		$party_name="";
		$insurance_date="";
		$chassis_no="";
		$engine_no="";
		$hpa="";
		$year_of_manuf="";
		
		if($_POST['hidden_compare_data']!=""){
			/*$json_data=json_decode($_POST['hidden_compare_data']);
			$party_name=$json_data->dname." ".$json_data->last_name;
			$insurance_date=date('Y-m-d',strtotime($json_data->ins_date));
			$chassis_no=$json_data->dchassis;
			$engine_no=$json_data->dengin;
			$hpa=$json_data->dhypo;
			$year_of_manuf=$json_data->year_of_manuf;*/
			$json_data=json_decode($_POST['hidden_compare_data']);
			$party_name=$json_data->dname." ".$json_data->last_name;
			$party_name = preg_replace('/[^\p{L}\p{N}\s\.\,\-\_\@\!\?\:]/u', '', $party_name);
			$insurance_date=date('Y-m-d',strtotime($json_data->ins_date));
			$chassis_no=$json_data->dchassis;
			$chassis_no = preg_replace('/[^\p{L}\p{N}\s\.\,\-\_\@\!\?\:]/u', '', $chassis_no);
			$engine_no=$json_data->dengin;
			$engine_no = preg_replace('/[^\p{L}\p{N}\s\.\,\-\_\@\!\?\:]/u', '', $engine_no);
			$hpa=$json_data->dhypo;
			$hpa = preg_replace('/[^\p{L}\p{N}\s\.\,\-\_\@\!\?\:]/u', '', $hpa);
			$year_of_manuf=$json_data->year_of_manuf;
		}
		
		$bdd->execute("INSERT INTO `scanner`.`vehicle_details` (`customer_id`,`party_name`, `insurance_date`, `chassis_no`, `engine_no`, `address_proof`, `hpa`, `year_of_mfg`) VALUES ('{$id}', '$party_name', '$insurance_date', '$chassis_no', '$engine_no', '$address_proof_name', '$hpa','$year_of_manuf')");
		
		echo '<script>alert("image uploaded successfully");location.href="scanImage.php?id='.$id.'"</script>';
	/*}else{
		echo "error while compressing image";
	}*/
	$bdd->disconnect();
}
?>