<?php
	define('_CONSTANT_', 1); 
	require_once '../../core/start.php';

	$type = (!empty($_GET['type']) ? $Filter->clearInt($_GET['type']) : NULL);

	if (empty($type)) 
	{
	$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . CHAT . "");
	/* 
	* * * * * * * * * *
	* IF NOT MESSAGES *
	* * * * * * * * * *
	*/
	if($all == 0) {
		errorNoExit('Сообщений нет...');
	};
	$n   = new Navigator($all,10,'');
    $sql = DB::$dbs->query("SELECT * FROM ".CHAT." ORDER BY `id` DESC LIMIT {$n->start()}, 10");


	while($post = $sql ->fetch()) {
		$iduser = userLink($Filter->clearInt($post['id_user']));
		$time   = dataTime($Filter->clearInt($post['time']));
		$text   = outputText($post['text'], $post['id_user']);

		/* 
		* * * * * * * 
		* ONLYADMIN *
		* * * * * * * 
		*/
		if($user['access'] == 2){
			$idzver = $Filter->clearInt($post['id']);
			echo "<button class='deletePost1'><i class='fa fa-times' aria-hidden='true'></i><span style='display:none'>".$idzver."</span></button>"; 
		}

		if($post['id_user'] != $user['id']){
			$idUzver   = $Filter->clearInt($post['id_user']);
			$nameUzver = $User->userAnswer($Filter->clearInt($post['id_user']), 'login');

			echo $iduser."<font color='#909090'>[".$time."]</font>
			<button class='answer'>(отв)
                <small style='display:none;'>".$nameUzver."</small>
                <span  style='display:none;'>".$idUzver."</span>
            </button>
            <br/> 
            <p>".$text."</p><hr />";
		}else{

			echo $iduser."<font color='#909090'>[".$time."]</font><br/> 
                ".$text."<hr />";
        }
	}

	if ($all > 10) 
	{
		echo '<a href="' . HOME . '/modules/chat/message.php?type=1" class="link-touch">Показать полностью</a>';
	}
	} 
	else 
	{
		head('История сообщений', 'История сообщений');
		$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . CHAT . "");
	/* 
	* * * * * * * * * *
	* IF NOT MESSAGES *
	* * * * * * * * * *
	*/
	if($all == 0) {
		errorNoExit('Сообщений нет...');
	};
	$n   = new Navigator($all,10,'type=1');
    $sql = DB::$dbs->query("SELECT * FROM ".CHAT." ORDER BY `id` DESC LIMIT {$n->start()}, 10");


	while($post = $sql ->fetch()) {
		$iduser = userLink($Filter->clearInt($post['id_user']));
		$time   = dataTime($Filter->clearInt($post['time']));
		$text   = outputText($post['text'], $post['id_user']);

		/* 
		* * * * * * * 
		* ONLYADMIN *
		* * * * * * * 
		*/

		if($post['id_user'] != $user['id']){
			$idUzver   = $Filter->clearInt($post['id_user']);
			$nameUzver = $User->userAnswer($Filter->clearInt($post['id_user']), 'login');

			echo $iduser."<font color='#909090'>[".$time."]</font>
            <br/> 
            <p>".$text."</p><hr />";
		}else{

			echo $iduser."<font color='#909090'>[".$time."]</font><br/> 
                ".$text."<hr />";
        }
	}
	echo $n->navi();
	echo '<a href="' . HOME . '/modules/chat/index.php" class="link-touch">В чат</a>';
	}
	


?>