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

function insert_notification_log($site_cd, $assign_from, $assign_to, $doc_no, $title, $body, $message, $module, $dvc_id, $audit_user){
	global $conn;
	
	$insert_ntf = true;
	$message = substr($message,0,1000);
	
	$sql_insert_ntf_log = "	INSERT ntf_log (	site_cd,				ntf_log_from,			ntf_log_to,				ntf_log_doc_no,			ntf_log_title,			
												ntf_log_body, 			ntf_log_msg,			ntf_log_priority,		ntf_log_icon,			ntf_log_sound,			
												ntf_log_event,			ntf_log_module,			ntf_log_send, 			ntf_log_send_date,		ntf_log_rcv,		
												ntf_log_rcv_dvc_id,		ntf_log_rcv_date,		audit_user,				audit_date,				column1,				
												column2,				column3,				column4,				column5 )

							VALUES 
											(	?,						?,						?,						?,						?,						
												?,						?,						?,						?,						?,	
												?,						?,						?,						getdate(),				?,	
												?,						getdate(),				?,						getdate(),				?,	
												?,						?,						?,						?) SELECT SCOPE_IDENTITY() as rowid";

	$params_insert_ntf_log = array(				$site_cd,				$assign_from,			$assign_to,				$doc_no,				$title,
												$body,					$message,				"",						"",						"",	
												"",						$module,				"0",											"",
												$dvc_id,										$audit_user,									NUll,
												NUll,					NUll,					NUll,					NUll);
									
	$stmt_insert_ntf_log = sqlsrv_query( $conn, $sql_insert_ntf_log, $params_insert_ntf_log);

	$json = array();

	do {
		 while ($row = sqlsrv_fetch_array($stmt_insert_ntf_log, SQLSRV_FETCH_ASSOC)) {
		 $json = $row;
		 
		 $a1[]=array("row"=>$row['rowid'],"insert_ntf"=>$insert_ntf);
		 
		 //echo '<pre>'; print_r($a1); echo '</pre>';
		 }
	} while ( sqlsrv_next_result($stmt_insert_ntf_log) );



	
	if( !$stmt_insert_ntf_log ) {
	$error_message = "Error (INSERT Table ntf_log)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
	
		$insert_ntf = false;
		
	}
	
	
	sqlsrv_free_stmt($stmt_insert_ntf_log);
	
	
	foreach($a1 as $x => $val) {
			
		$row = $x = $val["row"];
		$insert_ntf = $x = $val["insert_ntf"];
		
		//echo '<pre>'; print_r($row); echo '</pre>';
		//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
		
		
	}
	
	
		//Get New Value Assignto Online Token
		$tkn_token_1 = get_online($assign_to, $site_cd);
		
		//echo '<pre>'; print_r($tkn_token_1); echo '</pre>';
	
		//Notify token user
		if (sizeof($tkn_token_1) > 0){		
			
			
			foreach($tkn_token_1 as $x => $val) {			
				$tokenlist = $x = $val["token"];
				$RowID = $x = $val["RowID"];
				$tkn_site = $x = $val["tkn_site"];
				
				//echo '<pre>'; print_r($tkn_site); echo '</pre>';
				//echo '<pre>'; print_r($RowID); echo '</pre>';			
				
				push_notification_android( $tokenlist,$title,$body,$site_cd,$tokenlist,$RowID,$row,$tkn_site,'TEST');			
				
			
			}		
			
		}
}



?>