<?php

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

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

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
} elseif (isset($user)) {
    $id = $user['id'];
}
$ank = get_user($id);
if (!$ank || $ank['id'] == 0) {
    header("Location:/index.php?");
    exit;
}

$set['title'] = lang('Подарки') . ' ' . $ank['nick'];
require_once H . 'sys/inc/thead.php';

aut();

define('DIR_GIFT', '/modules/gifts/');

if (isset($_GET['do'])) {
    only_reg();
    if (isset($_GET['cat']) && $db->super_query("SELECT COUNT(*) as count FROM `gift_cat` WHERE `id` = '" . intval($_GET['cat']) . "'") != 0) {
        $cat = $db->get_array($db->query("SELECT * FROM `gift_cat` WHERE `id` = '" . intval($_GET['cat']) . "'"));
        if ($cat['show'] == 1 || $cat['show'] == 0 && $user['level'] >= 3) {
            $cat = $cat;
        } else {
            $cat = array();
            $cat['id'] = 1;
            $cat['show'] = 1;
        }
    } else {
        $cat = array();
        $cat['id'] = 1;
        $cat['show'] = 1;
    }
    if (isset($_GET['gift']) && intval($_GET['gift']) != NULL && $db->super_query("SELECT COUNT(*) as count FROM `gift` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_cat` = '$cat[id]'") != 0 && ($cat['show'] == 1 || $cat['show'] == 0 && $user['level'] >= 3)) {
        $gift = $db->get_array($db->query("SELECT * FROM `gift` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_cat` = '$cat[id]'"));

        #Навигация
        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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='" . DIR_GIFT . "'>" . lang('Подарки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Новый подарок');
        echo "</small></div><br />";

        if (isset($_POST['submited'])) {
            $msg = $_POST['msg'];
            if (utf8_strlen($msg) > 512) {
                $_SESSION['err'] = lang('Слишком длинное сообщение');
            }
            if ($user['money'] < $gift['cena']) {
                $_SESSION['err'] = lang('Вам не хватает рублей');
            }
            if ($user['id'] == $ank['id']) {
                $_SESSION['err'] = lang('Нельзя дарить себе');
            }
            if (!isset($_SESSION['err'])) {
                if ($_POST['mode'] == 2 || $_POST['mode'] == 3) {
                    $type = intval($_POST['mode']);
                } else {
                    $type = 1;
                }
                $db->query("INSERT INTO `gifts` SET `id_user` = '$ank[id]', `id_kont` = '$user[id]', `msg` = '" . $db->safesql($msg) . "', `time` = '$time', `id_gift` = '$gift[id]', `type` = '$type'");
                $id = $db->insert_id();
                $db->query("UPDATE `user` SET `money` = '" . ($user['money'] - $gift['cena']) . "' WHERE `id` = '$user[id]'");

                if ($user['sex'] == 0) {
                    $sex = lang('прислала');
                } elseif ($user['sex'] == 1) {
                    $sex = lang('прислал');
                }

                $msg = "$sex " . lang('Вам подарок') . ". [url=/modules/gifts/?id=$ank[id]&amp;gift=$id]" . lang('смотреть') . "[/url]";

                $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank[id]', '6', '$msg', '$time')");

                $_SESSION['message'] = lang('Подарок успешно отправлен');
                header("Location: ?id=$ank[id]");
                exit;
            }
        }

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo lang('Подарок для') . " <a href='/id$ank[id]'><b>$ank[nick]</b></a>";
        echo "</div><div class='list-group-item-null'>";
        echo "<center><img src='images/$gift[image]' /></center><br />";
        echo lang('Стоимость') . ": <font color='green'><b><i class='fa fa-money fa-fw'></i> $gift[cena]</b></font><br />";
        echo lang('У Вас на счету') . ": <font color='green'><b><i class='fa fa-money fa-fw'></i> $user[money]</b></font><br />";
        echo "</div>";

        echo "<form method='post' class='list-group-item-null list-group-item-grey' action=''>";
        echo lang('Тип подарка') . ":<br />";

        echo "<label class='custom-control custom-radio'><input value='1' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-globe fa-fw'></i> " . lang('Публичный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок, сообщение и логин') . ".</small><br />";
        echo "<label class='custom-control custom-radio'><input value='2' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-lock fa-fw'></i> " . lang('Личный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок, но только получатель сможет видеть Ваш логин и сообщение') . ".</small><br />";
        echo "<label class='custom-control custom-radio'><input value='3' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-eye-slash fa-fw'></i> " . lang('Анонимный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок. Только получатель увидит ваше сообщение. Никто не увидит Ваш логин') . ".</small><br /><br />";

        $doc->Textarea('msg', 'Комментарий к подарку', 512, "Лови подарок");
        echo "<br />";
        $doc->Button('btn btn-success btn-sm', 'submited', 'gift', 'Отправить');

        echo "<span style='color:red;'><i class='fa fa-money fa-fw'></i> -$gift[cena] " . lang('руб') . ".</span>";
        echo "</form>";

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

    $q = $db->query("SELECT * FROM `gift_cat`");

    #Навигация
    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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_GIFT . "'>" . lang('Подарки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Новый подарок');
    echo "</small></div><br />";

    echo "<div class='list-group-item-null'>";
    echo "<span style='float: right;color: green;'><i class='fa fa-money fa-fw'></i> $user[money]</span>";
    echo lang('Выберите подарок для') . " ";
    echo "<a href = '/id$ank[id]'>";
    echo gradient("$ank[nick]", "$ank[ncolor]", "$ank[ncolor2]");
    echo "</a> ";
    echo "</div>";

    $k_p = $db->super_query("SELECT COUNT(*) as count FROM gift_cat");
    $k_p = $k_p['count'];

    if ($k_p == 0) {
        echo "<div class='list-group-item-null'><small style='color: grey;'>" . lang('Категории не созданы') . "</small></div>";
    } else {
        $i = 0;

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-pills'>";

        while ($post = $db->get_array($q)) {
            if ($post['show'] == 1 || $post['show'] == 0 && $user['level'] >= 3) {
                echo ($i != 0 && $i < $db->num_rows($q) ? "" : NULL) . ($post['id'] != $cat['id'] ? "<li class='nav-item'><a class='nav-link' href='?id=$ank[id]&amp;do&amp;cat=$post[id]'>" : "<li class='nav-item'><a class='nav-link active'>") . "$post[name]" . ($post['id'] != $cat['id'] ? "</a></li>" : "</a></li>") . "";
            }
            $i++;
        }
        echo "</ul>";
        echo "</div>";
    }

    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `gift` WHERE `id_cat` = '$cat[id]'");
    $k_post = $k_post['count'];

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

    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];
    $q = $db->query("SELECT * FROM `gift` WHERE `id_cat` = '$cat[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
    while ($post = $db->get_array($q)) {
        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        echo "<img src='images/$post[image]' height='40' width='40' /> ";
        echo "</td><td class='null'>";
        echo "<span class='tag tag-info right'><i class='fa fa-money fa-fw'></i> $post[cena] " . lang('руб') . "</span>";

        echo "<a href='?id=$ank[id]&amp;do&amp;cat=$cat[id]&amp;gift=$post[id]'>";
        echo toOutput($post['name']);
        echo "</a>";
        echo "</td></tr></table>";
        echo "<div class='hr'></div>";
    }

    if ($k_page > 1) {
        str("?id=$ank[id]&amp;do&amp;cat=$cat[id]&amp;", $k_page, $page);
    }

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

if (isset($_GET['gift']) && intval($_GET['gift']) != NULL && $db->super_query("SELECT COUNT(*) as count FROM `gifts` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_user` = '$ank[id]'") != 0) {
    $gift = $db->get_array($db->query("SELECT * FROM `gifts` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_user` = '$ank[id]'"));
    $giftt = $db->get_array($db->query("SELECT * FROM `gift` WHERE `id` = '$gift[id_gift]'"));
    $ank2 = get_user($gift['id_kont']);
    if ($gift['type'] == 2) {
        $type = "<i class='fa fa-lock fa-fw'></i> " . lang('Личный');
    } elseif ($gift['type'] == 3) {
        $type = "<i class='fa fa-eye-slash fa-fw'></i> " . lang('Анонимный');
    } else {
        $type = "<i class='fa fa-globe fa-fw'></i> " . lang('Публичный');
    }

    #Навигация
    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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Подарки');
    echo "</small></div><br />";

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    echo "<img src='images/$giftt[image]' /><br />";
    echo "</td><td class='null'>";
    if ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && $user['level'] >= 3) {
        echo user($ank2['id']);
        echo "<br />";
    } else {
        echo "<i class='fa fa-user-secret fa-fw'></i> Аноним<br />";
    }

    if ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && ($ank['id'] == $user['id'] || $user['level'] >= 3)) {
        if ($gift['msg'] != NULL) {
            echo "<div class='user__status user__status-message'>";
            echo toOutput($gift['msg']);
            echo "</div>";
        }
    }
    echo "<span style='color: grey;' title='" . date::time($gift['time']) . "'>$type " . date::times($gift['time']) . "</span>";

    if ($ank['id'] == $user['id'] || $user['level'] >= 3) {
        echo "<br />";
        if ($ank['id'] == $user['id'] && ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && $user['level'] >= 3)) {
            $doc->Link('btn btn-success btn-sm', "?id=$ank2[id]&amp;do", 'gift', 'Отправить подарок');
        }
        $doc->Link('btn btn-success btn-sm', "?id=$ank[id]&amp;del=$gift[id]", 'trash-o', 'Удалить');
    }

    echo "</td></tr></table>";

    require_once H . 'sys/inc/tfoot.php';
    exit();
}
if (isset($_GET['del']) && intval($_GET['del']) != NULL && $db->super_query("SELECT COUNT(*) as count FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'") != 0) {
    $gift = $db->get_array($db->query("SELECT * FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'"));
    if ($gift['id_user'] == $user['id'] || $user['level'] >= 3) {
        $db->query("DELETE FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'");
        header("Location:?id=$ank[id]");
        exit;
    }
}

$q = $db->super_query("SELECT COUNT(*) as count FROM `gifts` WHERE `id_user` = '$user[id]' AND `read` = '0'");
if ($q['count'] != 0 && $ank['id'] == $user['id']) {
    $db->query("UPDATE `gifts` SET `read` = '1' WHERE `id_user` = '$user[id]' AND `read` = '0'");
    header("Location:?id=$user[id]");
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `gifts` WHERE `id_user` = '$ank[id]'");
$k_post = $k_post['count'];

#Навигация
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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Подарки');
echo "</small></div><br />";

$doc->Link('list-group-item-null list-group-item-grey', "?id=$ank[id]&amp;do", 'gift', 'Отправить подарок');

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

$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];
$q = $db->query("SELECT * FROM `gifts` WHERE `id_user` = '$ank[id]' ORDER BY `time` DESC LIMIT $start, $set[p_str]");
while ($post = $db->get_array($q)) {
    $ank2 = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '$post[id_kont]'"));
    $gift = $db->get_array($db->query("SELECT * FROM `gift` WHERE `id` = '$post[id_gift]'"));

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    echo "<img src='images/$gift[image]' height='30' width='30'/>";
    echo "</td><td class='null'>";
    echo "<div style='float:right;' id='hides'>";
    echo "<span title='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</span>";
    echo "</div>";

    echo "<a href='?id=$ank[id]&amp;gift=$post[id]'>" . gradient("$ank2[nick]", "$ank2[ncolor]", "$ank2[ncolor2]") . "</a>";
    echo "</td></tr></table>";
    echo "<div class='hr'></div>";
}

if ($k_page > 1) {
    str("?id=$ank[id]&amp;", $k_page, $page);
}

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

