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

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}

$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json, true);


$site_cd = $data['site_cd'];

$sync_step = $data['sync_step'];
$sync_time = $data['sync_time'];
$sync_status = $data['sync_status'];
$sync_url = $data['sync_url'];
$LOGINID = $data['LOGINID'];
$dvc_id = $data['dvc_id'];


if($sync_status =="online"){
	
}else{
	f_logging($json,"update_response.php",$sync_url,$LOGINID,$sync_step,$sync_time,$site_cd);
}



$mst_RowID = $data['mst_RowID'];
$wko_det_tech_resp_date = $data['wko_det_tech_resp_date'];
$wko_det_tech_resp_id = $data['wko_det_tech_resp_id'];
$wko_det_resp_contact = $data['wko_det_resp_contact'];
$wko_det_resp_id = $data['wko_det_resp_id'];
$wko_det_resp_date = $data['wko_det_resp_date'];
$wko_det_resp_name = $data['wko_det_resp_name'];


	$sql = "SELECT	wko_mst_org_date		
			FROM 	wko_mst (NOLOCK)
			WHERE  	wko_mst.site_cd = '".$site_cd."'
			AND		wko_mst.RowID = '".$mst_RowID."' ";
			
	$stmt = sqlsrv_query( $conn, $sql);	
			
	if( !$stmt ) {
		$error_message = "SELECT  table Error (emp_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		 
		  $wko_mst_org_date = $row['wko_mst_org_date'];
			
			 
	   }
	} while ( sqlsrv_next_result($stmt));
	sqlsrv_free_stmt( $stmt);

	// Ensure $wko_mst_org_date is a DateTime object
	$start = ($wko_mst_org_date instanceof DateTime)     ? $wko_mst_org_date     : new DateTime($wko_mst_org_date);

	// Ensure $wko_det_resp_date is a DateTime object
	$end = ($wko_det_resp_date instanceof DateTime)     ? $wko_det_resp_date     : new DateTime($wko_det_resp_date);

	// Calculate difference in minutes
	$minutes = ($end->getTimestamp() - $start->getTimestamp()) / 60;
	
$sql="UPDATE wko_det  SET						
			wko_det_tech_resp_date =?,		
			wko_det_tech_resp_id=?,		
			wko_det_resp_contact=?,			
			wko_det_resp_id=?,				
			wko_det_resp_name=?,			
			wko_det_resp_date=?,
			wko_det_opn_res =?,
			mbl_audit_user=?,
			mbl_audit_date=GETDATE()	
			WHERE site_cd = ? 
			AND mst_RowID = ? ;";	
				
	//echo $sql;
	$params = array($wko_det_tech_resp_date,$wko_det_tech_resp_id,$wko_det_resp_contact,$wko_det_resp_id,$wko_det_resp_name,$wko_det_resp_date,$minutes,$LOGINID,$site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql,$params);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (wko_det)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			$json= array();

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
				 
					$json[] = $row;		
				
				 }
			} while ( sqlsrv_next_result($stmt) );
			sqlsrv_free_stmt( $stmt);	


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

function returnData($json){
	
	 $json1 = json_encode($json);  

	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Response updated successfully ',	
	'data' => $json);
	
	echo json_encode($returnData);
}

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


 
?>