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

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


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

$MobileURL = $_REQUEST['url'];
$Folder = $_REQUEST['folder'];
$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_mst, wko_det, wrk_sts, wko_ref
		where 	wko_mst.site_cd = wko_ref.site_cd
		and 	wko_mst.rowid= wko_ref.mst_rowid
		and 	wko_mst.site_cd = wko_det.site_cd
		and 	wko_mst.rowid= wko_det.mst_rowid
		and 	wko_mst.site_cd = wrk_sts.site_cd
		and 	wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
		AND		(wrk_sts.wrk_sts_typ_cd NOT IN ('CLOSE', 'COMPLETE', 'FORCE-CLOSE', 'CANCEL'))
		and		wko_ref.column2 = 'SIGN'
		and 	wko_det_assign_to = '".$EmpID."'";

$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['mst_RowID'];
          $site_cd = $row['site_cd'];
		   
		  // echo  $fileName;
		   
		   
		   // 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) ){
				 
				 $exportFullPathFileName = $exportdir.$fileName;
				 $exportFullPathTNFileName = $exportdirtn.$fileName;
				  
				  // 	echo $exportFullPathFileName;
				  //echo $exportFullPathTNFileName;
				  
				  $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
				  $publicFullPathTNFileName = $MobileURLTnSize.$fileName;
				  
				   $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
				   $publicFullPathTNFileName = $MobileURLTnSize.$fileName;	 
				   
				   
				 If ($file = fopen($exportFullPathFileName,"w")) {
						if (fwrite($file, $Photo)==false) {
						echo "Error Write Files";
						}
						fclose($file);
					}	

				If ($file = fopen($exportFullPathTNFileName,"w")) {
						if (fwrite($file, $Photo)==false) {
						echo "Error Write Files";
						}
						fclose($file);
					}		  
				  
				   //$file = fopen($exportFullPathTNFileName,"w"); 
				 
				  // echo $publicFullPathFileName;
				  $json_img_link['site_cd'] = $site_cd;
				  $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';
				  array_push($json_return, $json_img_link);
				 
			 }else{
				 
				 if (strpos($fileName, 'jpg') == true){
						$mime = 'image/jpg';
				  }

				  if (strpos($fileName, 'jpeg') == true){
					   $mime = 'image/jpeg';
				  }

				  if (strpos($fileName, 'gif') == true){
					   $mime = 'image/gif';
				  }

				  if (strpos($fileName, 'png') == true){
					   $mime = 'image/png';
				  }
				  if (strpos($fileName, 'pdf') == true){
					   $mime = 'image/pdf';
				  }	  
				  
				 
				  

				$exportFullPathFileName = $exportdir.$fileName;
				$exportFullPathTNFileName = $exportdirtn.$fileName;
				  
				  //echo $exportFullPathFileName;
				   //echo $exportFullPathTNFileName;
				  
				  $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
				  $publicFullPathTNFileName = $MobileURLTnSize.$fileName;
				  
				   $publicFullPathFileName = $MobileURLFullSize.$fileName;		  
				   $publicFullPathTNFileName = $MobileURLTnSize.$fileName;	  
				   //echo $publicFullPathFileName;

				  //Export and create full size file
				  //$file = fopen($exportFullPathFileName,"w");         
				  //fwrite($file, $Photo);
				  //fclose($file);
				   //echo $fileName;
				  //echo explode('.',$fileName,0);
				  
				 
				  if (file_exists($exportFullPathFileName)){
					  $counter++;
					   $newname  = str_replace(".jpg","_".$counter.".jpg",$fileName);
						//echo  $newname;
					  
					  $exportFullPathFileName = $exportdir.$newname;
					  $exportFullPathTNFileName = $exportdirtn.$newname;
					  
					  
					$publicFullPathFileName = $MobileURLFullSize.$newname;	  
					$publicFullPathTNFileName = $MobileURLTnSize.$newname;
				  
					$publicFullPathFileName = $MobileURLFullSize.$newname; 
					$publicFullPathTNFileName = $MobileURLTnSize.$newname;
					  
					  //echo $newname;
					  
				  }
				  If ($file = fopen($exportFullPathFileName,"w")) {
						if (fwrite($file, $Photo)==false) {
						echo "Error Write Files";
						}
						fclose($file);
					}		  
				  
				   $file = fopen($exportFullPathTNFileName,"w"); 
				   
				if (strpos($fileName, 'pdf') == true){
					   
				  }else{
					  
					   if (file_exists($exportFullPathFileName)) {
					  list($width, $height, $type) = getimagesize($exportFullPathFileName);
						$imageObject = load_image($exportFullPathFileName, $type);
						# Get exif information
						//$exif = exif_read_data($exportFullPathFileName);
						$exif = exif_imagetype($exportFullPathFileName);
					}else {
						echo "File not exist";
						}
						
					if (!empty($exif['Orientation'])) {
						# Get orientation
						
						$orientation = $exif['Orientation'];
					}else{
						$orientation = 0 ;
						//echo "0";
					}
					
					switch ($orientation) {
						case 2:
							imageflip($imageObject, IMG_FLIP_HORIZONTAL);
							break;
						case 3:
							$imageObject = imagerotate($imageObject, 180, 0);
							break;
						case 4:
							imageflip($imageObject, IMG_FLIP_VERTICAL);
							break;
						case 5:
							$imageObject = imagerotate($imageObject, -90, 0);
							imageflip($imageObject, IMG_FLIP_HORIZONTAL);
							break;
						case 6:
							$imageObject = imagerotate($imageObject, -90, 0);
							break;
						case 7:
							$imageObject = imagerotate($imageObject, 90, 0);
							imageflip($imageObject, IMG_FLIP_HORIZONTAL);
							break;
						case 8:
							$imageObject = imagerotate($imageObject, 90, 0); 
							break;
					}
				  # Write image
					imagejpeg($imageObject, $exportFullPathTNFileName, 20);
				  
				  fclose($file);	
					  
				  }	  


				 // echo $publicFullPathFileName;
				  $json_img_link['site_cd'] = $site_cd;
				  $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));

echo returnData($json_return);

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

// $b64Src = "data:" . $mime . ";base64," . base64_encode($Photo);
// echo '<img src="' . $b64Src . '" alt="" />';

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
