<?php
	
	/**
	 *	@project	Битва титанов
	 *	@author		ЭKZO
	 */
	
	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('/');
	
	$query	=	@mysql_query(sprintf(
		'SELECT
				*
			FROM
				`complects`
			WHERE
				`id`	=	%d',
		intval(@$_GET['id'])
	));
	
	if (@mysql_num_rows($query) == 0)
			redirect('/shop/');
	
	$complect	=	@mysql_fetch_assoc($query);
	
	$skill	=	get_item_skill(
		$complect['quality']
	);
	
	if ($user['skill'] < $skill)
			redirect('/shop/');
	
	
		$params	=	[];
	$params['title']	=	$complect['name'];
	$params['list_errors']	=	[];
	
	
	
	
	
	
	if (isset ($_GET['buy']))
	{
		
		$item	=	get_item(
			(int) $_GET['buy']
		);
		
		$itemList	=	[];
		
		for	(
				$i	=	1;
				$i	<	9;
				++$i
			)
		{
			$itemList[]	=	$complect[ $i ];
		}
		
		if ( ! $item || ! in_array($item['id'], $itemList))
			redirect(sprintf(
				'/complect/%d',
				$complect['id']
			));
		
		if ($user['gold'] < $item['cost'])
			$params['list_errors'][]	=	sprintf(
				'Нехватает <img src="/images/icon/gold.png" alt="" /> %d',
				$item['cost']-$user['gold']
			);
		
		if (sizeof($params['list_errors'])==0)
		{
			
			if (@mysql_num_rows(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`inv`
					WHERE
						`place`	=	0 AND `user_id`	=	%d AND `used` = 0',
				$user['id']
			))) < 20)
			{
				
				//
				put_item	(
					$user['id'],
					$item['id'],
					0,	//	bag
					$item['quality']
					);
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`gold`	=	`gold`	-	%d
						WHERE
							`id`	=	%d',
					$item['cost'],
					$user['id']
				));
				
				redirect('/inv/bag');
				
			}
			elseif (chest_exists($user['id']) && @mysql_num_rows(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`inv`
					WHERE
						`place`	=	1 AND `user_id`	=	%d AND `used` = 0',
				$user['id']
			))) < 20)
			{
				
				put_item	(
					$user['id'],
					$item['id'],
					1,	//	chest
					$item['quality']
					);
						
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`gold`	=	`gold`	-	%d
							WHERE
								`id`	=	%d',
						$item['cost'],
						$user['id']
					));
				
				redirect('/inv/chest');
				
			}	
			
		}
		
	}
	
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	$sum	=	0;
	
	for	(
			$i	=	1;
			$i	<	9;
			++$i
		)
	{
	
		$item	=	get_item(
			$complect[ $i ]
		);
		
		$sum	+=	$item['cost'];
		
	}
	
	$sum	=	ceil($sum / 2);
	
	
	if (isset ($_GET['buyAll']))
	{
		
		if ($user['gold']<$sum)
			$params['list_errors'][]	=	sprintf(
				'Нехватает <img src="/images/icon/gold.png" alt="" /> %d',
				$sum-$user['gold']
			);
		
		if (sizeof($params['list_errors'])==0)
		{
			
			if (@mysql_num_rows(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`inv`
					WHERE
						`place`	=	0 AND `user_id`	=	%d AND `used` = 0',
				$user['id']
			))) <= 12)
			{
				
				for	(
					$i	=	1;
					$i	<	9;
					++$i
				)
				{
			
					$item	=	get_item(
						$complect[ $i ]
					);
				
					put_item	(
						$user['id'],
						$item['id'],
						0,	//	bag
						$item['quality']
					);
					
				}
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`gold`	=	`gold`	-	%d
						WHERE
							`id`	=	%d',
					$sum,
					$user['id']
				));
				
				redirect('/inv/bag');
				
			}
			elseif (chest_exists($user['id']) && @mysql_num_rows(@mysql_query(sprintf(
					'SELECT
							*
						FROM
							`inv`
						WHERE
							`place`	=	1 AND `user_id`	=	%d AND `used` = 0',
				$user['id']
			))) >= 12)
			{
				
				for	(
					$i	=	1;
					$i	<	9;
					++$i
				)
				{
			
					$item	=	get_item(
						$complect[ $i ]
					);
				
					put_item	(
						$user['id'],
						$item['id'],
						0,	//	bag
						$item['quality']
					);
					
				}
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`gold`	=	`gold`	-	%d
						WHERE
							`id`	=	%d',
					$sum,
					$user['id']
				));
				
				redirect('/inv/chest');
				
			}
			
		}
		
	}
	
	?>
	
	<div class='title'><?=$complect['name']?></div>
	<div class='container cntr'>
		<img src='/manekenImage/<?=$user['sex']?>/<?=$complect[1]?>/<?=$complect[2]?>/<?=$complect[3]?>/<?=$complect[4]?>/<?=$complect[5]?>/<?=$complect[6]?>/<?=$complect[7]?>/<?=$complect[8]?>' alt='' />
	</div>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/complect/<?=$complect['id']?>/buyAll'><img src='/images/icon/arrow.png' alt='' /> Купить все вещи за <img src='/images/icon/gold.png' alt='' /> <?=$sum?> <span class='c_blue'>(50% скидка)</span></a></li>
	</ul>
	<div class='dtd-line'></div>
	<ul class='menu'>
	<?php
	
	for	(
			$i	=	1;
			$i	<	9;
			++$i
		)
	{
	
		$item	=	get_item(
			$complect[ $i ]
		);
		
		$sum	=	0;
		$usedItemSum	=	0;
		
		//	Quality
		$quality=	get_item_quality(
			$item['quality']
		);
		
		//	Bonus
		$bonus	=	get_item_bonus(
			$item['quality']
		);
		
		$sum	+=	$item['str'];
		$sum	+=	$item['dex'];
		$sum	+=	$item['life'];
		$sum	+=	$item['protection'];
		
	?>
	
		<li>
			<table>
				<tr>
				<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$complect[ $i ]?>' alt='' /></td>
				<td style='vertical-align:	top;
					padding:	0px 0px 0px 2px;'>
					<img src='/images/icon/quality/<?=$complect['quality']?>.png' alt='' /> <a href='/shop/item/<?=$complect[ $i ]?>'><?=$item['name']?></a><br />
					<span class='small quality-<?=$complect['quality']?>'><?=$quality?> [0/<?=$bonus?>]</span>
	<?php
	
		if ($user[ $item['type'] ] != 0)
		{
			
			$usedItem	=	get_user_item(
				$user['id'],
				$user[ $item['type'] ]
			);
			
			$usedItem_bonus_k=	get_item_bonus_k(
				$usedItem['quality']
			);
			
			$usedItem_rune	=	get_rune_by(
				$usedItem['type']
			);
			
			$usedItem_rune_bonus	=	get_rune_bonus(
				$usedItem['rune']
			);
			
			$usedItemSum	+=	$usedItem['str']	+	ceil($usedItem['bonus'] * $usedItem_bonus_k)	+	($usedItem['up'] * 24);
			$usedItemSum	+=	$usedItem['dex']	+	ceil($usedItem['bonus'] * $usedItem_bonus_k)	+	($usedItem['up'] * 24);
			$usedItemSum	+=	$usedItem['life']	+	ceil($usedItem['bonus'] * $usedItem_bonus_k)	+	($usedItem['up'] * 24);
			$usedItemSum	+=	$usedItem['protection']
													+	ceil($usedItem['bonus'] * $usedItem_bonus_k)	+	($usedItem['up'] * 24);	
			$usedItemSum	+=	$usedItem_rune_bonus;
			
		}
		
		$diff	=	max(0, ceil($sum	-	$usedItemSum));
			
		if ($diff !=	0)
		{
					
	?>			
				<span class='small'><span class='c_green'>+<?=$diff?></span></span>
	<?php
				
		}
		
	?>
				</td>
			</tr></table>
			<div class='cntr'>
				<a href='/complect/<?=$complect['id']?>/<?=$item['id']?>' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> <?=$item['cost']?></a><br />
			</div>
		</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
	);
	
	?>