<?php
require_once ('system/func.php');
$title = 'Друзья';
require_once ('system/header.php');

auth(); // Закроем от гостей

if (isset($_GET['ok'])) {
    mysql_query("UPDATE `friends` SET `status` = '1' WHERE `id` = '".intval($_GET['ok'])."' AND (`user_2` = '".$myID."') LIMIT 1");
    $_SESSION['msg'] = "Заявка принята!";
    header('Location: ?new');
    exit();
} elseif (isset($_GET['no'])) {
    mysql_query("DELETE FROM `friends` WHERE `id` = '".intval($_GET['no'])."' AND (`user_1` = '".$myID."' OR `user_2` = '".$myID."') LIMIT 1");
    $_SESSION['msg'] = "Друг удален!";
    header('Location: ?');
    exit();
}


if (isset($_GET['new'])) {
    $set['p_str'] = 10;
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user_2` = '".$myID."') AND `status` = '0'"),0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str']*$page-$set['p_str'];
    $q = mysql_query("SELECT * FROM `friends` WHERE (`user_2` = '".$myID."') AND `status` = '0' ORDER BY `time` DESC LIMIT $start, $set[p_str]");
} else {
    $set['p_str'] = 10;
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user_1` = '".$myID."' OR `user_2` = '".$myID."') AND `status` = '1'"),0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str']*$page-$set['p_str'];
    $q = mysql_query("SELECT * FROM `friends` WHERE (`user_1` = '".$myID."' OR `user_2` = '".$myID."') AND `status` = '1' ORDER BY `time` DESC LIMIT $start, $set[p_str]");
}

echo div('header').'<div class="cntr white m5"><div class="tptrn ib"><div class="p_end"><a href="?">Друзья</a> / <a href="?new">Заявки</a></div></div></div></div>';

if($k_post == 0)echo "<div class='block_c'>Список друзей пуст</div>";

while ($post = mysql_fetch_assoc($q)){

echo "<div class='wrapper white'>";
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".($post['user_1'] == $myID?$post['user_2']:$post['user_1'])."' LIMIT 1"));

echo "<li><a href='/profile/$ank[id]'>";
echo "<font color='white'> $ank[login] </font>";
if ($post['status'] == 1) {
    echo '<a href="?no='.$post['id'].'">[Удалить]</a>';
} else {
    echo '<a href="?no='.$post['id'].'">[Отменить]</a>';
    echo '<a href="?ok='.$post['id'].'">[Принять]</a>';
}
echo "</a></li>";
echo "</div>";
}

if( $k_post > 10) {
echo"<div class='header'>";

str('?',$k_page,$page); // Вывод страниц
echo "</div>";
}
require_once ('system/footer.php');
