<?php

$title = 'Турнир Арены';
include_once 'protected/sys.php';

$turnir = $db->fetch("SELECT * FROM `arena_turnir` WHERE `id`=?",
						array(1));


if ($_GET['show'] == 'last')
{


	?>

		<div class="menu">
		<div class="nl">
		<div class="nr cntr lyell lh1 p5 sh">
			Победители прошлого турнира!
		</div>
		</div>
		</div>
	<?php

	

	for ($i = 0; $i < 3; $i++)
	{
		$us = $db->fetch("SELECT `id`,`login`,`sex` FROM `users`
								WHERE `id`=?",
								array ($turnir['place_'.$i]));

		?>
		<a class="menu"  href ="/user?id=<?=$us['id'];?>"/> 
			<img src='/imgData/static/<?=($us['sex'] == 0 ? 0:1);?>.png'/>
			<?=$us['login'];?>
		</a>
		<?php
	}

	?>
	<a class="menu"  href ="/arena_turnir"/> 
		<img src='/imgData/static/best.png'/> 
		Назад
	</a>

	<?php

	include_once 'protected/footermain.php';
	exit;
}

if ($turnir['status'] == 'active')
{

	if ($_GET['show'] == 'reward')
	{
		$turnirPlace = explode(';', $turnir['reward']);
		$turnirPlaceImpl = implode(',', $turnirPlace);
		$turnirReward = explode(',', $turnirPlaceImpl);
		$Reward = array_chunk($turnirReward, 3);

		?>
		<div class="hs">
		<div class="nl">
		<div class="nr cntr lyell lh1 p5 sh">
			Награды за турнир
		</div>
		</div>
		</div>
		<?

		for ($i = 0; $i<3; $i++)
		{
			$place = $i+1;
			?>
			<div class ='menu'/>
				<?=$place;?> место.
				<br/>
				+<?=$Reward[$i][0];?><img src='/imgData/static/crystal.png'/><br/>
				+<?=$Reward[$i][1];?><img src='/imgData/static/gold.png'/><br/>
				+<?=$Reward[$i][2];?><img src='/imgData/static/silver.png'/><br/>
			</div>
			<?php
		}

		?>

		<a class="menu"  href ="/arena_turnir"/> 
		<img src='/imgData/static/best.png'/> 
			Назад
		</a>

		<?php
		include_once 'protected/footermain.php';
		exit;
	}

	?>
	<div class ='menu'/>
		<center>
			Список лидеров турнира
		</center>
	</div>

	<?php

	$TTime = $turnir['end']-time();
	$THour = $TTime/3600%60;
	$TMin = $TTime/60%60;
	$TSec = $TTime%60;

	?>

	<div class="menu">
	<div class="nl">
	<div class="nr cntr lyell lh1 p5 sh">
		Турнир закончится через: 
		<?=$THour;?> часов
		<?=$TMin;?> минут
		<?=$TSec;?> секунд.
	</div>
	</div>
	</div>	

	<div class  = 'menu'/>

	<?php

	$liders = $db->fetchAll("SELECT `id`,`login`,`rating`,`sex` FROM `users` ORDER BY `rating` DESC LIMIT 10");

	if (is_array($liders))
	{
		$i = 0;
		foreach ($liders as $data) {
			$i++;


			?>
			<?=$i;?>. <img src ='/imgData/static/<?=($data['sex'] == 0 ? 0:1);?>.png'/>
			<?=($i <4 ? '<i>':null);?>
			<a href ='/user?id=<?=$data['id'];?>'/>
				<?=$data['login'];?>
			</a>
			<span style='float:right;margin-left:5px;'/>
				<?=$data['rating'];?><img src ='/imgData/static/arena_rating.png'/>
			</span>
			<?=($i <4 ? '</i>':null);?>
			<div class="hr_arr mt2 mb2 mlr10">
				<div class="alf">
					<div class="art">
						<div class="acn">
						</div>
					</div>
				</div>
			</div>
			<?php
		}
	}

	?>
	</div>
	<a class="menu"  href ="?show=reward"/> 
		<img src='/imgData/static/best.png'/> 
			Награды
	</a>

	<?
} elseif ($turnir['status'] == 'stop') {
	?>
	<div class ='content'/>
		<center>
			<span class= 'menu'/>
				На данный момент турнит еще не объявлен!
			</span>
		</center>
	</div>
	<?
}

?>
<a class="menu"  href ="?show=last"/> 
	<img src='/imgData/static/arena_grey.png'/> 
		Победители прошлого турнира
</a>
<?php



include_once 'protected/footermain.php';