<?php

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

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

H ('Оповещения', 'Оповещения');

$items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `action` WHERE (`us`='" . $us['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 'Оповещений нет..';
}
else {

        if (isset ($_GET['reset'])) {
                $query = DB :: $dbs -> query ("SELECT * FROM `action` WHERE (`us`='" . $us['id'] . "')");
                while ($action = $query -> fetch()) {    
                        DB :: $dbs -> query ("DELETE FROM `action` WHERE (`id`='" . $action['id'] . "')");
                }
                header ('location: /action.php');
                exit;
        }

        echo '<div class="list1"><a href="/action.php?reset">Очистка</a></div>';


        $query = DB :: $dbs -> query ("SELECT * FROM `action` WHERE (`us`='" . $us['id'] . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
        while ($action = $query -> fetch()) {
        
                echo '<div class="lst">';
                echo bb ($action['value']) . ' (' . datef ($action['t']) . ')';
                if ($action['see'] == 1) {
                        echo ' <b style="color:red;">[new]</b>';
                }
                echo '</div>';

                
                // 
                if ($action['see']==1) {
                        DB :: $dbs -> query ("UPDATE `action` SET `see`='0' WHERE (`id`='" . $action['id'] . "')");
                }
        
        }
        
        echo '<div class="lst">' . pages ('/action') . '</div>';
   
}


F ();
