<?php 
	$id = (int)abs($_GET['id']); #ID 
	$ank = profile :: getUser($id); #юзер
	


	$link = "/".$thisPage; #Ссылка страницы
	
	$table = intval($_GET['tab']); #Переключатель
	
	if (isset($user['id']) && $user['id'] == $ank['id']) {
		if ($user and isset ($_GET['add']) and !empty ($_GET['add'])) { require("inc/add_friend.php"); exit; }   #Добавления в друзья
		if ($user and isset ($_GET['new']) and !empty ($_GET['new']) and isset ($_GET['c'])) { require('inc/new.php'); exit; } #Новая заявка
		if ($user and isset ($_GET['cancel']) and !empty ($_GET['cancel']) and isset ($_GET['c'])) { require('inc/cancel.php'); exit; } #Отмена заявки
		if($table == 4) {$table = 1;}
	}

	$title = $l = 'Друзья'; #Тайтл
	$lBack = profile :: getLink($ank); #Ссылка назад
 // Поис  логинов //
	$ss = "";
	if(isset($_POST['s_cfms'])) {
		$qs = system :: text($_POST['search']);
		$ss = "AND LOWER(`users`.`login`) LIKE '%".system :: check($qs)."%'";
	}
 // Конец поиска //
	

	require (CORE."head.php"); #Шапка

echo "<div class=\"app-block\">";


	?>
	<div class="sw-block"><div class="sw-cont sw-3">
		<div class="sw-item"><a href="<?=$link?>?id=<?=$ank['id']?>&amp;tab=1" class="sw-c<?=($table != 2 && $table != 3 && $table != 4 ? " sw-act" : '' )?>">Все</a></div>
		<div class="sw-item"><a href="<?=$link?>?id=<?=$ank['id']?>&amp;tab=2" class="sw-c<?=($table == 2 ? " sw-act" : '' )?>">Онлайн</a></div>
		<?php if(@$user['id'] == $ank['id']) { ?>
		<div class="sw-item"><a href="<?=$link?>?id=<?=$ank['id']?>&amp;tab=3" class="sw-c<?=($table == 3 ? " sw-act" : '' )?>">Заявки<?=( $user['count_fr_new'] > 0 ? " <span class=\"_bold\">{$user['count_fr_new']}</span>" : '' )?></a></div>
		<?php } else { ?>
		<div class="sw-item"><a href="<?=$link?>?id=<?=$ank['id']?>&amp;tab=4" class="sw-c<?=($table == 4 ? " sw-act" : '' )?>">Общие</a></div>
		<?php	} ?> 
	</div></div>
<?php if($table != 3 && $table != 4) { ?>
		<div class="app-search">
			<form method="post" action="">
			<table class="_row-table">
				<tbody>
					<tr>
						<td class="_row-table-main">
							<input type="text" class="app-search-input" value="<?=system :: check($qs)?>" name="search" placeholder="Введите ник для поиска..">
						</td>
						<td class="_row-table-last">
							<input type="submit" name="s_cfms" class="main-btn" value="Найти">
						</td>
					</tr>
				</tbody>
			</table>
			</form>
		</div>
	<?php
}



	if($table == 3 && isset($user['id']) && $user['id'] == $ank['id']){
		$query ="SELECT * FROM `query_friends` WHERE `id_user` = '".$id."' ORDER by `id` DESC "; 
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `query_friends` WHERE `id_user` = '".$id."'");
	} else if($table == 2) {
		$query = "SELECT * FROM `friends` INNER JOIN `users` ON `friends`.`id_friend`=`users`.`id` WHERE `users`.`last_time` > '".(time()-$config['TIME_ONLINE'])."' AND `friends`.`id_user` = '".$id."' {$ss} ORDER BY `friends`.`id` DESC "; 
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `friends` INNER JOIN `users` ON `friends`.`id_friend`=`users`.`id` WHERE `users`.`last_time` > '".(time()-$config['TIME_ONLINE'])."' AND `friends`.`id_user` = '".$id."' {$ss}");
	}else if($table == 4) {
		$query = "SELECT * FROM `friends` WHERE `id_friend` IN (SELECT `id_user` FROM `friends` WHERE `id_friend` = '{$user['id']}') AND `id_user` = '{$ank['id']}';";
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `friends` WHERE `id_user` IN (SELECT `id_user` FROM `friends` WHERE `id_friend` = ?) AND `id_friend` = ?;", [$user['id'], $data['id']]);
	} else {
		$query = "SELECT * FROM `friends` INNER JOIN `users` ON `friends`.`id_friend` = `users`.`id` WHERE `friends`.`id_user` = '".$id."' {$ss} ORDER BY `friends`.`id` DESC "; 
		//$total = $ank['count_friends'];
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `friends` INNER JOIN `users` ON `friends`.`id_friend`=`users`.`id` WHERE `friends`.`id_user` = '".$id."' {$ss}");
      //  var_dump($query);
	}

	if($total > 10) {
			$p = (($_GET['p']) ? intval($_GET['p']) : 1);
			$pagination = new pagination(10, $total, $p);
			$query .= $pagination -> limit;
	}
		
	$q = DB :: $pdo -> query("{$query}");

	$i = 0;

	while($r = $q -> fetch()) {
		++$i;
		if($table == 3){$r['friend_id'] = $r['ot'];}
		
		$data = profile :: getUser($r['id_friend']);

		?>
		<div class="list">
 <div>
                            <a class="toggle-menu-act" onclick="UI.toggleMenu(this);">
                                <span></span>
                            </a>
                            <div class="toggle-menu">
                            <div class="toggle-menu-arrow"><span></span></div>
                            <ul class="toggle-menu-block">
                                
                                <a href="/mail?act=show&peer=<?=$data['id']?>">Написать</a>
                                
                            </ul>
                            </div>
</div>
			<div class="post-l emotion-show" data-emotions="<?=system::textOut($data['emotions'])?>"><a href="<?=profile :: getLink($data)?>"><?=profile :: userAvatar($data, 0)?></a></div>
			<div class="post-c">
				<span class="_name"><a href="<?=profile :: getLink($data)?>"><?=($qs != "" ? preg_replace('~' . preg_quote($qs) . '~iu', '<span class="highlight">$0</span>', profile :: getFullName($data)) : profile :: getFullName($data))?></a></span><br />
			<span class="_time"><?=$data['info_first_name']?></a>
<?php
			if($table == 3){?>
				<div class="_gray _font-m" style="margin-bottom: 3px;"><?=$r['message']?></div>
				<div class="_lh-n">
				<span class="_dib _oh"><a href="/friends?id=<?=$user['id']?>&amp;new=<?=$data['id']?>&c=<?=$r['code']?>" class="main-btn _dib">Принять</a></span> 
				<span class="_dib _oh"><a href="/friends?id=<?=$user['id']?>&amp;cancel=<?=$data['id']?>&c=<?=$r['code']?>" class="action-btn _dib">Отклонить</a></span>
				</div>
			<?php } ?>
			</div>
		</div>
		<?php
		
	}
	
	if($i == 0) {?><div class="list _gray _font-s _center"><?=( $table == 3 ? "Заявок" : ($table == 4 ? "Общих друзей" : 'Друзей') )?> не найдено.</div><?php }

	 if ($total > 10)
        {
            echo $pagination -> printPages($link."?id={$ank['id']}&amp;"); 
        }
	echo "</div>";
	require (CORE."foot.php");