<?php 
	$queryUser = profile :: getUser(intval(abs($_GET['add'])));

	if (empty($queryUser['id'])) { system :: redirect ("Пользователя не существует.", $link."?id={$user['id']}", 1); }
	else $ank = $queryUser;

	if(!isset($_GET['access']) || (isset($_GET['access']) && $_GET['access'] != ACCESS)) {
		system :: redirect("Вероятно Вам подкинули эту ссылку.", profile :: getLink($ank), true); 
	}

	// Отмена запроса в друзья.
	$queryTryAddFruends = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `query_friends` WHERE `id_user` = '".$ank['id']."' AND `id_friend` = '".$user['id']."'");
	if ($queryTryAddFruends > 0) { 
		DB :: $pdo -> query("DELETE FROM `query_friends` WHERE `id_user` = ? AND `id_friend` = ? LIMIT 1;", array($ank['id'],$user['id']));
		DB :: $pdo -> query("UPDATE `users` SET `count_fr_new`=`count_fr_new`-1 WHERE `id` = ?;", array($ank['id']));

   
		system :: redirect(NULL, profile :: getLink($ank)); 
	}

	// Уже отправили заявку, принять.
	$queryTryAddFruendsTwo = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `query_friends` WHERE `id_user` = '".$user['id']."' AND `id_friend` = '".$ank['id']."'");
	if ($queryTryAddFruendsTwo > 0) { 
		DB :: $pdo -> query("INSERT INTO `friends` SET `id_user` = '".$user['id']."', `id_friend` = '".$ank['id']."'");
		DB :: $pdo -> query("INSERT INTO `friends` SET `id_user` = '".$ank['id']."', `id_friend` = '".$user['id']."'");
		
		DB :: $pdo -> query("UPDATE `users` SET `count_friends`=`count_friends`+1 WHERE `id` = ?;", array($user['id']));
		DB :: $pdo -> query("UPDATE `users` SET `count_friends`=`count_friends`+1 WHERE `id` = ?;", array($ank['id']));
		DB :: $pdo -> query("UPDATE `users` SET `count_fr_new`=`count_fr_new`-1 WHERE `id` = ?;", array($user['id']));



		
		DB :: $pdo -> query("DELETE FROM `query_friends` WHERE `id_user` = '".$user['id']."' AND `id_friend` = '".$ank['id']."'");
		system :: redirect(NULL, profile :: getLink($ank));
	}

			
			
			
			
			
			
			
			
			
			
			
			
			
			

	if ($ank['id'] == $user['id']){  system :: redirect("Вот не нужно так делать. Самого себя в друзья нельзя добавлять.", profile :: getLink($user), 1); }
	 
	// Уже в друзьях. Удалить из друзей.
	$queryTest = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `friends` WHERE `id_user` = '".$user['id']."' AND `id_friend` = '".$ank['id']."'");  
	if ($queryTest > 0){  
		DB :: $pdo -> query("DELETE FROM `friends` WHERE `id_user` = ? AND `id_friend` = ?;", array($user['id'],$ank['id']));
		DB :: $pdo -> query("DELETE FROM `friends` WHERE `id_user` = ? AND `id_friend` = ?;", array($ank['id'],$user['id']));
		
		DB :: $pdo -> query("UPDATE `users` SET `count_friends`=`count_friends`-1 WHERE `id` = ?;", array($user['id']));
		DB :: $pdo -> query("UPDATE `users` SET `count_friends`=`count_friends`-1 WHERE `id` = ?;", array($ank['id']));
		
		DB :: $pdo -> query("UPDATE `users` SET `count_fr_new`=`count_fr_new`+1 WHERE `id` = ?;", array($user['id']));
		
		
       
		DB :: $pdo -> query("INSERT INTO `query_friends` SET `id_user` = '".$user['id']."', `id_friend` = '".$ank['id']."'");
		system :: redirect(NULL, profile :: getLink($ank));
	}

	if (isset ($_POST['friend'])) {
		$code = mt_rand(1936234, 9364912);
		$message = system :: check(system :: text(system :: cutText($_POST['message'], 130)));
		if(DB :: $pdo -> query("INSERT INTO `query_friends` SET `message` = ?, `id_user` = ?, `id_friend` = ?, `code` = ?;", array($message, $ank['id'], $user['id'], $code))) {
		DB :: $pdo -> query("UPDATE `users` SET `count_fr_new`=`count_fr_new`+1 WHERE `id` = ?;", array($ank['id']));
		 
       notif::init()->friend(['id_user' => $ank['id'],   'id_last' => $user['id'], 'id_obj' => $code, 'notices_text' => $message]);
       DB:: $pdo->query("UPDATE `users` SET `count_new_notice` = `count_new_notice`+1 WHERE `id` = '".$ank['id']."' LIMIT 1;");
                   
		    
		}
		system :: redirect(NULL, profile :: getLink($ank));
	}


	$title = $l = 'Запрос в друзья';
	require (CORE."head.php");

	?>
<div class="app-block">
	<div class="list _gray _bg-gray _font-m">
		Вы действительно хотите отправить уведомление о дружбе пользователю <?=profile :: goPage($ank)?>?
	</div>
	<div class="list _gray">
		<form action="" method="POST">    
			<label class="_dbl" for="friend-message">Прикрепить сообщение(130 символов):</label>
			<textarea name="message" id="friend-message"></textarea>
			   
			<input type="submit" name="friend" value="Отправить" class="_dbl _w-100" style="margin: 3px 0;">
		</form>         
	</div>
	<?php
	echo system :: back(profile :: getLink($ank), "Отменить");
	require (CORE."foot.php");