<?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'];
$rowid= $_REQUEST['rowid'];
$type= $_REQUEST['type'];
$MobileURL = $_REQUEST['url'];
$Folder = $_REQUEST['folder'];
$dvc_id = $_REQUEST['dvc_id'];


$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 RowID, attachment, file_name,column2  from ast_ref (NOLOCK) 
		where site_cd ='".$site_cd."' 
		And mst_RowID ='".$rowid."'
		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);

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

$mime = null;
$json_img_link = array();
$rowCount = 0;
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		 
          $Photo = $row['attachment'];
          $fileName = $row['file_name'];
          $rowid = $row['RowID'];
		  $column2 = $row['column2'];
		  
		  $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
		  $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;
				
				  if (!file_exists($exportFullPathFileName)) {
					// If the full-size file does not exist, write the file
					writeFile($exportFullPathFileName, $Photo);
				  }
				
				$json_img_link['rowid'] = $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';
				$json_img_link['column2'] = $column2;
				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){	
				
					  if (!file_exists($exportFullPathFileName)) {
						// If the full-size file does not exist, write the file
						writeFile($exportFullPathFileName, $Photo);
					  }
					
					
					  $json_img_link['rowid'] = $rowid;
					  $json_img_link['type'] = 'PDF';
					  $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);
				
				}else if (strpos($fileName, 'docx') == true){	
			
				}else{
					
					  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);
					  } 
					  $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));


function generateUniqueName($file_name,$image_count) {
	
    // Get current date in Ymd format (YearMonthDay);
	//$date = date("YmdHis");	
	
	$random_id = rand(100000, 999999);
	
	// Concatenate date and image count with file extension
    $unique_name = "Asset_Ref_image_" . $random_id . ".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' => 'Asset 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);
	
	
}


