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

if ($user['count_new_notice'] > 0) {
    DB:: $pdo->query("UPDATE `users` SET `count_new_notice` = '0' WHERE `id` = '" . $user['id'] . "' LIMIT 1");
    $user['count_new_notice'] = 0;
}
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_user,hash FROM `photos` WHERE `id` = ? LIMIT 1");
$photo = 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'])) {
       if ($qr['type'] == comment_photo || $qr['type'] == add_comment_photo){
            $qPhoto->execute([$qr['id_obj']]);
            $photo = $qPhoto->fetch();
            if($photo !== false)
                $link = '/photos'.$photo['id_user'].'/view'.$qr['id_obj'];
          
        }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'];
          
        }
        }
        }
        }
        }
        }
        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'] == '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'])))."";
        }
  ?>
  
  <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,folse);?>         <?= $text ?>           </div>
                    <div class="_time">
                        <?echo system::times($r['time']);?>                </div>
                               </div>
            </div>
            </div>
                    <div class="_center _gray">
                    <div>
                         <?= $lol ?> 
                    </div>
                        <?php if(($r['type'] == 'add_comment_photo' || $r['type'] == 'comment_photo') && $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>
            </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 ($total > 10) {
          echo $pagination -> printPages("{$thisLink}&"); 
    }
}
echo "</div>";
require(CORE . "foot.php");
?>
