<?php

include '../system/sys.php';
include '../system/funcs.php';

if (!isset ($us)) {
        header ('location: /');
        exit;
}

H ('Почта', 'Почта');

if (!isset ($_GET['id'])) {
        
}
else {
        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`='" . intval ($_GET['id']) . "')");
        if ($query -> rowCount () == 0) {
                echo '<div class="list1">Ошибка</div>';
        }        
        else {
                $u = $query -> fetch ();
                    if ($u['level']>=$us['level']) {
                        echo '<div class="list1">Ошибка</div>';
                }
                elseif ($u['id']==$us['id']) {
                        echo '<div class="list1">Ошибка</div>';
                }
                else {
                        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `msg` WHERE (`kem`='" . $u['id'] . "') OR (`us`='" . $u['id'] . "')");
                        $items_per_page = 10;
                        $pages = ceil ( $items / $items_per_page );
                        $page = (int) $_GET['page'];

                        if ($page < 1) {
                                $page = 1;
                        }
                        if ($page > $pages) {
                            $page = $pages;
                        }
                        $start = $page * $items_per_page - $items_per_page;

                        if ($items==0) {
                            echo '<div class="list1">Сообщений нет..</div>';
                        }
                        else {
                                $query = DB :: $dbs -> query ("SELECT * FROM `msg` WHERE (`kem`='" . $u['id'] . "') OR (`us`='" . $u['id'] . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
                                while ($msg = $query -> fetch ()) {
                                        echo '<div class="list1">' . uNick ($msg['kem']) . ' в переписке с ' . uNick ($msg['us']) . ' [' . datef ($msg['time']) . ']<br/>';
                                        echo bb ($msg['text']);
                                        echo '</div>';
                                }
                                if ($pages > 1) {
                                        echo '<div class="list1">' . pages ('/adm/mail' . $u['id']). '</div>';
                                }
                        }
                }
        }
}



F ();

?>