<?php
	
	include_once __DIR__ . '/include/common.php';
	include_once __DIR__ . '/include/general_functions.php';
	include_once __DIR__ . '/include/utils.php';	
	include_once __DIR__ . '/include/user.php';
	
	if ( ! @$user)	{
		redirect('/');
	}
	
	$params	=	[];
	
	if (isset ($_GET['id']))
	{
		
		$info	=	get_user(
			$_GET['id']		//	id пользователя
		);
		
		if ( ! $info){
			redirect('/');
		}
	}
	else
	{
		
		$info	=	$user;
		
	}
	
	$params['title']	=	$info['id'] == $user['id'] ? 'Мой герой' : $info['login'];
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	$equip	=	[];
	
	$bonus	=	0;
	$all_bonus	=	0;
	
	$up	=	0;
	$runeList	=	[];
	
	for	(
			$i	=	1;
			$i	<	9;
			$i++
		)
	{
		
		if ($info[ $i ] != 0)
		{
			
			$item	=	get_user_item(
				$info['id'],
				$info[ $i ]
			);			
			
			$equip[ $i ]	=	$item['shop_id'];
			
			if ($item['bonus']!= 0)
			{
				$bonus	+=	$item['bonus'];
			}
			
			$all_bonus	+=	get_item_bonus(
				$item['quality']
			);
			
			if ($item['up']!= 0)
			{
				$up	+=	$item['up'];
			}
			
			if ($item['rune'] != 0)
			{
				$runeList[]	=	$item['rune'];
			}
			
		}
		
	}
	
	$abilities	=	get_user_abilities(
		$info['id']
	);
	
	$abilityList	=	[];
	for	(
		$i	=	1;
		$i	<	6;
		++$i
	)
	{
		
		if ($abilities	[ $i ]	!=	0)
		{
			
			$abilityList[ $i ]	=	$abilities[ $i ];
			
		}
		
	}
	
	?>
	<div class='container'>
	<img src="/images/icon/align/<?=($info['lastActive'] > time() - (60 * 15) ? $info['align'] : $info['align'] . '-off')?>.png" alt="" /> <b><?=$info['login']?></b> <img src="/images/icon/level.png" alt="" /> <?=$info['level']?> ур, <?=($info['align'] == 0 ? 'Асура' : 'Борея')?><br />
	<?php
	
	$query	=	@mysql_query(sprintf(
		'SELECT
				`clan_memb`.*,
				`clans`.`name`	as	`clan_name`,
				`clans`.`align`	as	`clan_align`
			FROM
				`clan_memb`
			LEFT JOIN
				`clans`
			ON
				`clans`.`id`	=	`clan_memb`.`clan_id`
			WHERE
				`clan_memb`.`user_id`	=	%d',
		$info['id']
	));
		
	if (@mysql_num_rows($query) != 0)
	{
		
		$memb	=	@mysql_fetch_assoc($query);
		
		//	
		$rank	=	get_clan_rank(
			$memb['rank']		//	index
		);
		
	?>
	
	<img src='/images/icon/clan/<?=$memb['clan_align']?>.png' alt='' /> <a href='/clan/<?=$memb['clan_id']?>'><?=$memb['clan_name']?></a>, <?=$rank?>
	
	<?php
		
	}
	?>
	<table>
		<tr>
		<td style="vertical-align:	middle;"><img src="/manekenImage/<?=$info['sex']?>/<?=(isset ($equip[1]) ? $equip[1] : 0)?>/<?=(isset ($equip[2]) ? $equip[2] : 0)?>/<?=(isset ($equip[3]) ? $equip[3] : 0)?>/<?=(isset ($equip[4]) ? $equip[4] : 0)?>/<?=(isset ($equip[5]) ? $equip[5] : 0)?>/<?=(isset ($equip[6]) ? $equip[6] : 0)?>/<?=(isset ($equip[7]) ? $equip[7] : 0)?>/<?=(isset ($equip[8]) ? $equip[8] : 0)?>" alt="" /></td>
		<td style="	vertical-align:	top;
					padding:	0px	0px 0px 2px;">
			<img src="/images/icon/str.png" alt="" /> Сила: <?=$info['str']?><br />
			<img src="/images/icon/agi.png" alt="" /> Ловкость: <?=$info['dex']?><br />
			<img src="/images/icon/vit.png" alt="" /> Жизни: <?=$info['life']?><br />
			<img src="/images/icon/def.png" alt="" /> Защита: <?=$info['protection']?><br />
			<img src="/images/icon/mana.png" alt="" /> Мана: <?=$info['mana']?><br />
		</td>
		</tr></table>
		<div style='padding:	6px 0px;'></div>
	<?php
		
		if (sizeof($abilityList) != 0)
		{
			
			$current	=	1;
			while	(
				$quality	=	@$abilityList[ $current ]
			)
			{
				
				if (isset ($quality))
				{
	?>
		<img src='/images/ability/<?=$current?>.<?=$quality?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
				}
				
				++$current;
				
			}
			
		}
	?>
	</div>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/equip/<?=$info['id']?>'><img src='/images/icon/equip.png' alt='' /> Снаряжение <span class='c_white'>(<?=sizeof($equip)?>/8)</span><br />
	<?php
		if (sizeof($equip) != 0)
		{
			
			if ($all_bonus != 0)
			{
	?>

	<img src='/images/icon/smith.png' alt='' /> <span class='c_white'>Бонус: <span class='c_l-green'><?=$bonus?></span> из <span class='c_l-green'><?=$all_bonus?></span></span><br />
	
	<?php
			}
			
			if ($up != 0)
			{
	?>
	<img src='/images/icon/smith.png' alt='' /> <span class='c_white'>Заточка: <span class='c_l-green'>+<?=$up?></span></span><br />
	<?php
			}
	
			if (sizeof($runeList) != 0)
			{
	?>
	<img src='/images/icon/rune.png' alt='' /> <span class='c_white'>Руны: </span>
	<?php
				
				$current	=	0;
				while	(
					$quality=	@$runeList[ $current ]	
				)
				{
					if ($quality)
					{
	?>
	
	<img src='/images/icon/quality/<?=$quality?>.png' alt='' />
	
	<?php
					}
					
					++$current;
					
				}
				
			}
			
		}
	?>
		</a></li>
	</ul>
	<div class='min-line'></div>
	<ul class='list'>
	<?php
	
	if ($info['id'] == $user['id'])
	{
		
		if ($user['level'] >= 5)
		{
			
	?>
		<li><a href='/ability'><img src='/images/icon/seif.png' alt='' /> Умения <span class='c_white'>(<?=sizeof($abilityList)?>/5)</span></a></li>
	<?php
	
		}
		
		//	Рюкзак
		$bag_items	=	@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`inv`
				WHERE
					`place`	=	0 AND `user_id`	=	%d AND `used` = 0',
			$user['id']
		)));
		
		//	Сундук
		$chest_items=	@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`inv`
				WHERE
					`place`	=	1 AND `user_id`	=	%d AND `used` = 0',
			$user['id']
		)));
		
	?>
		
		<li><a href='/inv/bag'><img src='/images/icon/bag.png' alt='' /> Сумка <span class='c_white'>(<?=$bag_items?>/20)</span></a></li>
	<?php
	
		if (chest_exists($user['id']))
		{
			
	?>
		<li><a href='/inv/chest'><img src='/images/icon/bag.png' alt='' /> Сундук <span class='c_white'>(<?=$chest_items?>/20)</span></a></li>
	<?php
		
		}
		
		$sack	=	get_user_sack(
			$user['id']
		);
		$sack_items	=	0;
		for	(
				$i	=	1;
				$i	<	10;
				++$i
			)
		{
			
			if ($sack[ $i ] != 0)
			{
				++$sack_items;
			}
			
		}

	?>
		<li><a href='/sack'><img src='/images/icon/res.png' alt='' /> Ресурсы <span class='c_white'>(<?=$sack_items?>/9)</span></a></li>
	<?php
		if ( ! premium_exists($user['id']) || ! eff_exists($info['id'], 10) && ! eff_exists($info['id'], 11) && ! eff_exists($user['id'], 12))
		{
	?>
		<li><a href='/effects/<?=$user['id']?>'><img src='/images/icon/equip.png' alt='' /> Усиления <span class='c_green'>(+)</span></a></li>
	<?php
		}
		else
		{
	?>
		<li><a href='/effects/<?=$user['id']?>'><img src='/images/icon/equip.png' alt='' /> Усиления</a></li>
	<?php
		
		}
		
		if (@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`mail`
				WHERE
					`to`	=	%d
						AND
					`read`	=	0',
			$user['id']
		))) != 0)
		{
	?>
		<li><a href='/mail'><img src='/images/icon/mail.png' alt='' /> Почта <span class='c_green'>(+)</span></a></li>
	<?php
		}
		else
		{
	?>
		<li><a href='/mail'><img src='/images/icon/mail.png' alt='' /> Почта</a></li>	
	<?php
		}
	?>
		<li><a href='/train'><img src='/images/icon/train.png' alt='' /> Тренировка</a></li>
	<?php
	
	}
	else
	{
	
	?>
		
		<li><a href='/mail/<?=$info['id']?>'><img src='/images/icon/mail.png' alt='' /> Отправить почту</a></li>
		
	<?php
	
	}
	
	?>
	</ul>
	<?php
		
		$duel		=	@mysql_fetch_assoc(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`duel`
				WHERE
					`uid`	=	%d',
			$info['id']
		)));
		
	?>
	<div class='min-line'></div>
	<div class='container'>
		<img src='/images/icon/2hit.png' alt='' /> Рейтинг дуэлей: <b><?=$duel['rating']?></b>
	</div>
	<?php

	
	if ($info['id'] == $user['id'])
	{
		
		@$progress	=	min(100, ceil(100 / ($exp / $user['exp'])));
		
	?>
	
	<div class='min-line'></div>
	<div class='container'><img src='/images/icon/exp.png' alt='' /> Опыт: <?=$user['exp']?>/<?=$exp?> <span class='c_grey'>(<?=$progress?>%)</span></div>
	
	<?php
	
	}
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
	?>
	