<?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	=	[];
	
	$place		=	intval(@$_GET['place']);
	if ($place	!=	0)
	{
		
		if ( ! chest_exists($user['id']))
				redirect('/inv/bag');
		
	}
	
	$inv	=	get_user_inv(
		$user['id']
	);
	
	$params['title']	=	$place	==	0	?	'Рюкзак'	:	'Сундук';
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	//	Продажа
		$sell	=	intval(@$_GET['sell']);
	if	(
		$sell != 0
	)
	{
		
		$query	=	@mysql_query(sprintf(
			'SELECT	
					*
				FROM
					`inv`
				WHERE
					`used`	=	0 AND `user_id`	=	%d AND `id`	=	%d',
			$user['id'],
			$sell
		));
		
		if (@mysql_num_rows($query) != 0)
		{
			
			$item	=	@mysql_fetch_assoc($query);
			
			@mysql_query(sprintf(
				'DELETE FROM
						`inv`
					WHERE
						`id`	=	%d',
				$sell
			));
			@mysql_query(sprintf(
				'UPDATE
						`users`
					SET
						`silver`	=	`silver`	+	1
					WHERE
						`id`	=	%d',
				$user['id']
			));
			
			redirect(sprintf(
				'/inv/%s',
				$item['place']	==	0	?	'bag'	:	'chest'
			));
			
		}
		
	}
	
		$use	=	intval(@$_GET['use']);
	if ($use	!= 0)
	{
		
		$query	=	@mysql_query(sprintf(
			'SELECT	
					`inv`.*,
					`shop`.`type`,
					`shop`.`str`,
					`shop`.`dex`,
					`shop`.`life`,
					`shop`.`protection`
				FROM
					`inv`
				LEFT JOIN
					`shop`
				ON
					`shop`.`id`	=	`inv`.`shop_id`
				WHERE
					`inv`.`used`	=	0 AND `inv`.`user_id`	=	%d AND `inv`.`id`	=	%d',
			$user['id'],
			$use
		));
		
		if (@mysql_num_rows($query) != 0)
		{
			
			$item	=	@mysql_fetch_assoc($query);
			
			//	Снаряжение
			$equipListPlace	=	[1,2,3,4,5,6,7,8];
			
			if (in_array($item['type'], $equipListPlace))
			{

				//	Мастерство
				$skill	=	get_item_skill(
					$item['quality']
				);
				
				//	Коэфицент бонуса
				$bonus_k	=	get_item_bonus_k(
					$item['quality']
				);
				
				$rune		=	get_rune_by(
					$item['type']
				);
				
				$rune_bonus	=	get_rune_bonus(
					$item['rune']
				);
				
				$str	=	$item['str']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
				$dex	=	$item['dex']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
				$protection
						=	$item['protection']
											+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
				$life	=	$item['life']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
				
				$$rune	+=	$rune_bonus;
				
				if ($user['skill'] >= $skill)
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`str`	=	`str`	+	%d,
								`dex`	=	`dex`	+	%d,
								`life`	=	`life`	+	%d,
								`protection`	=	`protection`	+	%d,
								`%d`	=	%d
							WHERE
								`id`	=	%d',
						$str,
						$dex,
						$life,
						$protection,
						$item['type'],
						$item['id'],
						$user['id']
					));
					
					@mysql_query(sprintf(
						'UPDATE
								`inv`
							SET
								`used`	=	1
							WHERE
								`id`	=	%d',
						$item['id']
					));
					
					redirect('/equip');
					
				}
				
			}
			//	Элики
			elseif ($item['type'] 	==	9)
			{
				
				/*
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`str`	=	`str`	+	%d,
							`dex`	=	`dex`	+	%d,
							`protection`	=	`protection`	+	%d,
							`life`	=	`life`	+	%d
						WHERE
							`id`	=	%d',
					$item['str'],
					$item['dex'],
					$item['protection'],
					$item['life'],
					$user['id']
				));
				
				@mysql_query(sprintf(
					'DELETE FROM
							`inv`
						WHERE
							`id`	=	%d',
					$item['id']
				));
				*/
				
			}
			
		}
		
		redirect(sprintf(
			'/inv/%s',
			$place	==	0	?	'bag'	:	'chest'
		));
		
	}
	
	
		$move	=	intval(@$_GET['move']);
	if ($move != 0)
	{
		
		if (@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`inv`
				WHERE
					`place`	=	%d
						AND
					`used`	=	0
						AND
					`user_id`
							=	%d',
			$place,
			$user['id']
		)))<20)
		{
			
			$query	=	@mysql_query(sprintf(
				'SELECT	
						`inv`.*,
						`shop`.`type`,
						`shop`.`str`,
						`shop`.`dex`,
						`shop`.`life`,
						`shop`.`protection`
					FROM
						`inv`
					LEFT JOIN
						`shop`
					ON
						`shop`.`id`	=	`inv`.`shop_id`
					WHERE
						`inv`.`user_id`	=	%d AND `inv`.`id`	=	%d',
				$user['id'],
				$move
			));
			
			if (@mysql_num_rows($query) != 0)
			{
				
				$item	=	@mysql_fetch_assoc($query);
				
				$equip	=	[];
				for	(
					$i	=	1;
					$i	<	9;
					++$i
				)
				{
					
					$equip[]=	$user[ $i ];
					
				}
				
				if (in_array ($item['id'], $equip))
				{
					
					$bonus_k	=	get_item_bonus_k(
						$item['quality']
					);
					
					$rune		=	get_rune_by(
						$item['type']
					);
					
					$rune_bonus	=	get_rune_bonus(
						$item['rune']
					);
					
					$str	=	$item['str']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
					$dex	=	$item['dex']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
					$protection
							=	$item['protection']
												+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
					$life	=	$item['life']	+	ceil($item['bonus'] * $bonus_k)	+	($item['up'] * 24);
					
					$$rune	+=	$rune_bonus;
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`str`	=	`str`	-	%d,
								`dex`	=	`dex`	-	%d,
								`life`	=	`life`	-	%d,
								`protection`	=	`protection`	-	%d,
								`%d`	=	0
							WHERE
								`id`	=	%d',
						$str,
						$dex,
						$life,
						$protection,
						$item['type'],
						$user['id']
					));
					
					@mysql_query(sprintf(
						'UPDATE
								`inv`
							SET
								`used`	=	0,
								`place`	=	%d
							WHERE
								`id`	=	%d',
						$place,
						$item['id']
					));
					
					redirect('/equip');
				}
				
				@mysql_query(sprintf(
					'UPDATE
							`inv`
						SET
							`used`	=	0,
							`place`	=	%d
						WHERE
							`id`	=	%d',
					$place,
					$item['id']
				));
				
				redirect(sprintf(
					'/inv/%s',
					$place	==	0	?	'chest'	:	'bag'
				));
				
			}
			
		}
		
	}
	
		$sellQuality	=	intval(@$_GET['sellQuality']);
	if ($sellQuality != 0)
	{
		
		@mysql_query(sprintf(
			'UPDATE
					`user_inv`
				SET
					`sell_quality`	=	%d
				WHERE
					`user_id`		=	%d',
			$sellQuality,
			$user['id']
		));
		
		redirect(sprintf(
			'/inv/%s',
			$place	==	0	?	'bag'	:	'chest'
		));
		
	}
	
	if (isset ($_GET['sellAll']))
	{
		
		$query	=	@mysql_query(sprintf(
			'SELECT
				`inv`.*,
				`shop`.`type`,
				`shop`.`name`,
				`shop`.`str`,
				`shop`.`dex`,
				`shop`.`life`,
				`shop`.`protection`
			FROM
				`inv`
			LEFT JOIN
				`shop`
			ON
				`shop`.`id`	=	`inv`.`shop_id`
			WHERE
				`inv`.`place`	=	%d AND `inv`.`user_id`	=	%d AND `inv`.`used` = 0 AND `inv`.`quality` <=	%d AND `shop`.`type` != 9',
			$place,
			$user['id'],
			$inv['sell_quality']
		));
		
		if (@mysql_num_rows($query) != 0)
		{
			
			while	(
				$item	=	@mysql_fetch_assoc($query)
			)
			{
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`silver`=	`silver`	+	1
						WHERE
							`id`	=	%d',
					$user['id']
				));
				
				@mysql_query(sprintf(
					'DELETE	FROM
							`inv`
						WHERE
							`id`	=	%d',
					$item['id']
				));
				
			}
			
		}
		
		redirect(sprintf(
			'/inv/%s',
			$place	==	0 ? 'bag' : 'chest'
		));
		
	}
	
	?>
	
	<ul class='list'>
	<?php
		if($place	==	0)
		{
			if ( ! chest_exists($user['id']))
			{
				
				if (isset ($_GET['chest']))
				{
					
					if ($user['gold']	>=	100)
					{
						
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`gold`	=	`gold`	-	100
								WHERE
									`id`	=	%d',
							$user['id']
						));
						
						@mysql_query(sprintf(
							'INSERT INTO
									`user_chest`
									(
										`user_id`
									)
									VALUES
									(
										%d
									)',
							$user['id']
						));
						
						redirect('/inv/chest');
						
					}
					
				}
				
	?>
		<li><a href='/inv/bag/?chest'><img src='/images/icon/bag.png' alt='' /> Купить сундук за <img src='/images/icon/gold.png' alt='' /> 100 золота</a></li>
	<?php
	
			}
			else
			{
	?>
		<li><a href='/inv/chest'><img src='/images/icon/bag.png' alt='' /> Открыть сундук</a></li>
	<?php
			}
		}
		else
		{
	
	?>
		<li><a href='/inv/bag'><img src='/images/icon/bag.png' alt='' /> Открыть рюкзак</a></li>
	<?php
	
		}
	?>
	</ul>
	<div class='min-line'></div>
	
	<?php
	
	$query	=	@mysql_query(sprintf(
		'SELECT
				`inv`.*,
				`shop`.`type`,
				`shop`.`name`,
				`shop`.`str`,
				`shop`.`dex`,
				`shop`.`life`,
				`shop`.`protection`
			FROM
				`inv`
			LEFT JOIN
				`shop`
			ON
				`shop`.`id`	=	`inv`.`shop_id`
			WHERE
				`inv`.`place`	=	%d AND `inv`.`user_id`	=	%d AND `inv`.`used` = 0
			ORDER BY
				`id`',
		$place,
		$user['id']
	));
	
	if (@mysql_num_rows($query) != 0)
	{
		
		$qualityList=	[];
	?>
	
	<ul class='menu'>
	
	<?php
		
		while	(
			$item	=	@mysql_fetch_assoc($query)
		)
		{
			
			switch($item['type'])
			{
				
				//	Снаряжение
				case	1:
				case	2:
				case	3:
				case	4:
				case	5:
				case	6:
				case	7:
				case	8:
				
				if ( ! in_array(
					$item['quality'],
					$qualityList
				))
					$qualityList[]	=	$item['quality'];
				
				//	
				$quality=	get_item_quality(
					$item['quality']
				);
				
				//	
				$bonus	=	get_item_bonus(
					$item['quality']
				);
				
				//	
				$bonus_k=	get_item_bonus_k(
					$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;
					
				}
				
	?>
		
		<li>
		<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/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a> <?=($item['up'] != 0	?	'<span class="c_l-green">+' . $item['up'] . '</span>'	:	''	)?><br />
				<div style='padding:	0px 16px;'>
				<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 />
				</div>
	<?php
		
				if ($item['rune'] != 0)
				{
	
	?>
	<div style='padding:	12px 16px;' 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;
						
					}
	?>
	</div>
	<?php
				}
		
	?>
			</td>
			</tr></table>
			<div class='cntr'>
	<?php
	
				if ($user[ $item['type'] ]	==	0)
				{
			
	?>
		<a href='/inv/use/<?=$item['id']?>' class='btn'>Надеть</a>	
	<?php
			
				}
	?>
	
<a href='/inv/sell/<?=$item['id']?>' class='btn'>Продать за <img src='/images/icon/silver.png' alt='' /> 1 серебро</a>
	<?php
				if (@mysql_num_rows(@mysql_query(sprintf(
					'SELECT
							*
						FROM
							`user_chest`
						WHERE
							`user_id`	=	%d',
					$user['id']
				))) != 0)
				{
					
	?>
		
		<a href='/inv/move/<?=($place	==	0	?	1	:	0)?>/<?=$item['id']?>' class='btn'>В <?=($place	==	0	?	'сундук'	:	'рюкзак')?></a>
		
	<?php
			
				}
		
	?>
			</div>
		</li>
	<?php
			
			
				break;
			
				//	Элексиры
				case	9:
	?>
		<li>
			<table>
				<tr>
				<td style='vertical-align:	top;'><img src='/images/alchemy/default.png' alt='' /></td>
				<td style='vertical-align:	top;'>
					<img src='/images/icon/wiz.png' alt='' /> <?=$item['name']?>
				</td>
			</tr></table>
			<div class='cntr'>
				<a href='/inv/use/<?=$item['id']?>' class='btn'>Использовать</a>
			</div>
		</li>
	<?php
				break;
			
			}
		
		}
		
	?>
	</ul>
	<div class='min-line'></div>
	<ul class='list'><li><a href='/inv/<?=$place?>/sellAll'><img src='/images/icon/arrow.png' alt='' /> Продать все вещи</a></li></ul>
	<div class='min-line'></div>
	<ul class='menu'>
		<li>
			Продать: 
	<?php
		
		//	
		array_multisort($qualityList, SORT_DESC);
		
		$quality	=	$qualityList[0];
		for	(
				$i	=	$quality;
				$i	>	0;
				--$i
			)
		{
			
			$name	=	get_item_quality(
				$i
			);
			$name	=	mb_substr($name, 0, 3);
			
			if ($inv['sell_quality'] == $i)
			{
	?>
	<img src='/images/icon/quality/<?=$i?>.png' alt='' /> <?=$name?>
	<?php	
			}
			else
			{
	?>
	<img src='/images/icon/quality/<?=$i?>.png' alt='' /> <a href='/inv/<?=$place?>/sellQuality/<?=$i?>'><?=$name?></a>
	<?php
			
			}
			
			if ($i > 1)
			{
	?>
		/
	<?php
			}
			
		}
		
		$qN		=	get_item_quality(
			$inv['sell_quality']
		);
		
	?>
		</li>
		<li class='small c_grey'>Будут продаваться все вещи с качеством <img src='/images/icon/quality/<?=$inv['sell_quality']?>.png' alt='' /> <?=$qN?> и ниже</li>
	</ul>
	
	<!--
	<div class='min-line'></div>
	<ul class='list'>
		<li></li>
	</ul>
	-->
	<?php
	
	}
	else
	{
	
	?>
	
	<div class='container cntr c_grey'><?=($place	==	0	?	'Рюкзак'	:	'Сундук')?> пуст</div>
	
	<?php
	
	}
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
?>