<?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';
	
	?>
	<div class='title'>Онлайн</div>
	<div class='container'>
		<a href='/online'>Все</a> | <a href='/online?sort=1'>Без клана</a> | <a href='/online/?act=search'>Поиск</a>
	</div>
	<div class='l'></div>
	<?php
	
	switch (@$_GET['act'])
	{
		
		default:
	?>
	<div class='container'>
	<?php
			$_GET['sort']	=	intval(@$_GET['sort']);
	switch ($_GET['sort'])
	{
		
		default:
		case	0:
		$sql	=	sprintf(
			'SELECT
					*
				FROM
					`users`
				WHERE
					`lastActive`	>=	%d',
			time()	-	60	*	15
		);
		break;
		
		case	1:
		$sql	=	sprintf(
			'SELECT
					`users`.*
				FROM
					`users`
				LEFT JOIN
					`clan_memb`
				ON
					`clan_memb`.`user_id`	=	`users`.`id`
				WHERE
					`users`.`lastActive`	>=	%d AND `clan_memb`.`id`	IS NULL',
			time()	-	60	*	15
		);
		break;
		
	}
	
	$items	=	@mysql_num_rows(@mysql_query($sql));
	
	$items_per_page	=	10;
	
	$pages	=	c_page($items, $items_per_page);
	$page	=	page($pages);
	
	$sql	.=	'
			ORDER BY
				`lastActive`	DESC,
				`level`	DESC,
				`id`	DESC';
	
	$at		=	($page	-	1)	*	$items_per_page;
	$sql	.=	sprintf(
		'
			LIMIT
				%d, %d',
		$at,
		$items_per_page
	);
	
	$query	=	@mysql_query($sql);
	if ($items != 0)
	{
		while	(
			$info	=	@mysql_fetch_assoc($query)
		)
		{
	?>
		<?=player($info['id'])?> <img src='/images/icon/level.png' alt='' /> <?=$info['level']?> ур<br />
	<?php
		}
		
		if ($pages > 1)
		{
			
			pagenation	(

				sprintf(
					'/online?sort=%s&amp;page=',
					$_GET['sort']
				),
				$pages,
				$page
				
			);
			
		}
		
	}
	else
	{
	?>
		<div class='cntr c_grey'>
		Никого нет
		</div>
	<?php
	}
	?>
	</div>
	<?php
		break;
		
		case	'search':
	
		if (isset($_POST['name'])):
			
			$errorList	=	[];
			
			$name	=	input($_POST['name']);
			if ( ! $name)
				$errorList[]=	'';
			
			$query	=	@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`users`
					WHERE
						`login`	=	"%s"',
				$name
			));
			
			if (@mysql_num_rows($query)==0)
				$errorList[]=	'';
			
			if (sizeof($errorList) == 0):
				
				$info	=	@mysql_fetch_assoc($query);
				
				redirect(sprintf(
					'/user/%d',
					$info['id']
				));
				
				else:
				
				//	
				
			endif;
			
		endif;
		
	?>
	<div class='container'>
	
		<form action='/online/?act=search' method='POST'>
		
			Имя игрока:<br />
			<input name='name' /><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
	);
	
	?>
