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

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


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

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

$uploaddir;
$uploaddir = "C:\\wamp64\\www\\".$Folder."\\temp_".$dvc_id."\\";



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

$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\\";

$sql = "SELECT wko_ref.*  
		from wko_ref (NOLOCK),wko_det (NOLOCK) ,wko_mst (NOLOCK) 
		where wko_ref.site_cd ='".$site_cd."' 
		and 	wko_ref.site_cd = wko_det.site_cd
		and 	wko_ref.mst_RowID= wko_det.mst_rowid
		and 	wko_ref.site_cd = wko_mst.site_cd
		and 	wko_ref.mst_RowID= wko_mst.RowID
		and 	wko_det_assign_to = '".$EmpID."'
		and		wko_mst_status  not  in ('CMP')
		and DataLength(attachment) > 0		
		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 (wko_ref)";
	 returnError($error_message);
     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'];
		$mst_RowID = $row['mst_RowID'];
		$site_cd = $row['site_cd'];
		$column2	 = $row['column2'];
	   
	
		$extension = strtolower(pathinfo($fileName,PATHINFO_EXTENSION));

		// Valid file extensions
		$extensions_arr = array("mp4","avi","3gp","mov","mpeg");
		
		if( in_array($extension,$extensions_arr) ){
			 
			$exportFullPathFileName = $exportdir.$fileName;
			$exportFullPathTNFileName = $exportdirtn.$fileName;
			  
			$publicFullPathFileName = $MobileURLFullSize.$fileName;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$fileName;
			
			writeFile($exportFullPathFileName, $Photo);
			$json_img_link['site_cd'] = $site_cd;
			$json_img_link['rowid'] = $rowid;
			$json_img_link['column2'] = $column2;
			$json_img_link['mst_RowID'] = $mst_RowID;
			$json_img_link['type'] = 'video';
			$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);
			 
		}else{
			 
			$exportFullPathFileName = $exportdir.$fileName;
			$exportFullPathTNFileName = $exportdirtn.$fileName;
				 
			$publicFullPathFileName = $MobileURLFullSize.$fileName;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$fileName;
			
			if (strpos($fileName, 'pdf') == true){	
			
				writeFile($exportFullPathFileName, $Photo);
				
				$json_img_link['site_cd'] = $site_cd;
				$json_img_link['rowid'] = $rowid;
				$json_img_link['column2'] = $column2;
				$json_img_link['mst_RowID'] = $mst_RowID;
				$json_img_link['type'] = 'video';
				$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);
			}else if (strpos($fileName, 'docx') == true){	
		
			}else{
				
				
				try {
					// Write the original file
					writeFile($exportFullPathFileName, $Photo);

					// Resize and write the thumbnail
					$maxWidth = 250;  // Maximum width of the thumbnail
					$maxHeight = 250; // Maximum height of the thumbnail
					$thumbnail = resizeImage($Photo, $maxWidth, $maxHeight);
					$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
					switch ($fileExtension) {
						case 'jpeg':
						case 'jpg':
						case 'jfif':
							imagejpeg($thumbnail, $exportFullPathTNFileName, 75); // Quality: 0-100
							break;
						case 'png':
							imagepng($thumbnail, $exportFullPathTNFileName, 6);  // Compression level: 0-9
							break;
						case 'gif':
							imagegif($thumbnail, $exportFullPathTNFileName);
							break;
						
						default:
							throw new Exception("Unsupported image format: " . $fileExtension);
					}
					imagedestroy($thumbnail);
				} catch (Exception $e) {
					error_log($e->getMessage());
					echo "Error processing image";
				}
					
				$file_name = generateUniqueName($fileName,$rowid);
			
			  $json_img_link['site_cd'] = $site_cd;
			  $json_img_link['rowid'] = $rowid;
			  $json_img_link['mst_RowID'] = $mst_RowID;
			  $json_img_link['column2'] = $column2;
			  $json_img_link['type'] = 'image';
			  $json_img_link['file_name'] = $file_name;
			  $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));

function generateUniqueName($file_name,$image_count) {
	
    $unique_name = "WO_Ref_image_" . $image_count . ".jpeg"; 
    return $unique_name;
}

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 Order Attachment Retrieve Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

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

?>
