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

	#$page = 0;
    $userId = (!empty($_POST['usid']) ? $Filter->clearInt($_POST['usid']) : $Filter->clearInt($_GET['userId']));

	if (empty($userId)) 
	{
		header("Location: " . HOME . "/modules/mail/index.php");
	} 
	else 
	{
		$type = (!empty($_GET['type']) ? $Filter->clearInt($_GET['type']) : NULL);

		/* 
		 * Show message list & navigation;
		 */
		if ($type == 1) 
		{
			head('История переписки', 'История переписки');

			$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".MAIL." WHERE `sender` = ? and `given` = ? or `sender` = ? and `given` = ?", [$userId, $user['id'], $user['id'], $userId]);
            $n = new Navigator($all,10,'userId='. $userId . '&type=1');
            $sql = DB::$dbs->query("SELECT * FROM ".MAIL." WHERE `sender` = ? and `given` = ? or `sender` = ? and `given` = ? ORDER BY `time` DESC LIMIT {$n->start()}, 10", [$userId, $user['id'], $user['id'], $userId]);

			while ($post = $sql -> fetch()) 
			{
				# Read; 
        if ($post['given'] == $user['id'] && $post['read'] == 1) 
        {
            DB::$dbs->query("UPDATE ". MAIL ." SET `read` = ? WHERE `id` = ? ", [0, $post['id']]);
            header("Location: " . $_SERVER['REQUEST_URI']);
            exit();
        }
				$iduser = userLink($Filter->clearInt($post['sender']));
    		    $time   = dataTime($Filter->clearInt($post['time']));
    		    $text   = outputText($post['text'], $post['sender']);

				echo "".$iduser."<font color='#909090'>[".$time."]</font><br/> 
                    ".$text."<hr />";
			} 
			echo $n->navi();
			echo '<a href="' . HOME . '/modules/mail/index.php?userId=' . $userId . '" class="link-touch">К переписке</a>';
		} 
		else 
		{
			$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".MAIL." WHERE `sender` = ? and `given` = ? or `sender` = ? and `given` = ?", [$userId, $user['id'], $user['id'], $userId]);
            $n = new Navigator($all,10,'userId='. $userId . '');
            $sql = DB::$dbs->query("SELECT * FROM ".MAIL." WHERE `sender` = ? and `given` = ? or `sender` = ? and `given` = ? ORDER BY `time` DESC LIMIT {$n->start()}, 10", [$userId, $user['id'], $user['id'], $userId]);

			while ($post = $sql -> fetch()) 
			{
				# Read; 
        if ($post['given'] == $user['id'] && $post['read'] == 1) 
        {
            DB::$dbs->query("UPDATE ". MAIL ." SET `read` = ? WHERE `id` = ? ", [0, $post['id']]);
            header("Location: " . $_SERVER['REQUEST_URI']);
            exit();
        }
				$iduser = userLink($Filter->clearInt($post['sender']));
    		    $time   = dataTime($Filter->clearInt($post['time']));
    		    $text   = outputText($post['text'], $post['sender']);

				echo $iduser .'<font color="#909090">['. $time .']</font><br/> 
                    '. $text .'<hr />';
			}
			
			if ($all > 10) 
			{
				echo '<a href="' . HOME . '/modules/mail/message.php?userId=' . $userId . '&amp;type=1" class="link-touch">Показать полностью</a>';
			}
		}
	}
?>