<?php 
	$pageTitle 	= 'Быстрый мини-чат';
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title><?php if ( $page[1] != '') { echo $pageTitle . ' - ' . $CRMname; } else { echo $CRMname . ' - CRM система для малого бизнеса'; } ?></title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta name="robots" content="noindex, nofollow" />
		<?php if ( $nCRMdateTo < $nCRMdateFrom ){if ( $page[1] != '' and $page[1] != 'admin' and $page[1] != 'crm-info' ){echo '<meta http-equiv="refresh" content="0;URL=/" />'; }}?>
		<link rel="yandex-tableau-widget" href="manifest.json" />
		<link rel="stylesheet" href="assets/css/animate.css" />
		<link rel="stylesheet" href="/<?php echo $folder; ?>/assets/css/bootstrap.css" />
		<link rel="stylesheet" href="/<?php echo $folder; ?>/assets/css/bootstrap-theme.css" />
		<?php if(get_utheme() == "dark"){ ?>
		<link rel="stylesheet" href="/<? echo $folder; ?>/assets/css/style-dark.css" />
		<?php } else { ?>
		<link rel="stylesheet" href="/<? echo $folder; ?>/assets/css/style.css" />
		<?php } ?>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" />
		<link rel="stylesheet" href="assets/css/c3.css" />
		<link rel="icon" href="favicon.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
		<meta name="yandex-tableau-widget" content="logo=browser_logo.png, color=#5079b4, feed=<?php echo ROOT; ?>manifest.json" />
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
	</head>
	<body style="background:transparent;margin:0px;padding:0px;" onLoad="window.scrollTo(0,999999999999999)">
	<div class="loader_qchat"></div>
	<?php if ($auth->isAuth() and $sessionIPCheck->length < 1) { ?>
	<div class="qchat">
		<?
		/* Удаление сообщения  */
		if( isset( $_POST['MessDelete'] ) ) {
			$doc = new DOMDocument; 
			$doc->load($baseQ);
			$xpath = new DOMXPath($doc); 
			
			$id = $_POST["MessDelId"];

			$messages = $xpath->query('/messages/message[@id="'.$id.'"]');
			$el = $messages->item(0);
			$el->parentNode->removeChild($el);
			$doc->save($baseQ); 
		}
		
		$dom = new DOMDocument;
		$dom->load($baseQ);
		$xpath = new DOMXPath($dom);
		$messages = $xpath->query('/messages/message');
		
		foreach ($messages as $message) {
			$time			= date("H:i:s", time()-60*5);
			$date			= date("Y-m-d");
			$qchatTime 		= $message->getElementsByTagName("time")->item(0)->nodeValue;
			$qchatDate 		= $message->getElementsByTagName("date")->item(0)->nodeValue;
			if( $qchatDate == $date and $qchatTime >= $time){
				for ($i = 0; $i < $messages->length; $i++) {
					$qchatMessages 	= $xpath->query('/messages/message[date="'.$date.'"]')->item($i)->getElementsByTagName("time")->item(0)->nodeValue >= $time;
				}
			}
			$countM += count($qchatMessages);
		}
		
		$xmlqchat 	= simplexml_load_file($baseQ);
		$countQchat = count($xmlqchat->xpath('/messages/message'));
			
		if($countQchat < 1) {
			echo '
				<meta http-equiv="Refresh" content="" />
				<div class="centered">- в чате нет сообщений -</div>
			';
		} else {
			if ($countM > 0 ){echo '<meta http-equiv="Refresh" content="10" />';} else {echo '<meta http-equiv="Refresh" content="" />';}
			foreach ($messages as $message) {
				$qchatId 				= $message->getAttribute('id');
				$qchatUserLogin = $message->getAttribute('user');
				$qchatMess 		= $message->getElementsByTagName("text")->item(0)->nodeValue;
				$qchatTime 			= $message->getElementsByTagName("time")->item(0)->nodeValue;
				$qchatDate 			= $message->getElementsByTagName("date")->item(0)->nodeValue;
				
				if (file_exists($baseU) and file_exists($fileU)) {
					$doc = new DOMDocument;
					$doc->load($baseU);
					$xpath = new DOMXPath($doc);
					$users = $xpath->query('/users/user[@login="'. $qchatUserLogin .'"]');
					for ($i = 0; $i < $users->length; $i++) {
						$qchatUserName 		= $xpath->query('/users/user[@login="'. $qchatUserLogin .'"]')->item($i)->getElementsByTagName("username")->item(0)->nodeValue;
						$qchatUserSurName 	= $xpath->query('/users/user[@login="'. $qchatUserLogin .'"]')->item($i)->getElementsByTagName("usersurname")->item(0)->nodeValue;
					}
				}
			?>
			<div id="<?php echo $qchatId; ?>" class="message">
				<div class="title"><b title="Логин: <?php echo $qchatUserLogin; ?>"><?php echo $qchatUserName; ?> <?php echo $qchatUserSurName; ?></b><span title="<?php echo $qchatDate; ?>"><?php echo $qchatTime; ?></span></div>
				<div class="text"><?php echo $qchatMess; ?></div>
				<?
				if ($GroupCheck == 'admin') { 
					echo '<form method="POST">
						<button class="btn btn-sm btn-link" type="submit" name="MessDelete" onclick="return confirm(\'Удалить сообщение?\')">Удалить</button>
						<input type="text" class="hidden" name="MessDelId" value="' . $qchatId . '" />
					</form>';
					}
				?>
			</div>
			<?} } ?>
	</div>
	<?php include_once(ROOT . 'templates/footer-scripts.php'); ?>
	<?php } ?>
	</body>
</html>