<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $LANG;
global $configverssion_id;
global $configapp_version;
global $server;
global $hooks;
global $URL;

//add navigation
$hooks->add_action('CustomJavaScripts','inboxnotifier_8353362o862');
function inboxnotifier_8353362o862(){
	global $LANG;
	echo "<script>";
	echo "function playSoundsAlerts() {
		var audioElement = document.createElement('audio');
		audioElement.setAttribute('src', 'files/alert.mp3');	
		audioElement.play();
	}";
	echo "function checkmessagelatieye() {
		var formData = {
			'check_if_new_inb' : 'true'
		};
		$.ajax({
			type        : 'POST', 
			url         : 'index.php', 
			data        : formData, 
		}).done(function(data) {
			var already = localStorage.getItem('last_unread_inbox893693');
			localStorage.setItem('last_unread_inbox893693',data);
			if(data != 0 && already !== data) { 
			";
				if(getSetting('audioAlerts',$userID)!=='0') {
				echo "playSoundsAlerts();";
				}
			echo "
			$('.inboxes').html('"; if(userRole()==4||userRole()==3) { echo "<i class=\"material-icons\">inbox</i> ";} echo $LANG['Inbox']."'+' <span class=\"new badge red\">'+data+'</span>');
				setTimeout(function(){ Materialize.toast('You have '+data+' new messages(s) in your inbox', 10000)}, 1000);
			}
			instanse = false;
		}).fail(function (jqXHR,status,err) {
			
		});
		var loadallc = setTimeout(checkmessagelatieye,60000)
	}";	
	echo "function checkticktemessagelatieye() {
		var formData = {
			'check_if_new_ticket' : 'true'
		};
		$.ajax({
			type        : 'POST', 
			url         : 'index.php', 
			data        : formData, 
		}).done(function(data) {
			var already = localStorage.getItem('last_unread_ticket893693');
			localStorage.setItem('last_unread_ticket893693',data);
			if(data != 0 && already !== data) {
			";
				if(getSetting('audioAlerts',$userID)!=='0') {
				echo "playSoundsAlerts();";
				}
				if(userRole()<3) {
				echo "
				$('.rttichte2').html('<i class=\"material-icons\">question_answer</i> ".$LANG['Support Tickets']."'+' <span class=\"new badge red\">'+data+'</span>');
				$('.aticket').text(data);";
				} else {
				echo "$('.rttichte2').html('<i class=\"material-icons\">question_answer</i> ".$LANG['My Support Tickets']."'+' <span class=\"new badge red\">'+data+'</span>');";
				}
				echo "setTimeout(function(){ Materialize.toast('You have '+data+' new tickets(s) awaiting your response', 10000)}, 4000);
			}
			instanse = false;
		}).fail(function (jqXHR,status,err) {
			
		});
		var loadallc = setTimeout(checkticktemessagelatieye,100000)
	}";	
	echo "$(document).ready(function(){
	localStorage.setItem('last_unread_inbox893693',0);
	localStorage.setItem('last_unread_ticket893693',0);
	checkmessagelatieye();
	checkticktemessagelatieye();
	});";
	echo "</script>";
}
$hooks->add_action('HeaderEvent','inboxnotifier_8353362ohrbubdd');
function inboxnotifier_8353362ohrbubdd(){
	global $server;
	if(isset($_REQUEST['check_if_new_inb'])) {
		header('Access-Control-Allow-Origin: *');
		if(userRole()<3) {
			echo countInbox(0,'ye');
		} else {
			echo countInbox(getUser(),'ye');
		}
		exit;  return true;
	}
}
$hooks->add_action('HeaderEvent','ticketnotifier_8353362ohrbubdd');
function ticketnotifier_8353362ohrbubdd(){
	global $server;
	if(isset($_REQUEST['check_if_new_ticket'])) {
		header('Access-Control-Allow-Origin: *');
		echo eg3et3_countTicketsUnread();
		exit;  return true;
	}
}
function eg3et3_countTicketsUnread() {
	global $server;	
	$id = getUser();
	$query = "SELECT t.* FROM tickets t WHERE t.customer_id = '$id' AND t.parent_id = 0 AND t.customer_reply = 0 AND t.status != 'closed'";
	if(userRole()<3 || userRole()>4)
	$query = "SELECT t.* FROM tickets t JOIN users u ON t.customer_id = u.id WHERE t.parent_id = 0 AND t.customer_reply = 1 AND u.reseller = '0' AND (t.status = 'open' OR t.status = 'customer-reply')";
	$result = mysqli_query($server, $query) or die(mysqli_error($server));
	$num = mysqli_num_rows($result);	
	return $num;
}
?>