<?php
profile:: is_user(true);
$thisLink = "/{$thisPage}";
$title = $l = 'Уведомления';
$lBack = profile:: getLink($user);
$table = "notices";

if (isset($_GET['delnotice']))
{
//Удаляем

//Запрос
DB :: $pdo ->query("DELETE FROM `notices` WHERE `id_user` = '".$user['id']."' ");
system :: redirect("Уведомления успешно удаленны", "notice");

}

$tab = (isset($_GET['tab']) ? intval($_GET['tab']) : 1);

$qPhoto = DB :: $pdo->prepare("SELECT id, id_user, hash FROM `photos` WHERE `id` = ? LIMIT 1");
$qPhotoComm = DB :: $pdo->prepare("SELECT id, id_user, id_photo FROM `photos_comments` WHERE `id` = ? LIMIT 1");
$qArbour = DB :: $pdo->prepare("SELECT id_user, message, time FROM `arbour` WHERE `id` = ? LIMIT 1");
$photo = false;
$photoComm = false;
$arbour = false;
if (isset($_GET['read'])) {
    $id = intval($_GET['read']);
    $qr = DB:: $pdo->queryFetch("SELECT * FROM `{$table}` WHERE `id` = ? AND `id_user` = ?;", array($id, $user['id']));

    if (isset($qr['id'])) {

        $link = '/notice';

        if ($qr['type'] == 'like_photocom') {
            $qPhotoComm->execute([$qr['id_obj']]);
            $photoComm = $qPhotoComm->fetch();

            if ($photoComm !== false) {
                $qPhoto->execute([$photoComm['id_photo']]);
                $photo = $qPhoto->fetch();

                if ($photo !== false) {
                    $link = '/photos' . $photo['id_user'] . '/view' . $photo['id'];
                }
            }

        } else if ($qr['type'] == 'comment_photo' || $qr['type'] == 'add_comment_photo' || $qr['type'] == 'like_photo') {
            $qPhoto->execute([$qr['id_obj']]);
            $photo = $qPhoto->fetch();
            if ($photo !== false)
                $link = '/photos' . $photo['id_user'] . '/view' . $photo['id'];
        } else if ($qr['type'] == 'gift') {
            $link = '/gifts?id=' . $qr['id_user'];
        } else if ($qr['type'] == 'friend') {
            $link = '/friends?id=' . $qr['id_user'] . '&tab=3';
        } else if ($qr['type'] == 'add_friend') {
            $link = '/id' . $qr['id_user'];
        } else if ($qr['type'] == 'blog') {
            $link = '/blog/?note=' . $qr['id_obj'];
        } else if ($qr['type'] == 'blog_otvet') {
            $link = '/blog/?note=' . $qr['id_obj'];
        } else if ($qr['type'] == 'arbour') {
            $link = '/arbour/?note=' . $qr['id_obj'];
        } else if ($qr['type'] == 'like_arbour') {
            $link = '/arbour/?note=' . $qr['id_obj'];
        }


        DB:: $pdo->query("UPDATE `{$table}` SET `read` = '1' WHERE `id` = ? LIMIT 1;", [$qr['id']]);
        system:: redirect(NULL, $link);
    } else {
        system:: redirect("Уведомления не найдено.", $thisLink, 1);
    }
}

require(CORE . "head.php");
echo "<div class=\"app-block\">";
?>
<div class="list">
    <div class="sw-block_">
        <div class="sw-cont sw-2">
            <div class="sw-item"><a href="<?= $thisLink . "?tab=1" ?>" class="sw-c<?= ($tab != 2 ? " sw-act" : '') ?>">Все</a>
            </div>
            <div class="sw-item"><a href="<?= $thisLink . "?tab=2" ?>" class="sw-c<?= ($tab == 2 ? " sw-act" : '') ?>">Новые</a>
            </div>
        </div>
    </div>
</div>
<div class="touch"><a href="/notice?delnotice" class="touch-cont _oh">
                 Очистить уведомления
                </a></div>
<?

$total = DB:: $pdo->querySingle("SELECT COUNT(*) FROM `{$table}` WHERE `id_user` = '" . $user['id'] . "'" . ($tab == 2 ? " AND `read` = '0'" : '') . " ORDER by `time` DESC ");
if ($total == 0) {
    ?>
    <div class="list _gray _font-s _center">Уведомления не найдены!</div>
    <?
} else {
    if ($total > 10) {
        $p = (($_GET['p']) ? intval($_GET['p']) : 1);
        $pagination = new pagination(10, $total, $p);
        $total_lin = $pagination->limit;
    } else {
        $total_lin = '';
    }

    $query = DB:: $pdo->query("SELECT * FROM `{$table}` WHERE `id_user` = '" . $user['id'] . "'" . ($tab == 2 ? " AND `read` = '0'" : '') . " ORDER by `time` DESC  " . $total_lin . "");
    while ($r = $query->fetch()) {
        $d = profile:: getUser($r['id_last']);
        $text = "";
        $icon_notif = notif::init()->getTypeIcon($r['type']);
        if($r['type'] == 'like_photocom') {
            $qPhotoComm->execute([$r['id_obj']]);
            $photoComm = $qPhotoComm->fetch();
            $text = "оценил" . ($d['info_sex'] == 0 ? "а" : "") . " комментарий к " . ($photoComm !== false && $photoComm['id_user'] == $user['id'] ? 'Вашей ' : '') . "фотографии";
            if($photoComm !== false) {
                $qPhoto->execute([$photoComm['id_photo']]);
                $photo = $qPhoto->fetch();
            }
            $text = "оценил" . ($d['info_sex'] == 0 ? "а" : "") . " комментарий к " . ($photo !== false && $photo['id_user'] == $user['id'] ? 'Вашей ' : '') . "фотографии";
        } else if ($r['type'] == 'comment_photo') {
            $qPhoto->execute([$r['id_obj']]);
            $photo = $qPhoto->fetch();
            $text = " ответил" . ($d['info_sex'] == 0 ? "а" : "") . " в комментариях к " . ($photo !== false && $photo['id_user'] == $user['id'] ? 'Вашей ' : '') . "фотографии";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . " ";

        } else if ($r['type'] == 'add_comment_photo') {
            $qPhoto->execute([$r['id_obj']]);
            $photo = $qPhoto->fetch();
            $text = "оставил" . ($d['info_sex'] == 0 ? "а" : "") . " комментарий к " . ($photo !== false && $photo['id_user'] == $user['id'] ? 'Вашей ' : '') . "фотографии";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . "";

        } else if ($r['type'] == 'comment_tracker') {
            $tracker = DB::$pdo->queryFetch("SELECT `id` FROM `bag_tracker` WHERE `id` = '" . $r['id_obj'] . "' LIMIT 1;");
            if (!isset($tracker) || $tracker['id'] <= 0) {
                $text = 'запись не найдена!';
                $icon_notif = notif::init()->getTypeIcon('error_post');
            } else {
                $text = 'ответил' . ($d['info_sex'] == 0 ? 'а' : '') . ' на Ваш отчет о багах!<br />Текст ответа: ' . stripcslashes(htmlspecialchars($r['notices_text']));
            }
        } else if ($r['type'] == 'gift') {
            $text = "подарил" . ($d['info_sex'] == 0 ? "а" : "") . " Вам подарок ";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . " <br/ >
                <img  src=\"" . FILES_SERVER . "/gifts/" . $r['id_obj'] . ".jpg\">
                ";
        } else if ($r['type'] == 'friend') {
            $text = "отправил" . ($d['info_sex'] == 0 ? "а" : "") . " запрос в друзья ";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . "";
        } else if ($r['type'] == 'add_friend') {
            $text = "добавил" . ($d['info_sex'] == 0 ? "а" : "") . " Вас в друзья";
        } else if ($r['type'] == 'no_friend') {
            $text = "отклонил" . ($d['info_sex'] == 0 ? "а" : "") . " заявку в друзья";
        } else if ($r['type'] == 'blog') {
            $text = "оставил" . ($d['info_sex'] == 0 ? "а" : "") . " комментарий к вашему блогу";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . "";
        } else if ($r['type'] == 'blog_otvet') {
            $text = "ответил" . ($d['info_sex'] == 0 ? "а" : "") . " на комментарий к вашему блогу";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . "";
        } else if ($r['type'] == 'arbour') {
            $text = "ответил" . ($d['info_sex'] == 0 ? "а" : "") . " на комментарий в общем чате";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . "";
        } else if ($r['type'] == 'like_photo') {
            $qPhoto->execute([$r['id_obj']]);
            $photo = $qPhoto->fetch();
            $text = "оценил" . ($d['info_sex'] == 0 ? "а" : "") . " Ваше фото";
          //  $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . " ";
        } else if ($r['type'] == 'like_arbour') {
            $text = "оценил" . ($d['info_sex'] == 0 ? "а" : "") . " Ваш пост  в общем чате";
            $lol = "" . emoji:: Text(system:: BBcode(system:: textOut($r['notices_text']))) . " ";
        }
        ?>

        <div class="list _l-padd-n">
            <div class="_l-padd">
                <div class="_oh">
                    <div class="list-flex">
                        <div class="post-l">
                            <?
                            echo profile::userAvatar($d, 3); ?>
                            <div class="notification__avatarIcon icon <?= $icon_notif ?>"></div>
                        </div>
                        <div class="post-c">
                            <div class="_name">
                                <?
                                echo profile::goPage($d, false); ?>         <?= $text ?>           </div>
                            <div class="_time">
                                <?
                                echo system::times($r['time']); ?>                </div>
                        </div>
                    </div>
                </div>
                <?php if ($r['type'] == 'like_arbour') {
                    $qArbour->execute([$r['id_obj']]);
                    $arbour = $qArbour->fetch();
                    if ($arbour !== false) {
                        $userData = profile:: getUser($arbour['id_user']);
                        ?>
                        <div class="msg_item message_arbour">
                            <div class="mi_iwrap">
                                <a href="/id<?= $userData['id'] ?>">
                                    <img src="<?= profile:: getAvatar($userData, 0) ?>" class="mi_img">
                                </a>
                            </div>
                            <div class="mi_cont">
                                <div class="mi_head">
                                    <span class="mi_date"><?= system::times($arbour['time']) ?></span>
                                    <a class="mi_author"
                                       href="/id<?= $userData['id'] ?>"> <?= profile::getFullName($userData) ?> <?= profile::ico($userData) ?> <?= profile::vermini($userData) ?></a>
                                </div>
                                <div class="mi_body">
                                    <div class="mi_text">
                                        <?= emoji:: Text(system:: BBcode(system:: textOut($arbour['message']))) ?>
                                    </div>
                                </div>
                            </div>
                            <?= like::init()->createButton(like::ARBOUR, [
                                'id_post' => $r['id_obj'],
                                'id_user' => $user['id'],
                                'id_author' => $userData['id']
                            ]) ?>
                        </div>
                        <?
                    }
                } else { ?>
                    <div class="_center _gray">
                        <div>
                            <?= $lol ?>
                        </div>
                        <?php if (($r['type'] == 'add_comment_photo' || $r['type'] == 'comment_photo' || $r['type'] == 'like_photo' || $r['type'] == 'like_photocom') && $photo !== false) { ?>
                            <div class="_photos-item">
                                <div class="_photos-item-padd">
                                    <div class="_photos-cont">
                                        <div class="_photos-ph">
                                            <div class="_photos-ph-t"><img
                                                        src="<?= FILES_SERVER . system:: photosGetView($photo['hash'], 3) ?>"
                                                        class="_photos-prew" style="border-radius:5px">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="_font-s">
                        </div>
                    </div>
                <?php } ?>
            </div>
            <div class="tb-line"></div>
            <div class="tb-bloc">
                <div class="tb-cont tb-1">
                    <div class="tb-item"><a href="<?= "{$thisLink}?read={$r['id']}" ?>" class="tb-c">Посмотреть</a>
                    </div>

                </div>
            </div>
        </div>
        <?php
    }

    if($tab == 2 && $total > 0) {
        DB:: $pdo->query("UPDATE `{$table}` SET `read` = '1' WHERE `id_user` = ? AND `read` = '0'", [$user['id']]);
    }

    if ($total > 10) {
          echo $pagination -> printPages("{$thisLink}&");
    }
}
echo "</div>";
require(CORE . "foot.php");
?>
