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

$json = file_get_contents('php://input');
$data = json_decode($json, true);

//For Powerbuilder Store Procedure call xp_cmdshell
//EXEC xp_cmdshell 'curl -d "{\"title\":\"title\",\"body\":\"body123\",\"message\":\"message123\",\"sitecode\":\"SILK\",\"emp_id\":[\"RASHIDD\", \"RAIHAN\"]}" -H "Content-Type: application/json" -X POST http://192.168.0.50:8080/cmms_standard/find_online_tablet.php';
//Start
$tokens = array();
$tkn_token;


if(ISSET($data['title'])){
	$title= $data['title'];
}

if(ISSET($data['body'])){
	$body= $data['body'];
}

if(ISSET($data['message'])){
	$message= $data['message'];
}

if(ISSET($data['sitecode'])){
	$sitecode= $data['sitecode'];
}

if(ISSET($data['emp_id'])){
	
	$emp_id= $data['emp_id'];
	
	for ($x = 0; $x < sizeof($emp_id); $x++) {
		$tokens = get_online($emp_id[$x], $sitecode);
		if (!empty($tokens)){
			push_notification_android( $tokens, $message, $title, $sitecode);
		}
	}
}
//End

function get_online($assignto, $sitecode){
	global $conn;
	
	$tkn_token_1 = array();
	$tkn_token_2 = array();
	
	//Scenario 1 -- The assigned user online and document's number belonging to mobile site login.
	$sql_wko_tkn_mst = "	SELECT 	tkn_site,
									tkn_online,
									tkn_dvc_id,
									RowID,
									tkn_token 
							FROM	tkn_mst (NOLOCK) 
							WHERE  	tkn_user = ? 
							AND		tkn_online = '1'
							AND		tkn_site = ? ";

	$params_wko_tkn_mst = array($assignto, $sitecode);	
	$stmt_wko_tkn_mst = sqlsrv_query( $conn, $sql_wko_tkn_mst, $params_wko_tkn_mst);	
			
	if( !$stmt_wko_tkn_mst ) {
		$error_message = "Error Select table (tkn_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	do {
		 while ($row = sqlsrv_fetch_array($stmt_wko_tkn_mst, SQLSRV_FETCH_ASSOC)) {
		 
		 $tkn_dvc_id = $row['tkn_dvc_id'];
		 $tkn_token_1[] = $row['tkn_token'];
		 $tkn_online = $row['tkn_online'];
		 $tkn_site = $row['tkn_site']; 
		 $RowID = $row['RowID'];
		 $a1[]=array("token"=>$row['tkn_token'],"RowID"=>$row['RowID'],"tkn_site"=>$row['tkn_site']);

	   }
	} while ( sqlsrv_next_result($stmt_wko_tkn_mst));
	
	sqlsrv_free_stmt( $stmt_wko_tkn_mst);
	
	if (empty($tkn_token_1)){
		//Scenario 2 -- The assigned user online but document's number NOT belonging to mobile site login.
		$sql_wko_tkn_mst2 = "	SELECT 	tkn_site,
										tkn_online,
										tkn_dvc_id,
										tkn_token,
										RowID
								FROM 	tkn_mst (NOLOCK) 
								WHERE	tkn_user = ? 
								AND		tkn_online = '1' 
								AND		tkn_site <> ? ";

		$params_wko_tkn_mst2 = array($assignto, $sitecode);	
		$stmt_wko_tkn_mst2 = sqlsrv_query( $conn, $sql_wko_tkn_mst2, $params_wko_tkn_mst2);	
				
		if( !$stmt_wko_tkn_mst2 ) {
			$error_message = "Error Select table (tkn_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wko_tkn_mst2, SQLSRV_FETCH_ASSOC)) {
			 
			 $tkn_dvc_id = $row['tkn_dvc_id'];
			 $tkn_token_2[] = $row['tkn_token'];
			 $tkn_online = $row['tkn_online'];
			 $tkn_site = $row['tkn_site']; 
			 $RowID = $row['RowID'];
			 $a1[]=array("token"=>$row['tkn_token'],"RowID"=>$row['RowID'],"tkn_site"=>$row['tkn_site']);
			

			 }
		} while ( sqlsrv_next_result($stmt_wko_tkn_mst2));
		
		sqlsrv_free_stmt( $stmt_wko_tkn_mst2);
	}
	
	
	
	
	//echo '<pre>'; print_r(array_merge_recursive($a1)); echo '</pre>';
	
	if(empty($a1)){
		$tkn_token = [];
	}else{
		//$tkn_token = array_merge($tkn_token_1, $tkn_token_2);
	$tkn_token = array_merge($a1);
	}
	
	
	
	return $tkn_token;
	
}

?>