<?
//
//
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/inc/system.php"); 
//
//
if(!$user){
header('location:/login/');
exit();
}
//
//
head([]);
//
//
echo logs();
//
//
$id = num($_GET['id']);
		$us = DB::$dbs->queryFetch('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', [$id]);
		//
		//
				if(!$us){
 header('location: /mail_contact/'); exit;
				}
//
//
				if($us['id'] == $user['id']){
 header('location: /mail_contact/'); exit;
				}
//
//
$ch = DB::$dbs->queryFetch('SELECT * FROM `mail` WHERE `id_ank` = ? AND `check` = ? LIMIT 1', [$user['id'],1]);
//
if($ch > 0){
DB::$dbs->query("UPDATE `mail_contact` SET `new` = ? WHERE `id_user` = ? AND `id_ank` = ?", array(0,$user['id'],$id));
DB::$dbs->query("UPDATE `mail` SET `check` = ? WHERE `id_user` = ? AND `id_ank` = ?", array(0,$id,$user['id']));
//header('location:?rc='.$r);
//exit();
}
//
//
if(isset($_POST['add_message'])):
			$text = text($_POST['text']);
			//
			//
			if (empty($text) || mb_strlen($text, 'UTF-8') < 1 || mb_strlen($text, 'UTF-8') > 500):
				$err = 'Сообщение не  может быть пустым';
			endif;
			//
			//
			if($user['level'] <= 6):
			    $err = 'Для общения нужен 7 уровень';
			    endif;
			    //
			    //
			    if($user['ban_time'] > time()):
			    $err = 'Вы забанены, вам нельзя писать сообщения<br><a href="/profile/'.$user['id'].'?my_ban"><u>Узнать подробнее</u></a>';
			    endif;
			    			    //
			    //
			    if($user['spam'] > time()):
			    $err = 'Не так быстро, писать смс можно раз в 3 секунды';
			    endif;
			//
//
			if(isset($err)):
				err2($err);
				header('location: ?id='.$id); exit;
			else:
DB::$dbs->query('INSERT INTO `mail` SET `text` = ?,`time` = ?,`check` = ?,`id_user` = ?,`id_ank` = ?',array($text,time(),1,$user['id'],$id));
DB::$dbs->query('UPDATE `users` SET `spam` = ? WHERE `id` = ? LIMIT 1', array(time()+3,$user['id']));
//
//
$contact = DB::$dbs->queryFetch('SELECT * FROM `mail_contact` WHERE `id_user` = ? AND `id_ank` = ? LIMIT 1', [$user['id'],$id]);
//
//
if($contact['id_ank'] != $id){
DB::$dbs->query('INSERT INTO `mail_contact` SET `time` = ?,`id_user` = ?,`id_ank` = ?',array(time(),$user['id'],$id));
DB::$dbs->query('INSERT INTO `mail_contact` SET `time` = ?,`id_user` = ?,`id_ank` = ?',array(time(),$id,$user['id']));
}else{
DB::$dbs->query('UPDATE `mail_contact` SET `time` = ? WHERE `id_user` = ? AND `id_ank` = ? LIMIT 1', array(time(),$user['id'],$id));
DB::$dbs->query('UPDATE `mail_contact` SET `time` = ?, `new` = ? WHERE `id_user` = ? AND `id_ank` = ? LIMIT 1', array(time(),1,$id,$user['id']));
}
//
//

				header('location: ?id='.$id); exit;
			endif;
		        endif;
//
//
//
//
?>
<div class="poster -gas">
		    <div class="start">
		        <center>
		            <span class="title-wqi">Диалог с <?=$us['name'];?></span>
		 <?
		 if($user['level'] >= 7){
		 ?>       

		            		        </center>
	<form action="?id=<?=$id;?>" method="post">
	<textarea class="" name = 'text' id='message' placeholder='' maxlength = '5000'></textarea>
	<input type="submit" name="add_message" class="butmin" value="Отправить">
	</form>
<?
}else{
?>
<div class="menu-not"><font color="tomato">Для общения нужен 7 уровень</font></div>
<?
}
?>
<div class="stats2"></div>
		        <?
                $coll = DB::$dbs->querySingle("SELECT COUNT(id) FROM `mail` WHERE `id_user` = '".$us['id']."' && `id_ank` = '".$user['id']."' OR `id_user` = '".$user['id']."' && `id_ank` = '".$us['id']."'");
		$num = 10;
		$total = intval(($coll - 1) / $num) + 1;
		$page = isset($_GET['page'])?intval($_GET['page']):1;
		if(empty($page) or $page < 0) $page = 1;
		if($page > $total) $page = $total;
		$start = $page * $num - $num; 

$sql = DB::$dbs->query("SELECT * FROM `mail` WHERE `id_user` = '".$us['id']."' && `id_ank` = '".$user['id']."' OR `id_user` = '".$user['id']."' && `id_ank` = '".$us['id']."' ORDER BY `id` DESC LIMIT $start, $num")->fetchAll(PDO::FETCH_BOTH);

				if($coll < 1){
		 ?>
		 <div class="menu-not">
		    Сообщений еще нет
		   </div>
		 <?   
		}
			foreach ($sql as $sqlls => $im): ?>


<div class="menu-not2">
<?=user_info($im['id_user']);?>
<br>

<font color="#777777">
<?=$im['text'];?>
<br>
<span style="float: right;"><?=times($im['time']);?></span>
</font>
<br>
</div>


			<?php endforeach;
			?>
			</div>
			<?
			echo page('?id='.$id.'&',$page,$total); ?>

			
<div class="start">
<a href="/mail_contact/"><div class="menu-not2">Вернуться к диалогам</div></a>
			</div>	</div>
<?
//
//
foot([]);
//
//
?>