<?php
require_once 'core/sys.php';
include 'core/func_smiles.php';
reg();
head('Почта');
if(!empty($_GET['ank'])){
$ank_id = int($_GET['ank']);
$ank = mysql_fetch_assoc(mysql_query("SELECT `login`,`id` FROM `users` WHERE `id`='".$ank_id."'"));
	if(!$ank){
	header("Location: mail.php");
	exit;
	}
	if($ank_id==$user['id']){
	header("Location: mail.php");
	exit;
	}
	if(isset($_GET['ignore'])){
	$test_ign =  mysql_result(mysql_query("SELECT COUNT(id) FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."'"),0);
	if($test_ign == 0){
	mysql_query("INSERT INTO `mail_ignore` SET `user_id`='".$user['id']."',`ignore_id`='".$ank_id."'");
	echo '<div class="row"><b>'.$ank['login'].'</b> успешно добавлен в игнор-лист!</div>';
	}else{
	echo '<div id="error">Этот пользователь уже в игноре!</div>';
	}
	echo '<div class="name"><a href="mail.php">Почта</a></div>';
	foot();
	exit;
	}
	if(isset($_GET['ign_del'])){
	$test_del = mysql_fetch_assoc(mysql_query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."' LIMIT 1"));
		if(!$test_del){
		header("Location: mail.php");
		exit;
		}
		echo '<div id="ok"><b>'.$ank['login'].'</b> убран из игнор листа.</div>';
		mysql_query("DELETE FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."'");
		echo '<div class="name"><a href="mail.php">Почта</a></div>';
	foot();
	exit;	
	}
	
$cont = mysql_query("SELECT * FROM `mail_contact` WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."' LIMIT 1");
$cont_to = mysql_query("SELECT * FROM `mail_contact` WHERE `user_id`='".$ank_id."' AND `contact_id`='".$user['id']."' LIMIT 1");
if(mysql_num_rows($cont)==0){
mysql_query("INSERT INTO `mail_contact` SET `user_id`='".$user['id']."',`contact_id`='".$ank_id."',`time`='".time()."'");
}
if(mysql_num_rows($cont_to)==0){
mysql_query("INSERT INTO `mail_contact` SET `user_id`='".$ank_id."',`contact_id`='".$user['id']."',`time`='".time()."'");
}
echo '<div class="name">Диалог с '.$ank['login'].'</div><div class="row"><form action="mail.php?ank='.$ank_id.'" method="post">
<textarea name="text"></textarea><br>
<input type="submit" value="Отправить"><a style="position: relative;right:-20px;" href="presents.php?send&ank='.$ank_id.'"><img src="img/presents/gift.png"></a></form></div>';
/*Тест на игнор*/
$test_igno = mysql_fetch_assoc(mysql_query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."' LIMIT 1"));
		echo '<div class="name">';
		if(!$test_igno){
		echo  '<a href="mail.php?ank='.$ank['id'].'&amp;ignore">В игнор</a>';
		}else{
		echo  '<a href="mail.php?ank='.$ank['id'].'&amp;ign_del">Убрать из игнора</a>';
		}
		echo '|<a href="smiles.php">Смайлы</a></div>';
if(!isset($_POST['text']) or empty($_POST['text'])){
/* Обновляем новые месаги*/
mysql_query("UPDATE `mail_contact` SET `new_mess`=0 WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."'");
/*--------*/
$total = mysql_result(mysql_query("SELECT COUNT(id) FROM `mail` WHERE `from_id`='".$user['id']."' AND `to_id`='".$ank_id."' OR `to_id`='".$user['id']."' AND `from_id`='".$ank_id."'"),0);
if($total == 0){
echo '<div class="row">Напишите что-нибудь :)</div>';
foot();
exit;
}
$mail_q = mysql_query("SELECT * FROM `mail` WHERE `from_id`='".$user['id']."' AND `to_id`='".$ank_id."' OR `to_id`='".$user['id']."' AND `from_id`='".$ank_id."' ORDER BY `time` DESC LIMIT ".$page.",".$set['str']." ");
	$new_mess ='';
	while($mail = mysql_fetch_assoc($mail_q)){
	$us = mysql_fetch_assoc(mysql_query("SELECT `id`,`login`,`admins` FROM `users` WHERE `id`='".$mail['from_id']."'"));
		/*if($mail['to_id']==$user['id'] AND $mail['read']==1){
		mysql_query("UPDATE `mail` SET `read`=0 WHERE `id`='".$mail['id']."'");
		}*/
		
		/*if($mail['read']==1){
		$new_mess = '<font color="#88b775;">(новое)</font>';
		}elseif($mail['read']==1 and $user['id']==$mail['from_id']){
		$new_mess = '<font color="#88b775;">(не прочитано)</font>';
		}*/
		$nick = '<a href="pers.php?ank='.$us['id'].'">'.$us['login'].'</a>';
			if($us['admins']>0){
	
			$nick = '<a href="pers.php?ank='.$us['id'].'"><span style="color: #0f7b8c; font-weight: bold;text-shadow: 1px 0 8px #0f7b8c;">'.$us['login'].'</span></a>';
			}
		echo '<div class="row">'.$nick.'<small>['.clock($mail['time']).']</small><br>
		'.smile($mail['text']).'</div>';
	}
if($total > $set['str'])
	{
		pages($page, $total, 'mail.php?ank='.$ank_id.'&amp;', $set['str']);
	}
	echo '<div class="name"><a href="mail.php">Почта</a></div>';
}else{
$test_na_ign =  mysql_result(mysql_query("SELECT COUNT(id) FROM `mail_ignore` WHERE `user_id`='".$ank_id."' AND `ignore_id`='".$user['id']."'"),0);
	if($test_na_ign >0){
	echo '<div id="error">Вы не можете писать этому игроку. Вы у него в игнор-листе.</div>';
	foot();
	exit;
	}
header("Location: mail.php?ank=".$ank_id);
$text = protect($_POST['text']);
mysql_query("INSERT INTO `mail` SET
`from_id`='".$user['id']."',
`to_id`='".$ank_id."',
`text`='".$text."',
`time`='".time()."'");
mysql_query("UPDATE `mail_contact` SET 
`mess`=`mess`+1,
`time`='".time()."'
WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."'");
mysql_query("UPDATE `mail_contact` SET 
`mess`=`mess`+1,
`new_mess`=`new_mess`+1,
`time`='".time()."'
WHERE `user_id`='".$ank_id."' AND `contact_id`='".$user['id']."'");
mysql_query("UPDATE `users` SET `new_mess`=`new_mess`+1 WHERE `id`='".$ank_id."'");
}





}else{
	if(isset($_GET['ign_list'])){
	echo '<div class="name">Игнор-Лист</div>';
	$ignore_q = mysql_query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' ORDER BY `id` DESC LIMIT ".$page.",".$set['str']." ");
	$total = mysql_result(mysql_query("SELECT COUNT(id) FROM `mail_ignore` WHERE `user_id`='".$user['id']."'"),0);
		if($total == 0){
		echo '<div class="row">В игнор-листе никого.</div>';
		}else{
			while($ignore = mysql_fetch_assoc($ignore_q)){
			$ign_us = mysql_fetch_assoc(mysql_query("SELECT `login`,`id` FROM `users` WHERE `id`='".$ignore['ignore_id']."' LIMIT 1"));
			echo '<div class="row"><a href="pers.php?ank='.$ign_us['id'].'">'.$ign_us['login'].'</a>|<a href="mail.php?ank='.$ign_us['id'].'&amp;ign_del">убрать</a></div>';
			
			}
			
			if($total > $set['str'])
			{
				pages($page, $total, 'mail.php?ign_list&amp;', $set['str']);
			}
		}
		echo '<div class="name"><a href="mail.php">Почта</a></div>';
	foot();
	exit;
	}
$total = mysql_result(mysql_query("SELECT COUNT(id) FROM `mail_contact` WHERE `user_id`='".$user['id']."'"),0);
$contact_q = mysql_query("SELECT * FROM `mail_contact` WHERE `user_id`='".$user['id']."' ORDER BY `time` DESC LIMIT ".$page.",".$set['str']." ");
	echo '<div class="name">Письма:</div>';
	if($user['new_mess']>0){
	mysql_query("UPDATE `users` SET `new_mess`=0 WHERE `id`='".$user['id']."'");
	}
	if($total==0){
	echo '<div class="row">Вы не видете ни с кем переписку :( </div>';
	foot();
	exit;
	}
	
	while($contact = mysql_fetch_assoc($contact_q)){
	$ank = mysql_fetch_assoc(mysql_query("SELECT `login`,`id` FROM `users` WHERE `id`='".$contact['contact_id']."'"));
		if($contact['new_mess'] >0){
		echo '<div class="row"><a href="mail.php?ank='.$ank['id'].'"><b>'.$ank['login'].'</b></a> ['.$contact['mess'].'|<font color="orange">новых: '.$contact['new_mess'].'</font>] <span style="font-size: 80%;">('.clock($contact['time']).')</span></div>';
		}else{
		echo '<div class="row"><a href="mail.php?ank='.$ank['id'].'">'.$ank['login'].'</a> ['.$contact['mess'].'] <span style="font-size: 80%;">('.clock($contact['time']).')</span></div>';
		}
	}
	echo '<div class="name"><a href="mail.php?ign_list">Игнор-Лист</a></div>';
if($total > $set['str'])
	{
		pages($page, $total, 'mail.php?', $set['str']);
	}
}
foot();
?>