<?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	=	[];
	$params['title']	=	'Золото';
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	switch	(
		@$_GET['act']
	)
	{
		
		default:
	?>
	<div class='container cntr c_blue'>Золото - самый ценный ресурс</div>
	<img src='/images/places/hd/trade.jpg' alt='' style='width:	100%;' />
	<ul class='list'>
	<?php
		if ($user['exchanges'] != 0)
		{
	?>
		<li><a href='/trade?act=exchange'><img src='/images/icon/arrow.png' alt='' /> Обменник золота <span class='c_green'>(+)</span></a></li>
	<?php
		}
		else
		{
	?>
		<li><a href='/trade?act=exchange'><img src='/images/icon/arrow.png' alt='' /> Обменник золота</a></li>
	<?php
		}
	?>
	</ul>
	<div class='min-line'></div>
	<ul class='hint'>
	<li>Золото - самый ценный ресурс!</li>
	</ul>
	<?php
		break;
		
		case	'exchange':
		
		$params['title']='Обменник';
		
			$gold=intval(abs(@$_POST['gold']));
		if ($gold!=0)
		{
			
			$list_errors=[];
			
			if ($gold > $user['exchanges'])
				$list_errors[]='';
			
			if ($user['silver']<($gold*200))
				$list_errors[]='';
			
			if (sizeof($list_errors)==0)
			{
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`exchanges`	=	`exchanges`	-	%d,
							`silver`	=	`silver`	-	%d,
							`gold`		=	`gold`		+	%d
						WHERE
							`id`		=	%d',
					$gold,($gold*200),$gold,
					$user['id']
				));	
				
				redirect('/trade?act=exchange');
			}
			else
			{
				
			}
			
		}
		
			$silver=intval(abs(@$_POST['silver']));
		if ($silver!=0)
		{
			
			$list_errors=[];
			
			$gold	=	ceil($silver/200);
			
			if ($silver<200)
				$list_errors[]='';
			
			if ($user['gold']<$gold)
				$list_errors[]='';
			
			if (sizeof($list_errors)==0)
			{
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`silver`	=	`silver`	+	%d,
							`gold`		=	`gold`		-	%d
						WHERE
							`id`		=	%d',
					($gold*200), $gold,
					$user['id']
				));	
				
				redirect('/trade?act=exchange');
			}
			else
			{
				
			}
			
		}
		
	?>
	
	<script type='text/javascript'>
		function calculate_silver (gold)
		{
			document.getElementById('computed_silver').value=	gold*200;
		};
		function calculate_gold	(silver)
		{
			document.getElementById('computed_gold').value	=	Math.ceil(silver/200);
		};
	</script>
	
	<div class='container cntr c_blue'>Обмен серебра на золото</div>
	<div class='min-line'></div>
	<div class='container cntr small c_grey'>Доступное золото к обмену: <?=$user['exchanges']?> из <?=$user['level']?></div>
	<div class='min-line'></div>
	<div class='container'>
		
		<form action='/trade?act=exchange' method='POST'>
		<img src='/images/icon/gold.png' alt='' />
		<input name='gold' value='0' size='4' style='text-align:	center;' oninput='calculate_silver (this.value)'/>
		<img src='/images/icon/silver.png' alt=''/>
		<input id="computed_silver" value='0' size='4' style='text-align:	center;' readonly /><br />
		<br />
		<input type='submit' value='Обменять' />
		</form>
	
	</div>
	<div class='min-line'></div>
	<div class='container cntr c_blue'>Покупка серебра</div>
	<div class='min-line'></div>
	<div class='container'>
		<form action='/trade?act=exchange' method='POST'>
		<img src='/images/icon/silver.png' alt='' />
		<input name='silver' value='0' size='4' style='text-align:	center;' oninput='calculate_gold(this.value)'/>
		<img src='/images/icon/gold.png' alt='' />
		<input id="computed_gold" value='0' size='4' style='text-align:	center;' readonly /><br />
		<br />
		<input type='submit' value='Купить' />
		</form>
	</div>
	
	<?php
		
		break;
		
	}
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
	?>
