<?php  session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin']) || isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("../j_conn/url.php");
require_once("../j_conn/dbc.php");
require_once("../j_incs/functions.php");
if(isset($_POST['option_value'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
$option_value = RMC($_POST['option_value']);
if($option_value=="read"){
	$read_id = RMC("'".$id."'");
	$sql25="SELECT * from j_notifications WHERE 
	j_deleted < 1 AND j_user_id='".$id."' AND j_read NOT LIKE '%$read_id%'
	|| j_deleted < 1 AND j_user_id='*' AND j_read NOT LIKE '%$read_id%'
	order by j_id desc
	"; //  
	$result25 = runSQL($sql25);
	$total_notify = mysqli_num_rows($result25);
	while($row = mysqli_fetch_array($result25)){
		$ref = $row['j_id']; 
		//read message
		$read_id = RMC("".$row['j_read'].",'".$id."'");
		//$read = "".$row['j_read'].",".$id."";
		if(strpos($row['j_read'],"'$id'") === false){
			$sql2 = "UPDATE j_notifications SET j_read='".$read_id."' WHERE 
			j_deleted < 1 AND j_id='".$ref."'";
			$result2 = runSQL($sql2);  
		}
	}
	if($total_notify > 0){
	?>
	<div class="notification-toast top-right" id="notification-toast"></div>
	<script>
	function showToast() {
	var toastHTML = `<div class="toast fade hide" data-delay="3000">
		<div class="toast-header">
			<i class="fas fa-check-circle text-primary m-r-5"></i>
			<strong class="mr-auto">Mark as Read</strong>
			<small></small>
			<button type="button" class="ml-2 close" data-dismiss="toast" aria-label="Close">
				<span aria-hidden="true">&times;</span>
			</button>
		</div>
		<div class="toast-body">
			<?php echo number_format($total_notify); ?> of your unread notifications has been marked as read. 
		</div>
	</div>`

	$('#notification-toast').append(toastHTML)
	$('#notification-toast .toast').toast('show');
	setTimeout(function(){ 
		$('#notification-toast .toast:first-child').remove();
		//window.location.href="";  
	}, 8000);
	}
	</script>
	<script>
		showToast();
	</script>
	<?php } ?>





<?php  	
}elseif($option_value=="unread"){
	$read_id = RMC(",'".$id."'");
	$sql25="SELECT * from j_notifications WHERE 
	j_deleted < 1 AND j_user_id='".$id."' AND j_read LIKE '%$read_id%'
	|| j_deleted < 1 AND j_user_id='*' AND j_read LIKE '%$read_id%'
	order by j_id desc
	"; //  
	$result25 = runSQL($sql25);
	$total_notify = mysqli_num_rows($result25);
	while($row = mysqli_fetch_array($result25)){
		$ref = $row['j_id']; 
		//unread message
		$p_read_id = RMC($row['j_read']);
		$r_id = RMC(",'".$id."'");
		$read_id = str_replace($r_id, '', $p_read_id);
		//$read = "".$row['j_read'].",".$id."";
		if(strpos($row['j_read'],"'$id'") !== false){
			$sql2 = "UPDATE j_notifications SET j_read='".$read_id."' WHERE 
			j_deleted < 1 AND j_id='".$ref."'";
			$result2 = runSQL($sql2);  
		}
	}
	if($total_notify > 0){
	?>
	<div class="notification-toast top-right" id="notification-toast"></div>
	<script>
	function showToast() {
	var toastHTML = `<div class="toast fade hide" data-delay="3000">
		<div class="toast-header">
			<i class="fas fa-check-circle text-primary m-r-5"></i>
			<strong class="mr-auto">Mark as Read</strong>
			<small></small>
			<button type="button" class="ml-2 close" data-dismiss="toast" aria-label="Close">
				<span aria-hidden="true">&times;</span>
			</button>
		</div>
		<div class="toast-body">
			<?php echo number_format($total_notify); ?> of your read notifications has been marked as unread. 
		</div>
	</div>`

	$('#notification-toast').append(toastHTML)
	$('#notification-toast .toast').toast('show');
	setTimeout(function(){ 
		$('#notification-toast .toast:first-child').remove();
		//window.location.href="";  
	}, 8000);
	}
	</script>
	<script>
		showToast();
	</script>
	<?php } ?>
	
	
<?php	
}
?>