<?

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();
//Подарок
$post = mysql_fetch_assoc(mysql_query("SELECT id,status,coment,id_gift,id_ank,id_user,time FROM `gifts_user` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1"));
//Если записи нет кидаем на главную
if (!$post['id']){ 
header("Location: /index.php?"); 
} 
//Сам Подарок 
$gift = mysql_fetch_assoc(mysql_query("SELECT id,name FROM `gift_list` WHERE `id` = '" . $post['id_gift'] . "' LIMIT 1"));
//Кому подарили
$ank = get_user($post['id_user']);
//Кто подарил
$anketa = get_user($post['id_ank']);
//Принятие подарка
if ($post['status'] == 0 && isset($_GET['ok']) && $user['id'] == $ank['id']){
mysql_query("UPDATE `gifts_user` SET `status` = '1' WHERE `id` = '$post[id]' LIMIT 1");
//Уведомления
mysql_query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `type`, `time`) VALUES ('$user[id]', '$anketa[id]', '$gift[id]', 'ok_gift', '$time')");
//Сообщение 
$_SESSION['message'] = 'Подарок от ' . $anketa['nick'] . ' принят';
header("Location: gift.php?id=$post[id]"); 
exit;	
}
//Отказ от подарка
if ($post['status'] == 0 && isset($_GET['no']) && $user['id'] == $ank['id']){
mysql_query("DELETE FROM `gifts_user` WHERE `id` = '$post[id]' LIMIT 1");
//Уведомления
mysql_query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `type`, `time`) VALUES ('$user[id]', '$anketa[id]', '$gift[id]', 'no_gift', '$time')");
$_SESSION['message'] = 'Подарок от ' . $anketa['nick'] . ' отклонен';
header("Location: ?new");
exit;
}
//Удаление подарка
if (isset($_GET['delete']) && ($ank['id'] == $user['id']  || $user['level'] > 2)){
//Запрос удаления
mysql_query("DELETE FROM `gifts_user` WHERE `id` = '$post[id]' LIMIT 1");
//Сообщение 
$_SESSION['message'] = 'Подарок от ' . $anketa['nick'] . ' удален';
header("Location: index.php"); 
exit;
}
//Заголовок страницы
$set['title'] = 'Подарок ' . $ank['nick'] . ' ' . htmlspecialchars($gift['name']);
include_once '../../sys/inc/thead.php';
title();
aut();

# Панель
echo '<table class="block_panel" cellspacing="0" cellpadding="0">';
echo '<td><a class="panel" href="/id'.$ank['id'].'">' .user::nick($ank['id'],0).'</a><td>';
echo '<td><a class="panel" href="/user/gift/index.php?id='.$ank['id'].'">Подарки</a></td>';
echo '</table>';

//Подарок
echo '<div class="block2">';
echo '<center><img src="/style/gift/' . $gift['id'] . '.png"></center>';
echo '</div>'; 	

//Автор подарка
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $anketa[id] LIMIT 1"));

echo '<table class="block2">';
echo '<td class="block_avatar">';
echo ''.avatar($anketa['id']).' <span class="avatar_icon">'.online($anketa['id']).'</span>';
echo '</td>';
echo '<td class="block_info">';
//Вывод дополнительного меню
if (isset($user) && $ank['id']==$user['id']){
echo '<a class="toggle-menu-act" onclick="UI.toggleMenu(this);"><span></span></a>';
echo '<div class="toggle-menu">';
echo '<div class="toggle-menu-arrow"><span></span></div>';
echo '<ul class="toggle-menu-block">';
echo '<a href="/user/gift/gift.php?id=' . $post['id'] . '&amp;delete">'.$lang['del'].'</a>';
echo '</ul>';
echo '</div>';
}
echo '<span class="nick">';
echo ''.user::nick($anketa['id']).'';
echo '</span>';
echo '<span class="time">' . vremja($post['time']) . '</span>';
echo '<div class="block_msg">';
if ($post['coment']){
echo '' . output_text($post['coment']).'';
}else{
echo 'Без комментариев';
}
echo '</div>';
echo '</td>';
echo '</table>';

if ($ank['id'] == $user['id']){

//Новый подарок
if ($post['status'] == 0){	
echo '<table style="text-align:center;" class="menu">';
echo '<td><a style="display: block; background: #34bfad; padding: 6px; color: white; border-radius: 3px;" href="?id=' . $post['id'] . '&amp;ok">Принять</a></td>';
echo '<td><a style="display: block; background: #e68c85; padding: 6px; color: white; border-radius: 3px;" href="?id=' . $post['id'] . '&amp;no">Отказаться</a></td>';
echo '</table>';
}

}

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

?>