<?php
require_once('config.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'];

$wko_det_resp_name = $data['wko_det_resp_name'];
$wko_det_tech_resp_date = $data['wko_det_tech_resp_date'];
$wko_det_tech_resp_id = $data['wko_det_tech_resp_id'];
$LOGINID = $data['LOGINID'];
$mst_RowID = $data['mst_RowID'];
$scanvalue = $data['scanvalue'];



$sql_wko_det = "UPDATE  wko_det  SET
						wko_det_tech_resp_date =?,		
						wko_det_tech_resp_id=?,	
						wko_det_resp_name=?,	
						mbl_audit_user=?,
						mbl_audit_date=GETDATE()
						where 	site_cd = ?
						AND		mst_RowID = ?";

$params_wko_det = array($wko_det_tech_resp_date,$wko_det_tech_resp_id,$wko_det_resp_name,$LOGINID,$site_cd,$mst_RowID);
$stmt_wko_det = sqlsrv_query( $conn, $sql_wko_det,$params_wko_det);


if( !$stmt_wko_det ) {
	$error_message = "Error insert table (UPDATE Table wko_det)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt( $stmt_wko_det);



 if($stmt_wko_det){

	 sqlsrv_commit( $conn );
	 returnData($wko_det_resp_name);

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



function returnData($wko_det_varchar10){

	if($wko_det_varchar10 === 'Not Verified'){

		$returnData = array(
		'status' => 'ERROR',
		'message' => 'Asset not verified');
		echo json_encode($returnData);

	}else{

		$returnData = array(
		'status' => 'SUCCESS',
		'message' => 'Asset verified');
		echo json_encode($returnData);

	}

}

function returnError($error_message){

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



?>