<?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'];

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

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

$sql;

if($table_name == 'ast_ref')
{
	$sql = "SELECT RowID, attachment, file_name,column2,audit_date  from ast_ref (NOLOCK) 
		where site_cd ='".$site_cd."' 
		And rowid ='".$rowid."'
		And DataLength(attachment) > 0 
		And	DataLength(attachment) is not null
		And mst_RowID is not null order by rowid desc";
	
}
else if($table_name == 'wkr_ref')
{
	$sql = "SELECT RowID, attachment, file_name, column2,audit_date  from wkr_ref (NOLOCK) 
			where site_cd ='".$site_cd."' 
			And rowid ='".$rowid."' 
			And DataLength(attachment) > 0 
			And	DataLength(attachment) is not null
			And rowid is not null";
	
	
} 
else if($table_name == 'wko_ref')
{
	$sql = "SELECT RowID, attachment, file_name,column2,audit_date  from wko_ref (NOLOCK) 
			where site_cd ='".$site_cd."'
			And rowid ='".$rowid."' 
			And DataLength(attachment) > 0 
			And	DataLength(attachment) is not null
			And mst_RowID is not null
			And COALESCE(column2,'') <> 'SIGN' 
			And COALESCE(column2,'') <> 'RESPONSE_SIGN' 
			And COALESCE(column2,'') <> 'Checklist'
			order by rowid asc";
	
	
} 
else if($table_name == 'itm_ref')
{
	$sql = "SELECT RowID, attachment, file_name,column2,audit_date  from itm_ref (NOLOCK) 
			where site_cd ='".$site_cd."'
			And rowid ='".$rowid."' 
			And DataLength(attachment) > 0 
			And	DataLength(attachment) is not null
			order by rowid asc";
	
	
} 


$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();
$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'];
		$audit_date = $row['audit_date'];

		// Select file type
		$extension = strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
		// Valid file extensions
		$videoFormats = array("3gp", "avi", "mov", "mp4", "mpeg", "m4v", "mkv", "flv", "webm", "wmv");
		
		
		$documentFormats = array("pdf","doc", "docx", "epub", "odt", "txt", "rtf", "html", "csv", "xlsx", "ppt", "pptx", "ps", "txt", "json", "xml", "yaml", "odf", "xps");
		
		
		if (in_array($extension, $videoFormats)) {
			
			
			$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['file_format'] = '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;
			$json_img_link['audit_date'] = $audit_date;
			array_push($json_return, $json_img_link);
			
		} else if (in_array($extension, $documentFormats)) {
			
			if (strpos($fileName, 'pdf') !== false){
				
				$file_name = generateUniquePDFName($fileName,$rowid);
				
				$exportFullPathFileName = $exportdir.$file_name;
				$exportFullPathTNFileName = $exportdirtn.$file_name;
					 
				$publicFullPathFileName = $MobileURLFullSize.$file_name;		  
				$publicFullPathTNFileName = $MobileURLTnSize.$file_name;
				
				if (!file_exists($exportFullPathFileName)) {
					// If the full-size file does not exist, write the file
					writeFile($exportFullPathFileName, $Photo);
				}
				
				// echo $publicFullPathFileName;
				$json_img_link['rowid'] = $rowid;
				$json_img_link['file_format'] = '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;
				$json_img_link['audit_date'] = $audit_date;
				array_push($json_return, $json_img_link);			
				
			}else{
				
				
				// echo $publicFullPathFileName;
				$json_img_link['rowid'] = $rowid;
				$json_img_link['file_format'] = 'File Format';
				$json_img_link['file_name'] = $fileName;
				$json_img_link['thumbnail_link'] = Null;
				$json_img_link['full_size_link'] = Null;
				$json_img_link['file_source'] = 'server';
				$json_img_link['column2'] = $column2;
				$json_img_link['audit_date'] = $audit_date;
				array_push($json_return, $json_img_link);
				
			}
			
		} else {
			
			// echo $publicFullPathFileName;
			$json_img_link['rowid'] = $rowid;
			$json_img_link['file_format'] = 'File Format';
			$json_img_link['file_name'] = $fileName;
			$json_img_link['thumbnail_link'] = Null;
			$json_img_link['full_size_link'] = Null;
			$json_img_link['file_source'] = 'server';
			$json_img_link['column2'] = $column2;
			$json_img_link['audit_date'] = $audit_date;
			array_push($json_return, $json_img_link);
			
		}
		 

     }
} while (sqlsrv_next_result($stmt));
sqlsrv_free_stmt($stmt);
sqlsrv_close( $conn);



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

function generateUniquePDFName($file_name,$image_count) {
	
    $unique_name = "CMMS_PDF_" . $image_count . ".pdf"; 
    return $unique_name;
}


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

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Retrieve successfully',
	'data' => $json);
	
	echo json_encode($returnData);
	exit();
}

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

?>