<?php

define('_CONSTANT_', 1);

require_once '../../../core/start.php';

checkAuth();

head('Журнал', 'Журнал');

# Delete;
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) 
{
    $delete = $Filter->clearInt($_GET['delete']);
    
    $number = DB::$dbs->queryFetch("SELECT * FROM " . JOURNAL . " WHERE `id` = ? and `user_id` = ?", [$delete, $user['id']]);
    
    if (empty($number)) 
    {
        errorNoExit('Данного сообщения не существует');
    } 
    else 
    {
        DB::$dbs->query("DELETE FROM " . JOURNAL . " WHERE `id` = ? and `user_id` = ?", [$delete, $user['id']]);
        success('Сообщение удалено.', '/modules/user/journal/index.php', 'В журнал');
    }
} 

$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . JOURNAL . " WHERE `user_id` = ? ", [$user['id']]);

# Clean;
if (isset($_GET['clean'])) 
{
    $_GET['clean'] = $Filter->clearString($_GET['clean']);
     if (empty($all)) 
     {
         error('Сообщений не найдено');
     } 
     else 
     {
         DB::$dbs->query("DELETE FROM " . JOURNAL . " WHERE `user_id` = ?", [$user['id']]);
         success('Журнал очищен.');
     }
}

# Message list;
if (empty($all)) 
{
    ?> 

    <div class="wrap">
        Записей нет...
    </div>

    <?php
} 
else 
{
    $n = new Navigator($all, 10, '');
    $sql = DB::$dbs->query("SELECT * FROM " . JOURNAL . " WHERE `user_id` = ? ORDER BY `id` DESC LIMIT {$n->start()}, 10", [$user['id']]);

    while ($post = $sql -> fetch()) 
    {
        $time = dataTime($post['time']);
        $text = $post['text']; # !

        echo '<div class="wrap"> [' . $time . ']<a href="' . HOME . '/modules/user/journal/index.php?delete=' . $post['id'] . '">[x]</a><br/>' . $text . '</div>';

        # Edit status;
        if ($post['status'] == 1) 
        {
            DB::$dbs->query("UPDATE " . JOURNAL . " SET `status` = ? WHERE `id` = ? ", [0, $post['id']]);
        }
    } 
    echo $n->navi();
    echo '<a href="'. HOME .'/modules/user/journal/index.php?clean" class="link-touch">Очистить журнал</a>';
} 

require_once '../../../core/foot.php';