<?
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/adm_check.php';
include_once '../../sys/inc/user.php';

if (!isset($user))header("location: /index.php?");


if (isset($_GET['delete_icon']) && $_GET['delete_icon']=='1')
{ 
if (isset($user)) 
{ 
mysql_query("UPDATE `user_icons` SET `id_user` = '0', `time` = '0' WHERE `id_user` = '$user[id]' LIMIT 1");	
$_SESSION['message']='Иконка успешно удалена'; 
header("Location: ?" ); 
exit; 
} 
}


if (isset($user) && isset($_POST['on_i']))
{
	$icon = mysql_fetch_assoc(mysql_query("SELECT * FROM `user_icons` WHERE `id` = '".intval($_POST['on_i'])."' LIMIT 1"));
	
	if ($user['money'] < 10)
	$err[] = "<img src='/style/icons/error.png' alt='*'> <font color='red'>У Вас недостаточно монет для покупки персональной иконки.</font>";
	
	if (!$icon['id'])
	$err[] = 'Такой иконки не существует';
	
	if ($icon['time'] < time()-604800 && $icon['id_user'] == $user['id'])
	$err[] = 'Вы уже купили эту иконку';
	
	if (!isset($err))
	{
		
		mysql_query("UPDATE `user_icons` SET `id_user` = '$user[id]', `time` = '$time' WHERE `id` = '$icon[id]' LIMIT 1");
		mysql_query("UPDATE `user` SET `money` = '".($user['money'] - 10)."' WHERE `id` = '$user[id]' LIMIT 1");	
		
		$_SESSION['message'] = "<img src='/style/icons/success.png' alt='*'> <font color='green'>Персональная иконка успешно куплена</font>";
		header('Location: ?');
		exit;
	}
}


$set['title'] = 'Персональные иконки';
include_once '../../sys/inc/thead.php';
title();

err();
aut();


echo '<div class="nav1"><form action="?" method="post"><center>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `user_icons`"),0);
$q = mysql_query("SELECT * FROM `user_icons`");
while ($post = mysql_fetch_assoc($q))
{

echo '<input type="radio" name="on_i" value="'.$post['id'].'"/> <b>-</b> ';
echo ' <img src="/style/personal_icons/i ('.$post['id'].').png" alt="*" />  ';

}
echo '</center><br><center><b>Цена :: <font color="green">10</font> монет.</b><br/> 
<input type="submit" value="Сменить иконку"/></form></br></center>';	

if ($k_post != 0){
echo '<br><a href="?delete_icon=1"><font color="red"><b>Удалить иконку</b></font></a>';
}

echo '</div>';


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