<?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']
		);
		
		if ( ! $info)
			redirect('/');
		
	}
	else
	{
		
		$info	=	$user;
		
	}
	
	
	$params['title']	=	$info['id'] == $user['id'] ? 'Снаряжение' : sprintf(
		'Снаряжение %s',
		$info['login']
	);
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	?>
	
	<div class='title'>Снаряжение</div>
	<ul class='menu'>
	<?php
	
	for	(
		$i	=	1;
		$i	<	9;
		++$i
	)
	{
		
		//	
		$place	=	get_item_place(
			$i
		);
		
	?>
		<li>
	<?php
		
		if ($info[ $i ] != 0)
		{
			
			//	
			$item	=	get_user_item(
				$info['id'],
				$info[ $i ]
			);
			//	Quality
			$quality=	get_item_quality(
				$item['quality']
			);
			$bonus_k=	get_item_bonus_k(
				$item['quality']
			);
			//	Bonus
			$bonus	=	get_item_bonus(
				$item['quality']
			);
			
			$rune_bonus	=	get_rune_bonus(
				$item['rune']
			);
			
			$sum	=	0;
			$usedItemSum	=	0;
			
			$sum	+=	$item['str']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
			$sum	+=	$item['dex']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
			$sum	+=	$item['life']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
			$sum	+=	$item['protection']
										+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);	
			
			$sum	+=	$rune_bonus;
			
			if ($user[ $item['type'] ] != 0)
			{
			
				$usedItem	=	get_user_item(
					$user['id'],
					$user[ $item['type'] ]
				);
				
				$bonus_k=	get_item_bonus_k(
					$usedItem['quality']
				);
				
				$rune_bonus	=	get_rune_bonus(
					$item['rune']
				);
				
				$usedItemSum	+=	$usedItem['str']	+	ceil($usedItem['bonus'] * $bonus_k)	+	($usedItem['up'] * 24);
				$usedItemSum	+=	$usedItem['dex']	+	ceil($usedItem['bonus'] * $bonus_k)	+	($usedItem['up'] * 24);
				$usedItemSum	+=	$usedItem['life']	+	ceil($usedItem['bonus'] * $bonus_k)	+	($usedItem['up'] * 24);
				$usedItemSum	+=	$usedItem['protection']
														+	ceil($usedItem['bonus'] * $bonus_k)	+	($usedItem['up'] * 24);	
				
				$usedItemSum	+=	$rune_bonus;
				
			}
			
	?>
			<table>
				<tr>
				<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
				<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$info['id']?>/<?=$item['id']?>'><?=$item['name']?></a><?=($item['up'] != 0	?	' <span class="c_l-green">+' . $item['up'] . '</span>'	:	''	)?><br />
					<span class='small quality-<?=$item['quality']?>'> <?=$quality?> [<?=$item['bonus']?>/<?=$bonus?>]</span>
	<?php
		$diff	=	max(0, ceil($sum	-	$usedItemSum));
			
		if ($diff !=	0)
		{
					
	?>			
				<span class='small'><span class='c_green'>+<?=$diff?></span></span>
	<?php
				
		}
	?>					
					<br />
	<?php
		
		if ($item['rune'] != 0)
		{
	
	?>
	<div style='padding:	12px 0px;' class='small'>
		<img src='/images/icon/quality/<?=$item['rune']?>.png' alt='' /> <span class='c_green'>+<?=$rune_bonus?></span>
	<?php
	
			$rune		=	get_rune_by(
				$item['type']
			);
			
			switch	(
						$rune
					)
			{
				
				case	'str':
	?>
		силы
	<?php
				break;
				
				case	'dex':
	?>
		ловкости
	<?php
				break;
				
				case	'protection':
	?>
		брони
	<?php
				break;
				
				case	'life':
	?>
		жизни
	<?php
				break;
				
			}
			
		}
		
	?>
				</td>
			</tr></table>
		
	
	<?php
	
		}
		else
		{
	
	?>
		
			<table>
				<tr>
				<td style='vertical-align:	top;'><img src='/images/items/null.png' alt='' /></td>
				<td style='vertical-align:	middle;
					padding:	0px 0px 0px 2px;'><?=$place?></td>
			</tr></table>
			
	<?php
		
		}
		
	?>
		</li>
	<?php
	}
	?>
	</ul>
	<?php
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
	?>
	