<?php
  
REQUIRE(ROOT.'/users/profile/mail/plugins/mail.php');
  
ECHO "<div class='navigation'>";
ECHO "<a href='/'>".ICONS('home', 25, 'fa-fw')."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "<a href='/id".$user['ID']."'>".$user['LOGIN']."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "".LG('Почта')."";
ECHO "</div>";

ECHO "<div class='list'>";
ECHO "<a href='/users/profile/?path=mail&section=settings' class='btn'>".ICONS('gear', 15, 'fa-fw')." ".LG('Настройки')."</a>";
ECHO "<a href='/users/profile/?path=mail&section=write' class='btn'>".ICONS('envelope', 15, 'fa-fw')." ".LG('Написать')."</a>";
ECHO "</div>";

ECHO "<div id='mail-upgrade' action='/users/profile/?path=mail&page=".CLEARSPECIALCHARS(GET('page'))."'>";

$column = DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `MAIL` WHERE `MY_ID` = '".$user['ID']."'");
$spage = SPAGE($column, $page_settings);
$page = PAGE($spage);
$limit = $page_settings * $page - $page_settings;

IF ($column == 0){
  
  HTML::EMPTY();
  
}ELSE{
  
  ECHO "<div class='list-body'>";
  
}

$data = DB::GET_STRING_ALL("SELECT * FROM `MAIL` WHERE `MY_ID` = '".$user['ID']."' ORDER BY `TIME` DESC LIMIT ".$limit.", ".$page_settings."");

WHILE ($list = $data->FETCH()){
  
  $message = DB::GET_STRING("SELECT * FROM `MAIL_MESSAGE` WHERE (`USER_ID` = '".$list['USER_ID']."' OR `MY_ID` = '".$list['USER_ID']."') AND (`USER_ID` = '".$list['MY_ID']."' OR `MY_ID` = '".$list['MY_ID']."') AND `USER` = '".$list['MY_ID']."' ORDER BY `TIME` DESC LIMIT 1");  
  
  //Удаляем диалог если в нем нет писем
  IF (!ISSET($message['ID'])){
    
    DB::GET_SET("DELETE FROM `MAIL` WHERE `ID` = '".$list['ID']."' LIMIT 1");
    REDIRECT("/users/profile/?path=mail");
  
  }
  
  ECHO "<a href='/users/profile/?path=mail&section=message&id=".$list['USER_ID']."'>";
  
  /*
  -----------
  Отправитель
  -----------
  */
  
  IF ($message['MY_ID'] == $user['ID']){
    
    IF ($message['READ'] == 0){ $class_mess = "mail-mess-send"; }ELSE{ $class_mess = "mail-mess-send-o"; }
    
    ECHO "<div class='list-menu'>";
    ECHO "<table style='width: 100%;'><tr>";
    
    ECHO "<td class='mail-list-avatar'>";  
    ECHO "".USER::AVATAR($list['USER_ID'], 50, 0, 1)."";  
    ECHO "</td>";
    
    ECHO "<td class='mail-list'>";  
    ECHO "".USER::LOGIN($list['USER_ID'])."";
      
    ECHO "<span style='float: right;'><font color='#68B4F0'>".ICONS('arrow-circle-right', 17, 'fa-fw')."</font></span>";
    
    ECHO "<br />";
    
    ECHO "<span class='time'>".STIME($list['TIME'])."</span><br />";
    ECHO "<div class='".$class_mess."'>".TEXT_ABSOLUTE(CROP_TEXT($message['MESSAGE'], 0, 20))."</div>";
    ECHO "</td>";
    
    ECHO "</tr></table>";
    ECHO "</div>";
  
  }
  
  /*
  ----------
  Получатель
  ----------
  */
  
  IF ($message['USER_ID'] == $user['ID']){
    
    IF ($message['READ'] == 0){
      
      $count2 = DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$message['USER_ID']."' AND `MY_ID` = '".$message['MY_ID']."' AND `READ` = '0' AND `USER` = '".$user['ID']."'");
      
      $bcgr_mess = "#E3EDF1"; 
      $count = "<span class='count'>".$count2."</span>"; 
    
    }ELSE{ 
      
      $bcgr_mess = "white"; 
      $count = NULL;
    
    }
    
    ECHO "<div class='list-menu' style='background-color: ".$bcgr_mess.";'>";
    ECHO "<table style='width: 100%;'><tr>";
    
    ECHO "<td class='mail-list-avatar'>";  
    ECHO "".USER::AVATAR($list['USER_ID'], 50, 0, 1)."";  
    ECHO "</td>";
    
    ECHO "<td class='mail-list'>";  
    ECHO "".USER::LOGIN($list['USER_ID'])." ".$count."";
    
    ECHO "<span style='float: right;'><font color='#FF776D'>".ICONS('arrow-circle-left', 17, 'fa-fw')."</font></span>";
    
    ECHO "<br />";
    
    ECHO "<span class='time'>".STIME($list['TIME'])."</span><br />";        
    ECHO "<div class='mail-mess-send-o'>".TEXT_ABSOLUTE(CROP_TEXT($message['MESSAGE'], 0, 20))."</div>";
    ECHO "</td>";
    
    ECHO "</tr></table>";
    ECHO "</div>";
  
  }
  
  ECHO "</a>";
  
}

IF ($column > 0){
  
  ECHO "</div>";
  
}

IF ($spage > 1){ ECHO STR_PAGE('/users/profile/?path=mail&', $spage, $page); }

ECHO "</div>";

ECHO "<div class='navigation'>";
ECHO "<a href='/'>".ICONS('home', 25, 'fa-fw')."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "<a href='/id".$user['ID']."'>".$user['LOGIN']."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "".LG('Почта')."";
ECHO "</div>";
  
?>