<?php
define('NTLCMS', 1);
$module = 'mail';
include ('../system/start.php');
include ('../system/function.php');
include ('../system/antidos.php');
$page_title = 'Страница личной почты - клик клуба ' . strtoupper($_SERVER['HTTP_HOST']);
include ('../system/head.php');
include ('../system/crypt.class.php');
if (!$active){
header('Location: '.URL);
$_SESSION['fail'] = 'Вы не авторизированы!';
exit;
}
if ($set['mails'] == '1' && $_USER['admin'] != '1'){
echo '<div class="title"><center>Личная почта</center></div>';
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> Модуль закрыт администратором!</center></div>';
echo '<div class="block"><i class="fa fa-home fa-fw"></i> <a href="'.URL.'/cabinet">В кабинет</a></div>';
include ('../system/foot.php'); exit;
}
echo '<div class="title"><b>'.razd.' Личная почта</b></div>';
$k_post = DB :: run() -> querySingle("SELECT COUNT(DISTINCT c.id) FROM `contacts` c 
WHERE c.kto = ? AND EXISTS (
SELECT 1 FROM `message` m 
WHERE (m.kto = c.kto AND m.komy = c.kogo) OR (m.kto = c.kogo AND m.komy = c.kto)
)", array($ulogin)); 
$k_page = k_page($k_post, $onpage); 
$page = page($k_page); 
$start = $onpage * $page - $onpage; 
$sql = DB :: run() -> query("SELECT c.*, MAX(m.time) as last_time 
FROM `contacts` c 
JOIN `message` m ON (m.kto = c.kto AND m.komy = c.kogo) OR (m.kto = c.kogo AND m.komy = c.kto) 
WHERE c.kto = ? 
GROUP BY c.kogo 
ORDER BY last_time DESC 
LIMIT ".$start.", ".$onpage, array($ulogin));
$has_contacts = false;
$displayed_contacts = array();
while ($row = $sql -> fetch(PDO::FETCH_OBJ)){
if (in_array($row->kogo, $displayed_contacts)) {
continue;
}
$displayed_contacts[] = $row->kogo;
$has_contacts = true;
$user_data = DB :: run() -> query("select * from `users` where `login` = ?", array($row -> kogo)) -> fetch(PDO::FETCH_OBJ);
echo '<div class="menu">'.ava($user_data -> login).'  '.login($row -> kogo, 1).' '.online($row -> kogo).' ['.date('d.m.Y H:i', $row -> last_time).']<br/>';
$count = DB :: run() -> querySingle("select count(`id`) from `message` where (`kto` = ? and `komy` = ?) OR (`kto` = ? and `komy` = ?)", 
array($ulogin, $row -> kogo, $row -> kogo, $ulogin));
$list = DB :: run() -> query("select * from `message` where (`kto` = ? and `komy` = ?) OR (`kto` = ? and `komy` = ?) order by `time` desc limit 1", 
array($ulogin, $row -> kogo, $row -> kogo, $ulogin)) -> fetch(PDO::FETCH_OBJ);
if ($count){
 echo '<i class="fa fa-file-text-o fa-fw"></i> '.outtext(mb_substr($crypt->xoft_decode($list -> text, $set['secure']), 0, 30, 'utf8'), 1 ,1 ,1).'... ';
}
if (!empty($list -> id) and $ulogin != $list -> kto and $list -> readlen == '0') echo '<span style="color:red">NEW!</span>';
echo '<br/><i class="fa  fa-plus-square fa-fw"></i> <a href="'.URL.'/dialog-'.checkout($row -> kogo).'">Диалог</a> ['.$count.']';
echo ' | <i class="fa fa-trash-o fa-fw"></i> <a href="'.URL.'/mail/clear?user='.checkout($row -> kogo).'" onclick="return confirm(\'Вы действительно хотите очистить диалог с пользователем '.checkout($row -> kogo).'?\')">Очистить</a>';
echo '</div>';
}
if (!$has_contacts) {
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> Список контактов пуст!</center></div>';
}
if ($k_page > 1) navigation(URL.'/messages?', $k_page, $page);
echo '<div class="block">';
echo '<i class="fa fa-ban fa-fw"></i> <a href="'.URL.'/mail/blacklist">Черный список</a><br/>';
echo '<i class="fa fa-home fa-fw"></i> <a href="'.URL.'/cabinet">В кабинет</a>';
echo '</div>';
include ('../system/foot.php');
?>