<?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('/');

		$cid	=	isset($_GET['cid'])	?	intval($_GET['cid'])	:	0;
	if ($cid !=	0)
	{
		
		if (@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					`clans`.*
				FROM
					`clan_memb`
				LEFT JOIN
					`clans`
				ON
					`clans`.`id`	=	`clan_memb`.`clan_id`
				WHERE
					`clan_memb`.`clan_id`	=	%d AND `clan_memb`.`user_id`	=	%d',
			$cid,
			$user['id']
		)))	==	0)
		redirect('/chat/');
		
	}
	
	$params	=	[];
	$params['title']	=	'Чат';
	$params['list_errors']=	[];
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	
		$delete	=	isset($_GET['delete'])	?	intval($_GET['delete'])	:	0;
	if ($delete!=0)
	{
		
		if ($user['accesslevel'] == 0)
			redirect(sprintf(
				'/chat/%d/',
				$cid
			));
		
		if (@mysql_num_rows(@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`chat`
				WHERE
					`id`	=	%d',
			$delete
		))) != 0)
		{
			
			$post	=	@mysql_fetch_assoc($query);
			$info	=	get_user(
				$post['user_id']
			);
			
			if ($info['accesslevel'] < $user['level'] || $info['id'] == $user['id'])
			{
				
				@mysql_query(sprintf(
					'DELETE	FROM
							`chat`
						WHERE
							`id`	=	%d',
					$delete
				));
				
			}
			
		}
		
		redirect(sprintf(
			'/chat/%d/',
			$cid
		));
		
	}
	
		$deleteAll	=	isset($_GET['deleteAll'])	?	intval($_GET['deleteAll'])	:	0;
	if ($deleteAll)
	{
		
		if ($user['accesslevel'] != 2)
			redirect(sprintf(
				'/chat/%d/',
				$cid
			));
		
		@mysql_query(sprintf(
			'DELETE	FROM
					`chat`
				WHERE
					`clan_id`	=	%d',
			$cid
		));
		
		redirect(sprintf(
			'/chat/%d/',
			$cid
		));
		
	}
	
		$_GET['to']	=	isset($_GET['to'])	?	intval($_GET['to'])	:	0;
	if ($_GET['to'] != 0)
	{
		
		$to	=	get_user(
			$_GET['to']
		);
		
		if ( ! $to || $to['id'] == $user['id'])
			redirect(sprintf(
				'/chat/%d/',
				$cid
			));
		
	}
	
	if (isset ($_POST['message']//,
		//$_POST['token']
	))
	{
		
			$message	=	input($_POST['message']);
		if ($message)
		{
			
			
			if ($user['saved']==0)
				$params['list_errors'][]	=	'Писать в чат могут только сохраненные игроки';
			
			if (@mysql_num_rows(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`chat`
					WHERE
						`user_id`	=	%d AND `time` > %d',
				$user['id'],
				time()-5
			))) != 0)
				$params['list_errors'][]	=	'Вы слишком часто пишите';
			
			if (sizeof($params['list_errors'])	==	0)
			{
				
				if ($_GET['to'] != 0)	{
					$message	=	strtr($message, [
							$to['login'] . ', '	=>	''
					]);
				}
				
				@mysql_query(sprintf(
					'INSERT INTO 
							`chat`
							(
								`clan_id`,
								`user_id`,
								`to`,
								`message`,
								`time`
							)
							VALUES
							(
								%d,
								%d,
								%d,
								"%s",
								%d
							)',
					$cid,
					$user['id'],
					$_GET['to'],
					$message,
					time()
				));
				
				redirect(sprintf(
					'/chat/%d/',
					$cid
				));
				
			}
		
		}
		
	}
	
	?>
	<div class='container'>
		<form action='/chat/<?=$cid?>/to/<?=$_GET['to']?>/' method='post'>
			<input name='message' style='width:	100%;' value='<?=($_GET['to']	!= 0	?	sprintf('%s, ', $to['login'])	:	'')?>' /><br />
			<input type='submit' value='Отправить' />
			<a href='/chat/<?=$cid?>' class='c_grey'>Обновить</a>
		</form>
	</div>
	<div class='min-line'></div>
	<ul class='menu'>
		<li>
	<?php
	
	$sql	=	sprintf(
		'SELECT
				*
			FROM
				`chat`
			WHERE
				`clan_id`	=	%d',
		$cid
	);
	
	$items	=	@mysql_num_rows(@mysql_query($sql));
	$items_per_page	=	15;
	$pages	=	c_page($items, $items_per_page);
	$page	=	page($pages);
	
	$sql	.=	'
			ORDER BY
				`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	(
			$post	=	@mysql_fetch_assoc($query)
		)
		{
			
			$info	=	get_user(
				$post['user_id']
			);
		
		
	?>
		<?=player($post['user_id'])?>
	<?php
		
			if ($post['user_id'] != $user['id'])
			{
	?>
	<a href='/chat/<?=$cid?>/to/<?=$post['user_id']?>/'>(&#187;)</a>
	<?php
			}
	?>
	:
	<?php
	
			if ($post['to'] != 0)
			{
				$to	=	get_user(
					$post['to']
				);
				
				if ($post['to'] == $user['id'])
				{
					
					
	?>
	<span class='c_l-green'><?=$to['login']?></span>, 
	<?php	
				}
				else
				{
					
	?>
	<?=$to['login']?>, 
	<?php
				}
			}
	
			switch ($info['accesslevel'])
			{
				
				default:
				case	0:
	?>
	<?=bb($post['message'])?>
	<?php
				break;
				
				case	1:
	?>
	<span class='c_orange'>
		<?=bb($post['message'])?>
	</span>
	<?php
				break;
				
				case	2:
	?>
	<span class='c_blue'>
		<?=bb($post['message'])?>
	</span>
	<?php
				break;
				
			}
			
			if ($user['accesslevel'] != 0)
			{
				if ($info['accesslevel'] < $user['level'] || $post['user_id'] == $user['id'])
				{
	?>
	<a href='/chat/<?=$cid?>/delete/<?=$post['id']?>/'>[x]</a>
	<?php
				}
			}
	?>
	<br />
	<?php
		}
		
		if ($pages > 1)
		{
	
	?>
		<li>
	<?php
			pagenation(
				sprintf(
					'/chat/%d/',
					$cid
				),
				$pages,
				$page
			);
	?>
		</li>
	<?php
		}
	
	}
	else
	{
	
	?>
	<div class='cntr c_grey'>Сообщений нет</div>
	<?php
	
	}
	
	?>
		</li>
		<li><img src='/images/icon/user.png' alt='' /> <a href='/moderators/'>Модераторы</a></li>
	<?php
	if ($user['accesslevel']==2)
	{
	?>
		<li><img src='/images/icon/arrow.png' alt='' /> <a href='/chat/<?=$cid?>/deleteAll/'>Удалить все сообщения</a></li>
	<?php
	}
	?>
	</ul>
	
	<?php
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		(array) $params
	);
	
	?>