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

$error_message;
$valid = true;
$counter = 0;

$json_return = array();
$json_img_link = array();

$json_zoom= array();

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

$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, photo_attachment, photo_file_name from wko_isp (NOLOCK) 
		where 	site_cd ='".$site_cd."' 
		and 	mst_RowID =".$mst_RowID."
		AND 	wko_isp_asset_no = '".$wko_isp_asset_no."'
		AND 	wko_isp_job_cd = '".$wko_isp_job_cd."'";

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

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

//$json_img_link = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		 
		$Photo = $row['attachment'];
		$fileName = $row['file_name'];
		$photo_attachment = $row['photo_attachment'];
		$photo_file_name = $row['photo_file_name'];
		
		$rowid = $row['RowID'];
		
		if(!$fileName == "NULL"){
			
			
			
		}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;
				  
			$publicFullPathFileName = $MobileURLFullSize.$fileName;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$fileName;
			
			$publicFullPathFileName = $MobileURLFullSize.$fileName;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$fileName;	
							   
					 
			if (file_exists($exportFullPathFileName)){
					  
				$counter++;
				$newname  = str_replace(".jpg","_".$counter.".jpg",$fileName);
						
				$exportFullPathFileName = $exportdir.$newname;
				$exportFullPathTNFileName = $exportdirtn.$newname;
			  
				$publicFullPathFileName = $MobileURLFullSize.$newname;	  
				$publicFullPathTNFileName = $MobileURLTnSize.$newname;
				
				$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);
			}		  			
					  
			$sql="UPDATE wko_isp SET	
						file_link ='".$publicFullPathFileName."'
				  WHERE site_cd = '".$site_cd."'
				  AND 	RowID = '".$rowid."' ;";	
				
						
			$Update_wko_isp_stmt = sqlsrv_query( $conn, $sql);

			if( !$Update_wko_isp_stmt ) {
				$error_message = "Error UPDATE IMAGE SELECT Table (wko_isp)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			while ( sqlsrv_next_result($Update_wko_isp_stmt) );
			
			
			
			
		}
		
		if(!$photo_file_name == "NULL"){
			
		}else{
			
			if (strpos($photo_file_name, 'jpg') == true){
						$mime = 'image/jpg';
				}

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

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

				if (strpos($photo_file_name, 'png') == true){
				   $mime = 'image/png';
				}
				if (strpos($photo_file_name, 'pdf') == true){
				   $mime = 'image/pdf';
				}	  
					  
					 
				$exportFullPathFileName = $exportdir.$photo_file_name;
				$exportFullPathTNFileName = $exportdirtn.$photo_file_name;
					  
				$publicFullPathFileName = $MobileURLFullSize.$photo_file_name;		  
				$publicFullPathTNFileName = $MobileURLTnSize.$photo_file_name;
				
				$publicFullPathFileName = $MobileURLFullSize.$photo_file_name;		  
				$publicFullPathTNFileName = $MobileURLTnSize.$photo_file_name;	
			  					   
					 
				if (file_exists($exportFullPathFileName)){
						  
					$counter++;
					$newname  = str_replace(".jpg","_".$counter.".jpg",$photo_file_name);
							
					$exportFullPathFileName = $exportdir.$newname;
					$exportFullPathTNFileName = $exportdirtn.$newname;
				  
					$publicFullPathFileName = $MobileURLFullSize.$newname;	  
					$publicFullPathTNFileName = $MobileURLTnSize.$newname;
					
					$publicFullPathFileName = $MobileURLFullSize.$newname;		  
				    $publicFullPathTNFileName = $MobileURLTnSize.$newname;	
						  
						  
				}
				
				If ($file = fopen($exportFullPathFileName,"w")) {
					
					if (fwrite($file, $photo_attachment)==false) {
						echo "Error Write Files";
					}
					fclose($file);
				}		  
					  
				If ($file = fopen($exportFullPathTNFileName,"w")) {
					
					if (fwrite($file, $photo_attachment)==false) {
						echo "Error Write Files";
					}
					fclose($file);
				}	
				
				
				
				$sql="UPDATE wko_isp SET	
							photo_file_link ='".$publicFullPathFileName."'
					  WHERE site_cd = '".$site_cd."'
					  AND 	RowID = '".$rowid."' ;";	
					
							
				$Update_wko_isp_stmt = sqlsrv_query( $conn, $sql);

				if( !$Update_wko_isp_stmt ) {
					$error_message = "Error UPDATE IMAGE SELECT Table (wko_isp)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}

				while ( sqlsrv_next_result($Update_wko_isp_stmt) );
			
			
		}
		   

    }
} while (sqlsrv_next_result($stmt));


$sql = "SELECT rowid,
			mst_rowid,
			site_cd,
			wko_isp_asset_no,
			wko_isp_job_cd,
			wko_isp_job_desc,
			wko_isp_sec_no,
			wko_isp_sec_desc,
			wko_isp_stp_no,
			wko_isp_stp_desc,
			wko_isp_stp_datatype,
			wko_isp_stp_rowid,
			wko_isp_content,
			wko_isp_method,
			wko_isp_std,
			wko_isp_varchar1,
			wko_isp_varchar2,
			wko_isp_varchar3,
			wko_isp_numeric1,
			wko_isp_numeric2,
			wko_isp_numeric3,
			wko_isp_datetime1,
			wko_isp_datetime2,
			wko_isp_datetime3,
			wko_isp_checkbox1,
			wko_isp_checkbox2,
			wko_isp_checkbox3,
			wko_isp_dropdown1,
			wko_isp_dropdown2,
			wko_isp_dropdown3,
			wko_isp_stp_varchar1,
			wko_isp_stp_varchar2,
			wko_isp_stp_varchar3,			
			wko_isp_uom,
			wko_isp_min_thr,
			wko_isp_max_thr,
			wko_isp_ovr_thr,
			column1,
			wko_isp_wko_mst_wo_no,
			file_name,
			file_link,
			photo_file_name,
			photo_file_link,
			audit_user,
			audit_date,
			mbl_audit_user,
			mbl_audit_date
			FROM wko_isp A
			WHERE site_cd = '".$site_cd."' 
			AND wko_isp_asset_no = '".$wko_isp_asset_no."'
			AND wko_isp_job_cd = '".$wko_isp_job_cd."'
			AND mst_rowid = '".$mst_RowID." ' order by wko_isp_job_cd, wko_isp_sec_no,wko_isp_stp_no" ;
			
$wko_isp_stmt = sqlsrv_query( $conn, $sql);

if( !$wko_isp_stmt ) {
     $error_message = "Error selecting table (Work Order Check List)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

$json = array();

do {
     while ($row = sqlsrv_fetch_array($wko_isp_stmt, SQLSRV_FETCH_ASSOC)) {
		//$json[] = $row;
		
		
		$site_cd = $row['site_cd'];
		$rowid = $row['rowid'];
		$mst_rowid = $row['mst_rowid'];
		$wko_isp_asset_no = $row['wko_isp_asset_no'];
		$wko_isp_job_cd = $row['wko_isp_job_cd'];
		$wko_isp_job_desc = $row['wko_isp_job_desc'];
		$wko_isp_sec_no = $row['wko_isp_sec_no'];
		$wko_isp_sec_desc = $row['wko_isp_sec_desc'];
		$wko_isp_stp_no = $row['wko_isp_stp_no'];
		$wko_isp_stp_desc = $row['wko_isp_stp_desc'];
		$wko_isp_stp_datatype = $row['wko_isp_stp_datatype'];
		$wko_isp_stp_rowid = $row['wko_isp_stp_rowid'];
		$wko_isp_content = $row['wko_isp_content'];
		$wko_isp_method = $row['wko_isp_method'];
		$wko_isp_std = $row['wko_isp_std'];
		$wko_isp_varchar1 = $row['wko_isp_varchar1'];
		$wko_isp_varchar2 = $row['wko_isp_varchar2'];
		$wko_isp_varchar3 = $row['wko_isp_varchar3'];
		$wko_isp_numeric1 = $row['wko_isp_numeric1'];
		$wko_isp_numeric2 = $row['wko_isp_numeric2'];
		$wko_isp_numeric3 = $row['wko_isp_numeric3'];
		$wko_isp_datetime1 = $row['wko_isp_datetime1'];
		$wko_isp_datetime2 = $row['wko_isp_datetime2'];
		$wko_isp_datetime3 = $row['wko_isp_datetime3'];
		$wko_isp_checkbox1 = $row['wko_isp_checkbox1'];
		$wko_isp_checkbox2 = $row['wko_isp_checkbox2'];
		$wko_isp_checkbox3 = $row['wko_isp_checkbox3'];
		$wko_isp_dropdown1 = $row['wko_isp_dropdown1'];
		$wko_isp_dropdown2 = $row['wko_isp_dropdown2'];
		$wko_isp_dropdown3 = $row['wko_isp_dropdown3'];
		
		$wko_isp_stp_varchar1 = $row['wko_isp_stp_varchar1'];
		$wko_isp_stp_varchar2 = $row['wko_isp_stp_varchar2'];
		$wko_isp_stp_varchar3 = $row['wko_isp_stp_varchar3'];
		
		$wko_isp_uom = $row['wko_isp_uom'];
		$wko_isp_min_thr = $row['wko_isp_min_thr'];
		$wko_isp_max_thr = $row['wko_isp_max_thr'];
		$wko_isp_ovr_thr = $row['wko_isp_ovr_thr'];
		$wko_isp_wko_mst_wo_no = $row['wko_isp_wko_mst_wo_no'];
		$column1 = $row['column1'];
		
		$file_name = $row['file_name'];
		$file_link = $row['file_link'];
		$photo_file_name = $row['photo_file_name'];
		$photo_file_link = $row['photo_file_link'];
		
		$audit_user = $row['audit_user'];
		$audit_date = $row['audit_date'];
		$mbl_audit_user = $row['mbl_audit_user'];
		$mbl_audit_date = $row['mbl_audit_date'];
		
		
		$sql = "SELECT stp_zom_data FROM stp_zom WHERE site_cd = '".$site_cd."' AND mst_rowid = '".$wko_isp_stp_rowid." ' order by stp_zom.RowID asc";
			
			$stp_zom = sqlsrv_query( $conn, $sql);

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

			$json_list = array();

			do {
				 while ($row = sqlsrv_fetch_array($stp_zom, SQLSRV_FETCH_ASSOC)) {
					//$json_list[] = $row;
					
					$stp_zom_data = $row['stp_zom_data'];
					
					if($wko_isp_dropdown1 === $stp_zom_data){
						
						$json_zoom['stp_zom_data'] = $stp_zom_data;
						$json_zoom['wko_isp_wko_mst_wo_no'] = $wko_isp_wko_mst_wo_no;
						$json_zoom['column1'] = '1';
						$json_zoom['rowid'] = $rowid;
						
						array_push($json_list, $json_zoom);
						
						
					}else{
						
						$json_zoom['stp_zom_data'] = $stp_zom_data;
						$json_zoom['wko_isp_wko_mst_wo_no'] = $wko_isp_wko_mst_wo_no;
						$json_zoom['column1'] = '0';
						$json_zoom['rowid'] = $rowid;
						
						array_push($json_list, $json_zoom);
						
					}
					
					
				 }
			} while ( sqlsrv_next_result($stp_zom) );
			
			
			
			
			$json_img_link['site_cd'] = $site_cd;
			$json_img_link['rowid'] = $rowid;
			$json_img_link['mst_rowid'] = $mst_rowid;
			$json_img_link['wko_isp_asset_no'] = $wko_isp_asset_no;
			$json_img_link['wko_isp_job_cd'] = $wko_isp_job_cd;
			$json_img_link['wko_isp_job_desc'] = $wko_isp_job_desc;
			$json_img_link['wko_isp_sec_no'] = $wko_isp_sec_no;
			$json_img_link['wko_isp_sec_desc'] = $wko_isp_sec_desc;
			$json_img_link['wko_isp_stp_no'] = $wko_isp_stp_no;
			$json_img_link['wko_isp_stp_desc'] = $wko_isp_stp_desc;
			$json_img_link['wko_isp_stp_datatype'] = $wko_isp_stp_datatype;
			$json_img_link['wko_isp_stp_rowid'] = $wko_isp_stp_rowid;
			$json_img_link['wko_isp_content'] = $wko_isp_content;
			$json_img_link['wko_isp_method'] = $wko_isp_method;
			$json_img_link['wko_isp_std'] = $wko_isp_std;
			$json_img_link['wko_isp_varchar1'] = $wko_isp_varchar1;
			$json_img_link['wko_isp_varchar2'] = $wko_isp_varchar2;
			$json_img_link['wko_isp_varchar3'] = $wko_isp_varchar3;
			$json_img_link['wko_isp_numeric1'] = $wko_isp_numeric1;
			$json_img_link['wko_isp_numeric2'] = $wko_isp_numeric2;
			$json_img_link['wko_isp_numeric3'] = $wko_isp_numeric3;
			$json_img_link['wko_isp_datetime1'] = $wko_isp_datetime1;
			$json_img_link['wko_isp_datetime2'] = $wko_isp_datetime2;
			$json_img_link['wko_isp_datetime3'] = $wko_isp_datetime3;
			$json_img_link['wko_isp_checkbox1'] = $wko_isp_checkbox1;
			$json_img_link['wko_isp_checkbox2'] = $wko_isp_checkbox2;
			$json_img_link['wko_isp_checkbox3'] = $wko_isp_checkbox3;
			$json_img_link['wko_isp_dropdown1'] = $wko_isp_dropdown1;
			$json_img_link['wko_isp_dropdown2'] = $wko_isp_dropdown2;
			$json_img_link['wko_isp_dropdown3'] = $wko_isp_dropdown3;
			$json_img_link['wko_isp_stp_varchar1'] = $wko_isp_stp_varchar1;
			$json_img_link['wko_isp_stp_varchar2'] = $wko_isp_stp_varchar2;
			$json_img_link['wko_isp_stp_varchar3'] = $wko_isp_stp_varchar3;
			$json_img_link['wko_isp_uom'] = $wko_isp_uom;
			$json_img_link['wko_isp_min_thr'] = $wko_isp_min_thr;
			$json_img_link['wko_isp_max_thr'] = $wko_isp_max_thr;
			$json_img_link['wko_isp_ovr_thr'] = $wko_isp_ovr_thr;
			$json_img_link['wko_isp_wko_mst_wo_no'] = $wko_isp_wko_mst_wo_no;
			$json_img_link['column1'] = $column1;
			$json_img_link['file_name'] = $file_name;
			$json_img_link['file_link'] = $file_link;
			$json_img_link['photo_file_name'] = $photo_file_name;
			$json_img_link['photo_file_link'] = $photo_file_link;
			$json_img_link['audit_user'] = $audit_user;
			$json_img_link['audit_date'] = $audit_date;
			$json_img_link['mbl_audit_user'] = $mbl_audit_user;
			$json_img_link['mbl_audit_date'] = $mbl_audit_date;
			$json_img_link['Zoom_list'] = $json_list;
			
			array_push($json_return, $json_img_link);
					
		
		
     }
} while ( sqlsrv_next_result($wko_isp_stmt) );

if ($valid) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json_return);	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Select 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();
}


?>