<?

include_once '../../sys/inc/start.php';
include_once '../../sys/inc/compress.php';
include_once '../../sys/inc/sess.php';
include_once '../../sys/inc/home.php';
include_once '../../sys/inc/settings.php';
include_once '../../sys/inc/db_connect.php';
include_once '../../sys/inc/ipua.php';
include_once '../../sys/inc/fnc.php';
include_once '../../sys/inc/user.php';

only_reg();

if (isset($_GET['id']))$ank['id'] = intval($_GET['id']);
$ank = get_user($ank['id']);

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

$set['title']='Подарок '.user::nick($ank['id'],0).'';
include_once '../../sys/inc/thead.php';
title();
aut();

//Дарим подарок
if (isset($_GET['gift']) && isset($_GET['category'])){
//Категория
$category = mysql_fetch_assoc(mysql_query("SELECT * FROM `gift_categories` WHERE `id` = '" . intval($_GET['category']) . "' LIMIT 1"));
//Подарок
$gift = mysql_fetch_assoc(mysql_query("SELECT * FROM `gift_list` WHERE `id` = '" . intval($_GET['gift']) . "' LIMIT 1"));
if (isset($_GET['ok']) ){
if ($user['money'] >= $gift['money']){	
//Комментарий
$msg = my_esc($_POST['msg']);  
mysql_query("UPDATE `user` SET `money` = '" . ($user['money'] - $gift['money']) . "' WHERE `id` = '$user[id]'");
mysql_query("INSERT INTO `gifts_user` (`id_user`, `id_ank`, `id_gift`, `coment`, `time` , `status`) values('$ank[id]', '$user[id]', '$gift[id]', '$msg', '$time', '1')");
$id_gift = mysql_insert_id();

//Уведомления о подарках
mysql_query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `type`, `time`) VALUES ('$user[id]', '$ank[id]', '$id_gift', 'new_gift', '$time')");

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

}else{

$err = 'У вас не достаточно средств на счету';

}
}

err(); 

//Панель
echo '<table class="block_panel" cellspacing="0" cellpadding="0">';
echo '<td><a class="panel" href="?id=' . $ank['id'] . '">Категории</a></td>';
echo '<td><a class="panel" href="">Подарок</a></td>';
echo '<td><a class="panel" href="/id'.$ank['id'].'">' . $ank['nick'] . '</a></td>';
echo '</table>';

echo '<div class="block">';
echo '<form action="?category=' . $category['id'] . '&amp;gift=' . $gift['id'] . '&amp;id=' . $ank['id'] . '&amp;ok" name="message" method="post">';
echo '<center><img width="100" src="/style/gift/' . $gift['id'] . '.png"></center>';
echo '</div>';
echo '<div class="block2">';
echo 'Получатель: <span class="nick">'.user::nick($ank['id'],0).'</span></br>';
echo 'Стоимость: <font color="green"><b>' . intval($gift['money']) . '</b></font> монет (У вас <font color="green"><b>' . $user['money'] . '</b></font> монет)';
echo '</div>'; 

echo '<div class="block2">';
echo '<textarea class="input" placeholder="Напишите сообщение..." type="text" name="msg"></textarea>';
echo '<table>';
echo '<td><input class="button2" value="Отправить" type="submit"></td>';
echo '</table>';
echo '</form>';
echo '</div>';

//Вывод смайлов
include_once H.'modules/smiles/smile.php';

}

else

//Вывод подарков
if (isset($_GET['category'])){
//Категория
$category = mysql_fetch_assoc(mysql_query("SELECT * FROM `gift_categories` WHERE `id` = '" . intval($_GET['category']) . "' LIMIT 1"));
if (!$category) {  
$_SESSION['message'] = 'Нет такой категории';
header("Location: ?");
exit;
}

//Панель
echo '<table class="block_panel" cellspacing="0" cellpadding="0">';
echo '<td><a class="panel" href="?id=' . $ank['id'] . '">Категории</a></td>';
echo '<td><a class="panel" href="/id'.$ank['id'].'">' . $ank['nick'] . '</a></td>';
echo '</table>';

//Список подарков
$k_post = mysql_result(mysql_query("SELECT COUNT(id) FROM `gift_list` WHERE `id_category` = '$category[id]'"),0);

if ($k_post == 0){
echo '<div class="block">Нет подарков</div>';
}

$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 name,id,money FROM `gift_list` WHERE `id_category` = '$category[id]' ORDER BY `id` LIMIT $start, $set[p_str]");

//Список подарков
while ($post = mysql_fetch_assoc($q)){

echo '<a class="link" href="?category=' . $category['id'] . '&amp;gift=' . $post['id'] . '&amp;id=' . $ank['id'] . '"><img width="100" src="/style/gift/' . $post['id'] . '.png"> <span class="count">' . intval($post['money']) . ' монет</span></a>';

}

//Вывод страниц
if ($k_page>1)str('categories.php?id=' . intval($_GET['id']) . '&amp;category=' . intval($_GET['category']) . '&amp;',$k_page,$page); 

}

else

//Категории
{

//Панель
echo '<table class="block_panel" cellspacing="0" cellpadding="0">';
echo '<td><a class="panel" href="/id'.$ank['id'].'">' . $ank['nick'] . '</a></td>';
echo '<td><a class="panel" href="?id=' . $ank['id'] . '">Категории</a></td>';
echo '</table>';

$k_post = mysql_result(mysql_query("SELECT COUNT(id) FROM `gift_categories`"),0);

if ($k_post == 0){
echo '<div class="block2">Нет категорий</div>';
}

$q = mysql_query("SELECT name,id FROM `gift_categories` ORDER BY `id`");

//Категории
while ($post = mysql_fetch_assoc($q)){
echo '<a class="link" href="categories.php?category=' . $post['id'] . '&amp;id=' . $ank['id'] . '"><span class="icon"><i class="fas fa-gift"></i></span> ' . htmlspecialchars($post['name']) . '';
echo '<span class="count">' . mysql_result(mysql_query("SELECT COUNT(id) FROM `gift_list` WHERE `id_category` = '$post[id]'"),0) . '</span></a>';

//Вывод случайных подарков с категории
echo '<div class="block2">';
$qq = mysql_query("SELECT name,id,money FROM `gift_list` WHERE `id_category` = '$post[id]' ORDER BY RAND() LIMIT 16");
while ($post = mysql_fetch_assoc($qq)){
echo '<a href="?category=' . $category['id'] . '&amp;gift=' . $post['id'] . '&amp;id=' . $ank['id'] . '"><img width="100" src="/style/gift/' . $post['id'] . '.png"></a>';
}
echo '</div>';

}

}

include_once '../../sys/inc/tfoot.php';

?>