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

$error_message;
$valid = true;
$counter = 0;
$json_return = array();
/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


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

$uploaddir;
$uploaddir = "C:\\wamp64\\www\\".$Folder."\\temp_".$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($uploaddir)) {
		mkdir($uploaddir, 0777, true);
		mkdir($uploaddir."\\thumbnail\\", 0777, true);
	}

$sql= "	SELECT 	site_cd,wko_ls11_name,wko_ls11_sts_upd, wko_ls11_category, mst_RowID,file_name,attachment,RowID,chatID,audit_user,audit_date
		FROM 	wko_ls11 (NOLOCK) 
		WHERE  	site_cd  = '".$site_cd."' 
		AND mst_RowID = ".$mst_RowID." 
		Order by RowID";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wko_ls11)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

$json = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
     
		  $wko_ls11_name = $row['wko_ls11_name'];
		  $wko_ls11_sts_upd = $row['wko_ls11_sts_upd'];
		  $wko_ls11_category = $row['wko_ls11_category'];
		  $chatID = $row['chatID'];
		  $audit_user = $row['audit_user'];
		  $audit_date = $row['audit_date'];
			  
		 If (!empty($row['file_name'])) {
			 
			 $Photo = $row['attachment'];
			 $fileName = $row['file_name'];
			 $rowid = $row['RowID'];
			 $thumbnail_link;
			 $full_size_link;
			  
			 // Select file type
			$extension = strtolower(pathinfo($fileName,PATHINFO_EXTENSION));

			// Valid file extensions
			$extensions_arr = array("mp4","avi","3gp","mov","mpeg");
			if( in_array($extension,$extensions_arr) ){
				$thumbnail_link = Null;
				$full_size_link =Null;	
			}else{
				$exportFullPathFileName = $exportdir.$fileName;
				$exportFullPathTNFileName = $exportdirtn.$fileName;
					  
				$publicFullPathFileName = $MobileURLFullSize.$fileName;		  
				$publicFullPathTNFileName = $MobileURLTnSize.$fileName;
				
				if (strpos($fileName, '.pdf') !== false) {
				
					$thumbnail_link = Null;
					$full_size_link =Null;
				}else if (strpos($fileName, '.mp4') !== false) {
					
					$thumbnail_link = Null;
					$full_size_link =Null;
				}else if (strpos($fileName, 'docx') == true){	
					
					$thumbnail_link = Null;
					$full_size_link = Null;
		
				}else{
					
					
			
					$exportFullPathFileName = $exportdir . $fileName;
					$exportFullPathTNFileName = $exportdirtn . $fileName;

					$publicFullPathFileName = $MobileURLFullSize . $fileName;
					$publicFullPathTNFileName = $MobileURLTnSize . $fileName;
						
					if (!file_exists($exportFullPathFileName)) {
						// If the full-size file does not exist, write the file
						writeFile($exportFullPathFileName, $Photo);
					 }

					if (!file_exists($exportFullPathTNFileName)) {
						// If the thumbnail does not exist, resize and write the thumbnail
						resizeImageImagick($Photo, $exportFullPathTNFileName);
					} 
					
					$thumbnail_link = $publicFullPathTNFileName;
					$full_size_link =$publicFullPathFileName;	
					
				}
				
				  $json_img_link['wko_ls11_name'] = $wko_ls11_name;
				  $json_img_link['wko_ls11_sts_upd'] = $wko_ls11_sts_upd;
				  $json_img_link['wko_ls11_category'] = $wko_ls11_category;
				  $json_img_link['chatID'] = $chatID;
				  $json_img_link['audit_user'] = $audit_user;
				  $json_img_link['audit_date'] = $audit_date;
				  $json_img_link['thumbnail_link'] = $thumbnail_link;
				  $json_img_link['full_size_link'] = $full_size_link;
				  
				  array_push($json_return, $json_img_link);
					 
			}
				
			 
		 }else{
			 
			$json_img_link['wko_ls11_name'] = $wko_ls11_name;
			$json_img_link['wko_ls11_sts_upd'] = $wko_ls11_sts_upd;
			$json_img_link['wko_ls11_category'] = $wko_ls11_category;
			$json_img_link['chatID'] = $chatID;
			$json_img_link['audit_user'] = $audit_user;
			$json_img_link['audit_date'] = $audit_date;
			$json_img_link['thumbnail_link'] ='';
			$json_img_link['full_size_link'] = '';
		  
			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' => 'Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'Alert',
	'message' => $error_message,
	'data' => $json);	
	
	echo json_encode($returnData);		
	exit();
}
 
?>