<?php

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

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

if (!isset ($_GET['id'])) {
        header ('location: /');
        exit;
}

$query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`=?)", array ((int) $_GET['id']));
if ($query -> rowCount () == 0) {
        header ('location: /');
        exit;
}

$u = $query -> fetch ();

$items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `senk` WHERE (`" . (isset($_GET['komu']) ? 'us' : 'komu') . "`='" . $u['id'] . "')");

H ('Поблагодарили ' . $u['nick'], uNick ($u['id']) . ' ' . (isset ($_GET['komu']) ? 'поблагодарил' : 'поблагодарили') . ' ' . $items . ' раз');

$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;

echo '<div class="list1"><a href="/senk' . $u['id'] . '">Поблагодарили</a> | <a href="/senk' . $u['id'] . '?komu">Поблагодарил</a></div>';

if ($u['id'] != $us['id']) {

    $senk = DB :: $dbs -> queryFetch ("SELECT * FROM `senk` WHERE (`us`='" . $us['id'] . "') AND (`komu`='" . $u['id'] . "') ORDER BY `id` DESC");
    if ((time () - $senk['time']) > 86400) {
   
        if (isset($_GET['add'])) { 
            $_POST['text'] = htmlspecialchars (addslashes (trim ($_POST['text'])));
            if (empty ($_POST['text'])) {
                    echo '<div class="list1">Ошибка!</div>';
            }
            else {
                
                    DB :: $dbs -> query ("INSERT INTO `senk` (`us`, `komu`,`why`,`time`) VALUES ('" . $us['id'] . "', '" . $u['id'] . "', '" . $_POST['text'] . "','" . time ()  . "')");
                    DB :: $dbs -> query ("INSERT INTO `action` (`value`,`t`,`us`,`see`) VALUES ('us{" . $us['id'] . "} [url=/senk" . $u['id'] . "]поблагодарил[/url] вас!', '" . time () . "', '" . $u['id'] . "', '1')");
                    
                    header ('location: /senk' . $u['id']);

            }
       
        }

        echo '<div class="list1">';
        echo '<form action="/senk.php?id=' . $u['id'] . '&add" method="post">';
        echo 'За что:<br><textarea name="text"></textarea><br/>';
        echo '<input type="submit" value="Поблагодарить"/>';
        echo '</form>';
        echo '</div>';
    
    }

}

if ($items != 0) {

        $query = DB :: $dbs -> query ("SELECT * FROM `senk` WHERE (`" . (isset($_GET['komu']) ? 'us' : 'komu') . "`='" . $u['id'] . "') ORDER BY `id` DESC limit $start, $items_per_page");
        while ($senk = $query -> fetch ()) {
            
            $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $senk['us'] . "')");
            
            echo '<div class="lst">';
            echo (isset ($_GET['komu']) ? 'Кого: ' . uNick ($senk['komu']) : 'Кто: ' . uNick ($senk['us'])) . ' (' . datef ($senk['time']) . ')<br/>';
            echo 'За что: <b>' . bb ($senk['why']) . '</b>';
            
            if ($senk['us']==$us['id'] && $us['level']>3 || $u['level']<$us['level']) {
                    echo '[<a href="/sek.php?id=' . $u['id'] . '&del=' . $senk['id'] . '">x</a>]';
            }
            
            echo '</div>';
        
        }
        
        echo '<div class="lst">' . pages ('/senk' . $u['id']) . '</div>';

}
else {
        echo '<div class="list1">Нет благодарностей!</div>';
}

echo '<div class="list1"><a href="/us' . $u['id'] . '">В анкету ' . $u['nick']  .'</a></div>';

F ();

?>