<?php
require_once('config.php');
require_once('utility_image.php');

$error_message;
$valid = true;
$json_return = array();
$counter = 0;

//$sql = "Select rowid, attachment, file_name from wko_ref where mst_rowid = ".$_REQUEST['rowid'];

$site_cd = $_REQUEST['site_cd'];
$rowid= $_REQUEST['rowid'];

$MobileURL = $_REQUEST['url'];
$Folder = $_REQUEST['folder'];
$dvc_id = $_REQUEST['dvc_id'];

$MobileURLFullSize = $MobileURL . "/temp_".$dvc_id."/";
$MobileURLTnSize = $MobileURL . "/temp_".$dvc_id."/thumbnail/";

$exportdir	 = "C:\\wamp64\\www\\". $Folder ."\\temp_".$dvc_id."\\";
$exportdirtn = "C:\\wamp64\\www\\". $Folder ."\\temp_".$dvc_id."\\thumbnail\\";

if (!is_dir($exportdir)) {
	mkdir($exportdir, 0777, true);
	mkdir($exportdir."\\thumbnail\\", 0777, true);
}
	



$sql = "SELECT rowid, attachment, file_name,column2 FROM itm_ref(NOLOCK) where site_cd ='".$site_cd."' and mst_RowID ='".$rowid."'";

$stmt = sqlsrv_query($conn, $sql);
//echo  $sql;
if (!$stmt) {
     $error_message = "Error selecting table (itm_ref)";
		returnError($error_message);
     die(print_r(sqlsrv_errors(), true));
}

$mime = null;
$json_img_link = array();
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		 
          $Photo = $row['attachment'];
          $fileName = $row['file_name'];
          $rowid = $row['rowid'];
          $column2 = $row['column2'];
		  
          if (strpos($fileName, 'jpg') == true){
               $mime = 'image/jpg';
          }

          if (strpos($fileName, 'jpeg') == true){
               $mime = 'image/jpeg';
          }

          if (strpos($fileName, 'gif') == true){
               $mime = 'image/gif';
          }

          if (strpos($fileName, 'png') == true){
               $mime = 'image/png';
          }
   
        
		  $exportFullPathFileName = $exportdir.$fileName;
		  $exportFullPathTNFileName = $exportdirtn.$fileName; 
		  
		  $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
		  $publicFullPathTNFileName = $MobileURLTnSize.$fileName;
		  
		  $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
		  $publicFullPathTNFileName = $MobileURLTnSize.$fileName;
		   
		  
			if (file_exists($exportFullPathFileName)){
						  
				$counter++;			  
				$newname  = str_replace(".jpg","_".$counter.".jpg",$fileName);
					
				  
				$exportFullPathFileName = $exportdir.$newname;
				$exportFullPathTNFileName = $exportdirtn.$newname;
				  
				  
				$publicFullPathFileName = $MobileURLFullSize.$newname;	  
				$publicFullPathTNFileName = $MobileURLTnSize.$newname;
			  
				$publicFullPathFileName = $MobileURLFullSize.$newname; 
				$publicFullPathTNFileName = $MobileURLTnSize.$newname;
						  
						 
						  
			}
					  
				If ($file = fopen($exportFullPathFileName,"w")) {
					if (fwrite($file, $Photo)==false) {
						//echo "Error Write 1232 Files";
					}
					 fclose($file);
				}
					  

				  //Export and create thumbnail size file
				  $file = fopen($exportFullPathTNFileName,"w"); 
				  
				  if (strpos($fileName, 'pdf') == true){
					  
				  }else{
					  
						if (file_exists($exportFullPathFileName)) {
							// list($width, $height, $type) = getimagesize($exportFullPathFileName);
							// $imageObject = load_image($exportFullPathFileName, $type);
							// # Get exif information
							//$exif = exif_read_data($exportFullPathFileName);
							// $exif = exif_imagetype($exportFullPathFileName);
						} else {
							echo "File not exist";
						}
					  
						
					  
				  }
				  
				  

				 // echo $publicFullPathFileName;
				  $json_img_link['rowid'] = $rowid;
					  $json_img_link['type'] = 'image';
					  $json_img_link['file_name'] = $fileName;
					  $json_img_link['thumbnail_link'] = $publicFullPathTNFileName;
					  $json_img_link['full_size_link'] = $publicFullPathFileName;
					  $json_img_link['file_source'] = 'server';
					  $json_img_link['column2'] = $column2;
					  array_push($json_return, $json_img_link);

     }
} while (sqlsrv_next_result($stmt));

echo	 returnData($json_return);

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Inventory Location Attachment Retrieve Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
		
	exit();
}

// $b64Src = "data:" . $mime . ";base64," . base64_encode($Photo);
// echo '<img src="' . $b64Src . '" alt="" />';

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
