<?php

/* DCMS Special
 * Дата последнего редактирования 02.10.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

only_reg();

$ank['id'] = $user['id'];

$doc->Title('Предложения дружбы');

require_once H . 'sys/inc/thead.php';

aut();

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='/info.php?id=$user[id]'>$user[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Друзья');
echo "</small></div><br />";

if (isset($_GET['ok'])) {
    $ok = intval($_GET['ok']);
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ok' LIMIT 1"), 0) == 0) {
        header("Location: index.php?");
        exit;
    }

    mysql_query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$ank[id]', '$ok', '$time', '1')");
    mysql_query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$ok', '$ank[id]', '$time', '1')");
    mysql_query("DELETE FROM `friends_new` WHERE `user` = '$ok' AND `to` = '$user[id]' LIMIT 1");
    mysql_query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$ok' LIMIT 1");
    mysql_query("OPTIMIZE TABLE `friends`");
    mysql_query("OPTIMIZE TABLE `friends_new`");

    $msg = "" . ($user['sex'] ? '' . lang('подтвердил') . '' : '' . lang('подтвердила') . '') . ", " . lang('что Вы друзья');
    mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ok', '" . mysql_real_escape_string($msg) . "', '$time')");
    $_SESSION['message'] = lang('Пользователь добавлен в список Ваших друзей');
}

if (isset($_GET['no'])) {
    $no = intval($_GET['no']);
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$no' LIMIT 1"), 0) == 0) {
        header("Location: index.php?");
        exit;
    }

    mysql_query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$no' LIMIT 1");
    mysql_query("DELETE FROM `friends` WHERE `user` = '$no' AND `friends` = '$user[id]' LIMIT 1");
    mysql_query("DELETE FROM `friends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");
    mysql_query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");
    mysql_query("OPTIMIZE TABLE `friends`");
    mysql_query("OPTIMIZE TABLE `friends_new`");

    $msg = "" . ($user['sex'] ? '' . lang('отклонил') . '' : '' . lang('отклонила') . '') . " " . lang('Ваше предложение дружбы');
    mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$no', '" . mysql_real_escape_string($msg) . "', '$time')");

    $_SESSION['message'] = lang('Предложение отклонено');

    header("Location: new.php?r=outgoing");
    exit();
}

if (isset($_GET['del'])) {
    $no = intval($_GET['del']);

    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$no' LIMIT 1"), 0) == 0) {
        header("Location: index.php?");
        exit;
    }

    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$no') OR (`user` = '$no' AND `friends` = '$user[id]') LIMIT 1"), 0) > 0) {
        mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$no', '" . ($user['sex'] ? '' . lang('удалил') . '' : '' . lang('удалила') . '') . " " . lang('Вас из списка своих друзей') . ".', '$time')");
        mysql_query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$no' LIMIT 1");
        mysql_query("DELETE FROM `friends` WHERE `user` = '$no' AND `friends` = '$user[id]' LIMIT 1");
        mysql_query("DELETE FROM `freinds_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");
        mysql_query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");
        mysql_query("OPTIMIZE TABLE `friends`");
        mysql_query("OPTIMIZE TABLE `friends_new`");

        $_SESSION['message'] = lang('Пользователь удален из списка ваших друзей');

        header("location:  " . htmlspecialchars($_SERVER['HTTP_REFERER']) . "");
    }
    exit;
}

$k_online = mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` INNER JOIN `user` ON `friends`.`friends`=`user`.`id` WHERE `friends`.`user` = '$ank[id]' AND `friends`.`i` = '1' AND `user`.`date_last`>'" . (time() - 300) . "'"), 0);
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE `user` = '$ank[id]' AND `i` = '1'"), 0);
$k_z = mysql_result(mysql_query("SELECT COUNT(id) FROM `friends_new` WHERE `to` = '$ank[id]'"), 0);


echo "<div class='list-group-item-null list-group-item-grey'>";
echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
echo "<li class='nav-item'><a class='nav-link' href='index.php'>" . lang('Друзья') . " <span class='label label-default'>$k_post</span></a></li>";
echo "<li class='nav-item'><a class='nav-link active'>" . lang('Заявки') . " <span class='label label-default'>$k_z</span></a></li>";
echo "<li class='nav-item'><a class='nav-link' href='online.php'>" . lang('Онлайн') . " <span class='label label-default'>$k_online</span></a></li>";
echo "</ul>";
echo "</div>";

$m = date('m', $time);
if (substr($m, 0, 1) == 0) {
    $m = str_replace('0', '', $m);
}
$d = date('d', $time);
$k_f = mysql_result(mysql_query("SELECT COUNT(id) FROM `friends_new` WHERE `to` = '$user[id]' LIMIT 1"), 0);

$r = (isset($_GET['r'])) ? htmlspecialchars($_GET['r']) : null;
switch ($r) {
    default:
        echo "<div class='list-group-item-null'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Входящие') . "</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='/user/friends/new.php?r=outgoing'>" . lang('Исходящие') . "</a></li>";
        echo "</ul>";
        echo "</div>";

        if ($k_f == 0) {
            $doc->NoResult();
        }

        $q = mysql_query("SELECT * FROM `friends_new` WHERE `to` = '$user[id]' ORDER BY time DESC");
        while ($f = mysql_fetch_array($q)) {
            $ank = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $f['user'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null'><tr><td class = 'icon14'>";
            avatar($ank['id'], '48', 'border-radius: 2px;');
            echo "</td><td class = 'null'>";
            echo "<small style='float: right;color: grey'>" . date::times($f['time']) . "</small>";

            echo user($ank['id'])." ". lang('хочет добавить Вас в друзья')."<br />";

            echo "<span style='float: right;'><a href='new.php?no=$ank[id]'><font color='red'>" . lang('Отклонить') . "</font></a></span>";
            echo "<a href='new.php?ok=$ank[id]'> <font color='green'><i class='fa fa-check fa-fw'></i> " . lang('Подтвердить') . "</font></a> ";

            echo "</td></tr></table>";
            echo "<div class='hr'></div>";
        }

        break;

    case 'outgoing':
        $k_t = mysql_result(mysql_query("SELECT COUNT(id) FROM `friends_new` WHERE `user` = '$user[id]' LIMIT 1"), 0);

        echo "<div class='list-group-item-null'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link' href='/user/friends/new.php?'>" . lang('Входящие') . "</a></li>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Исходящие') . "</a></li>";
        echo "</ul>";
        echo "</div>";

        if ($k_t == 0) {
            $doc->NoResult();
        }
        $q = mysql_query("SELECT * FROM `friends_new` WHERE `user` = '$user[id]' ORDER BY time DESC");
        while ($f = mysql_fetch_array($q)) {
            $ank = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $f['to'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null'><tr><td class = 'icon14'>";
            avatar($ank['id'], '48', 'border-radius: 2px;');
            echo "</td><td class = 'null'>";
            user($ank['id']);
            echo " <small style='float: right; color:grey;'>" . date::times($f['time']) . "</small>";
            echo "<br />";
                        echo "<span style='float:right'>";
            echo "<a href = 'new.php?no=$ank[id]' class='hint--left' data-hint='" . lang('Отменить заявку') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
            echo "</span>";
            echo lang('Ожидается ответ') . "...";
            echo "</td></tr></table>";
            echo "<div class='hr'></div>";
        }

        break;
}

require_once H . 'sys/inc/tfoot.php';
