<?

include_once '../../sys/inc/start.php';
include_once '../../sys/inc/compress.php';
include_once '../../sys/inc/sess.php';
include_once '../../sys/inc/home.php';
include_once '../../sys/inc/settings.php';
include_once '../../sys/inc/db_connect.php';
include_once '../../sys/inc/ipua.php';
include_once '../../sys/inc/fnc.php';
include_once '../../sys/inc/user.php';

only_reg(); 
$set['title']='Новые сообщения';
include_once '../../sys/inc/thead.php';
title();
aut();

$k_post=mysql_result(mysql_query("SELECT COUNT(DISTINCT `mail`.`id_user`) FROM `mail` LEFT JOIN `users_konts` ON `mail`.`id_user` = `users_konts`.`id_kont` AND `users_konts`.`id_user` = '$user[id]' WHERE `mail`.`id_kont` = '$user[id]' AND (`users_konts`.`type` IS NULL OR `users_konts`.`type` = 'common' OR `users_konts`.`type` = 'favorite') AND `mail`.`read` = '0'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

if ($k_post==0){
echo '<div class="block">Нет новых сообщений</div>';
}else{
$q=mysql_query("SELECT MAX(`mail`.`time`) AS `last_time`, COUNT(`mail`.`id`) AS `count`, `mail`.`id_user`, `users_konts`.`name` FROM `mail` LEFT JOIN `users_konts` ON `mail`.`id_user` = `users_konts`.`id_kont` AND `users_konts`.`id_user` = '$user[id]' WHERE `mail`.`id_kont` = '$user[id]' AND (`users_konts`.`type` IS NULL  OR `users_konts`.`type` = 'common' OR `users_konts`.`type` = 'favorite') AND `mail`.`read` = '0' GROUP BY `mail`.`id_user` ORDER BY `count` DESC LIMIT $start, $set[p_str]");

while ($kont = mysql_fetch_assoc($q)){
$ank=get_user($kont['id_user']);
$ank2 = get_user($ank['id']);

echo '<a style="line-height: 23px;" class="link" href="mail.php?id=' . $ank['id'] . '">';
echo '<span style="float: left; margin: -1px 10px 0 0;">'.avatar($ank['id']).'</span> <span style="margin: 25px -23px;" class="avatar_icon">'.online($ank['id']).'</span>';
echo '<span class="nick">';
//Система
if ($ank2['id']==0){
echo ''.$ank2[ank_name].' '.$ank2[ank_fam].'';
}
echo ''.user::nick($ank['id'],0).'';
echo '</span> <span class="time">'.vremja($kont['last_time']).'</span>';
echo '</br>';
echo 'Открыть диалог +'.$kont[count].'';
echo '</a>';
}

}

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

echo '<a class="link3" href="/modules/mail/"><i class="material-icons icon">chat_bubble</i> Диалоги</a>';

include_once '../../sys/inc/tfoot.php';

?>