<?php

$title = 'Турнир Арены';

include_once '../protected/sys.php';

if (!$user OR $user['access']!='3')
{
	header("Location:/");
	exit;
}

$turnirData = $db->fetch("SELECT * FROM `arena_turnir` WHERE `id`='1'",
							null);

if (isset($_GET['on']) && $turnirData['end'] < $config['time'])
{
	$on = $db->query("UPDATE `arena_turnir` SET `end`=?,`status`='active' WHERE `id`=?",
						array(($config['time']+$turnirData['start']),$turnirData['id']));

	$usUpd = $db->query("UPDATE `users` SET `rating`='0' WHERE `rating`>0",null);

	$_SESSION['info'] = 'Турнир начат!';
	header("Location:?");
	exit;
}

if (isset($_GET['off']))
{
	$off = $db->query("UPDATE `arena_turnir` SET `end`='0',`status`='stop' WHERE `id`=?",
																	array($turnirData['id']));

	$usUpd = $db->query("UPDATE `users` SET `rating`='0' WHERE `rating`>0",null);
	$_SESSION['error'] = 'Турнир остановлен преждевременно!';
	header("Location:?");
	exit;
}

if (isset($_GET['time']))
{
	if (isset($_GET['send']))
	{
		$time = (int) abs($_POST['time']);

		$updTime = $db->query("UPDATE `arena_turnir` SET `start`=? WHERE `id`=?",
													array($time,1));

		$_SESSION['info'] = 'Время изменено!';
		header("Location:?");
		exit;
	}

	?>
	<div class ='menu'/>
		<center>
			Введите время в секундах продолжительности турнира:
			<br/>
			<form action ='?time&amp;send' method='post'/>
				<input type ='text' name ='time' class ='input' value ='<?=$turnirData['start'];?>'/>
				<br/>
				<input type='submit' class ='input' value ='Update Turnir data'/>
			</form>
		</center>
	</div>
	<?
include_once $config['root'].'/protected/footermain.php';
exit;
}



?>
<a class="menu" href ='?<?=($turnirData['end'] < $config['time'] ?  'on':'off');?>'/>
	<?=($turnirData['end'] < $config['time'] ?  'Включить':'Завершить');?>		
</a>
<a class="menu" href ="?rewards"/>
	Настройки наград турнира
</a>
<a class="menu" href ="?time"/>
	Настройки времени проведения турнира
</a>


<?php

include_once $config['root'].'/protected/footermain.php';


?>