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

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

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

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

$uploaddir;
$uploaddir = "C:\\wamp64\\www\\".$Folder."\\mobile_image_temp\\temp_".$LOGINID."_".$dvc_id."\\";
if (!is_dir($uploaddir)) {
		mkdir($uploaddir, 0777, true);
		mkdir($uploaddir."\\thumbnail\\", 0777, true);
}
	

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

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

$counter =0;
$sql = "SELECT RowID, attachment, file_name  from wko_ref (NOLOCK) 
		where site_cd ='".$site_cd."' 
		And mst_RowID ='".$rowid."' 
		And column2 = 'RESPONSE_SIGN' 
		And DataLength(attachment) > 0 
		And	DataLength(attachment) is not null
		And mst_RowID is not null
		And file_name not like '%xlsx%' 
		And file_name not like '%xls%'";

$stmt = sqlsrv_query($conn, $sql);
//echo  $sql;
if (!$stmt) {
     $error_message = "Error selecting table (itm_ref)";

     die(print_r(sqlsrv_errors(), true));
}

$mime = null;
$json_img_link = array();

 /* $json1 = json_encode($json_img_link);
	 echo$json1; */

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
          $Photo = $row['attachment'];
          $fileName = $row['file_name'];
          $rowid = $row['RowID'];
		  

          $exportFullPathFileName = $exportdir.$fileName;
          $exportFullPathTNFileName = $exportdirtn.$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;
			  
			  
		  }
		  
		  If ($file = fopen($exportFullPathFileName,"w")) {
			 if (fwrite($file, $Photo)==false) {
				 echo "Error Write Files";
			 }
			 fclose($file);
		  }
		  
          $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';
          array_push($json_return, $json_img_link);

     }
} while (sqlsrv_next_result($stmt));

if( $stmt ) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData($json_return);	
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Work Request 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);
	exit();
}

?>