<?php


$paginate = 1;
$load = array('f_smiles','user');
$title = (isset($_GET['smiles']) ? 'Список смайлов':'Чат');
include_once 'protected/sys.php';
?>
<?

if (!$user)
{
	header("Location:/");
	exit;
}

if($user['access']!=0){
	if(isset($_GET['del'])){
		$id = intval($_GET['del']);

		$db->query("UPDATE `chat` SET `del`='1' WHERE `id`=?",
																	array($id));
	}
}

if ($user['save'] == 0)
{
	$_SESSION['error'] ='Сохранитесь для общения в чате!';
	header("location:/");
	exit;
}


/**
* Cписок смайлов 
* @author Alex Priadko
* @package Dk-game.ru
* @return smiles list
* 17:20 19.04.15
*/


if (isset($_GET['smiles'])){
	//$smilesAll = $db->fetchAll("SELECT `id`,`mark` FROM `smiles` ",null);

	$smilesAll = array(
  array('id' => '1','name' => '','mark' => ':ag'),
  array('id' => '2','name' => '','mark' => ':beee'),
  array('id' => '3','name' => '','mark' => ':dance'),
  array('id' => '4','name' => '','mark' => ':grust'),
  array('id' => '5','name' => '','mark' => ':krutoi'),
  array('id' => '6','name' => '','mark' => ':nono'),
  array('id' => '7','name' => '','mark' => ':rzhu'),
  array('id' => '8','name' => '','mark' => ':super'),
  array('id' => '9','name' => '','mark' => ':ulibka'),
  array('id' => '10','name' => '','mark' => ':vsemprivet')
);

	echo '<div class =\'menu\'/>';

	if (is_array($smilesAll)){
		echo '<div class="list">';
		foreach ($smilesAll as $listSm)
		{
			echo '<li><img src=\'/imgData/smiles/'.$listSm['id'].'.png\' alt=\''.$listSm['mark'].'\'/>';
			echo 'пишем в чат : <b>'.$listSm['mark'].'</b></li>';
		}
		echo '</div>';
	}

	echo '</div>';

	echo '<a class=\'mbtn mb2\' href =\'/chat\'/><img src=\'/imgData/smiles/9.png\' width=\'16\' height=\'16\'/> Вернуться в чат </a>';

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



if (isset($_GET['post'])){
	$message = htmlspecialchars($_POST['text']);
	if (isset($_POST['to']))
	{
		$to = (int) abs($_POST['to']);
	}

	if ($user['access']==0 and mb_strlen($message)<5 OR mb_strlen($message)>250)
	{
		$error = 'Длина 5-250 символов!';
	}

	if (!$to)
	{
		$to = 0;
	}

	if ($error)
	{
		$_SESSION['error'] =  $error;
		header("Location:/chat");
		exit;
	}
	else
	{
		$insC = $db->query("INSERT INTO `chat` SET `text`=?,`user`=?,`time`=?,`to`=?",
							array($message,$user['id'],$config['time'],$to));
		header("Location:/chat");
		exit;
	}

}
$c = 10;
$k_post = $db->rows("SELECT `id` FROM `chat`",array());
$k_page = k_page($k_post,$c);
$page = page($k_page);
$start = $c*$page-$c;


$db->offEmulate();
$chatMessages = $db->fetchAll("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT ?,?",
							array($start,$c));

if ($user['close'] == 1)
{
	?>
	<div class ='menu'/>
		<span class ='grey2'/>
			Для Вас теперь чат и форум в режиме чтения...
		</span>
	</div>
	<?php
}else{
?>
<div class ='menu'/>
<center>
<div id="smilles" style="display:none;">
<div style="text-align: center;" class="close">Скрыть</div>
<?

	$smilesAll = array(
  array('id' => '1','name' => '','mark' => ':ag'),
  array('id' => '2','name' => '','mark' => ':beee'),
  array('id' => '3','name' => '','mark' => ':dance'),
  array('id' => '4','name' => '','mark' => ':grust'),
  array('id' => '5','name' => '','mark' => ':krutoi'),
  array('id' => '6','name' => '','mark' => ':nono'),
  array('id' => '7','name' => '','mark' => ':rzhu'),
  array('id' => '8','name' => '','mark' => ':super'),
  array('id' => '9','name' => '','mark' => ':ulibka'),
  array('id' => '10','name' => '','mark' => ':vsemprivet')
);


	foreach ($smilesAll as $sm) {
	 echo '<img style="padding:5px;" src="/imgData/smiles/'.$sm['id'].'.png" data-mark="'.$sm['mark'].'" id="smile">';
	}

?>	

</div>
<form action ='?post' method = 'POST'/>
	<?php
	if (isset($_GET['to']) && is_numeric($_GET['to']))
	{

		$tolog = $db->fetch("SELECT * FROM `users` WHERE `id`=? ",array(abs(intval($_GET['to']))));

		if ($tolog)
		{
			?>
			<input type ='hidden' name ='to' value = '<?=$tolog['id'];?>'/>
			<input type ='text' name ='text' class ='input' style = 'width:90%;' value ='<?=$tolog['login'];?>, ' required/>
			<?
		}
	}
	else
	{
		?>
		<input type ='text' name ='text' class ='input' style = 'width:90%;'  required/> 
		<img id="bt" src="/imgData/smiles/9.png">
		<?
	}
	?>

<button class="button green">Отправить</button>
<a class="button green" href = '/chat'/>Обновить</a>
</center>
</form>

</div>
<?
}




if ($k_post == 0) {
	?> Нет сообщений... <?
} else {
echo  '<div class = \'content\'/>';
$fonts = array ('#0c8c6c','#0c8c6c','#0c8c6c','#4DE137','','#15DEFF');
?>
<div class='menu'>
<?

foreach ($chatMessages as $data) {
	$userMessage = $db->fetch("SELECT * FROM `users` WHERE `id`=?",
							  array($data['user']));
	if ($data['user'] == 0){
		$userMessage['login'] = 'Система';
		$userMessage['sex'] = 1;
		$userMessage['access'] = 1;
	}

	?>
	
	<? if($user['access'] != 0){?>
	<a href='?del=<?=$data['id']?>'>[ Х ]
</a>|<a href='/admin/ban?act=ban&id=<?=$userMessage['id']?>'>[ Ban ]</a>	
	<? } ?>
	 <img src = '/imgData/static/<?=$userMessage['sex'];?>.png'/>
	<?
	if ($userMessage['id'] != 0) {
		$us=new user($db);
		$login=$us->login($userMessage['id']);	
?>
	<a href ='/user?id=<?=$userMessage['id'];?>'/><?=$login?></a>
<?	} else { 
	$userMessage['login'];
	}
	if ($user['id'] !=$data['user'] && $data['user']!=0) { ?>
	  <a href ='/chat?to=<?=$data['user'];?>'/>(>>)</a>
	<? } ?> :
	<? 
if($data['del']==1){
?>
<strike>Сообщение удалено</strike>
<?

if ($data['to'] == $user['id']) { 

?>
	  <i><font color = '<?=$fonts['5']?>'/><?=smiles(htmlspecialchars($data['text']))?></i>	
	</font> 
	<? }
} else { ?>
		<font color = '<?=$fonts[$userMessage['access']];?>'/><?=smiles(htmlspecialchars($data['text']));?>
	</font>
		<? } ?>
	<br/> </li> 
<? }
echo '</div>';
if ($k_page>1){
?>


<div class="wrapper center _pagination"><div class="btn _dark">

<?


str('?',$k_page,$page,NULL); // Вывод страниц


?>
</div></div>

<?
}

echo '</div>';
}

$countChat = $db->rows("SELECT `self`,`online` FROM `users` WHERE `self`=? and `online`>?",
														array('Чат',(time()-600)));

echo '<div class=\'menu\' style =\'padding:6px\'/> Сейчас в чате общаются  '.$countChat.' игроков </div>';



echo '<a class=\'menu\' href =\'/chat?smiles\'/><img src=\'/imgData/smiles/8.png\' width=\'16\' /> Список смайлов </a>';
?>

<script type="text/javascript">
	$('#bt').click( function() {
		$('#smilles').css('display','block');
	});

	$('.close').click( function() {
		$('#smilles').css('display','none');
	});

	$('img#smile').click( function(){
		var sm = $(this).data('mark');
		var val = $('input[name=text]').val()+' '+sm;
		$('input[name=text]').val(val);
	});

</script>

<?

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




