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

//$uploaddir = "C:\\wamp64\\www\\CMMS_Standard\\temp\\";
$uploaddir;
$error_message;
$valid = true;
$json_return = array();
$json_return2 = array();
$json_return = [];



$site_cd = $_REQUEST["site_cd"];
$rowid 	 = $_REQUEST["rowid"];
$wkr_mst_wr_no 	 = $_REQUEST["wkr_mst_wr_no"];
$LOGINID 	 = $_REQUEST["LOGINID"];



$sql = "	INSERT wko_ref 
						(	site_cd,	mst_RowID,		file_name,		type, 
							status,		attachment,		column1,		audit_user,	 	audit_date ) 
							
				Select '".$site_cd."', '".$rowid."', 
						wkr_ref.file_name, wkr_ref.type,
						wkr_ref.status, wkr_ref.attachment,						
						wkr_ref.column1,'".$LOGINID."', GETDATE()
						from wkr_mst, wkr_ref
						where wkr_mst.RowID = wkr_ref.mst_RowID
						and wkr_mst.wkr_mst_wr_no = '".$wkr_mst_wr_no."'
						AND wkr_mst.site_cd = '".$site_cd."'";

						
						$stmt = sqlsrv_query( $conn, $sql);	
						if( !$stmt ) {
						$error_message = "Error (INSERT Table wko_ref)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
							
						}
						sqlsrv_free_stmt($stmt);	


if ($valid) {
	
	sqlsrv_close( $conn);	
	
	returnData();	
}


function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Attachment Insert Successfully');
	
	echo json_encode($returnData);
}

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


?>