<?php
  
REQUIRE(ROOT.'/users/profile/mail/plugins/mail.php');  
  
$mail = DB::GET_STRING("SELECT * FROM `MAIL` WHERE `USER_ID` = '".INTVAL(GET('id'))."' AND `MY_ID` = '".$user['ID']."' LIMIT 1");
$account = DB::GET_STRING("SELECT * FROM `USERS` WHERE `ID` = '".$mail['USER_ID']."' LIMIT 1");
  
IF (!ISSET($mail['ID'])){
  
  ERROR('Такой переписки не существует');
  REDIRECT('/users/profile/?path=mail');
  
}

IF (!ISSET($account['ID'])){
  
  ERROR('Такого пользователя не существует');
  REDIRECT('/users/profile/?path=mail');
  
}

IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `MAIL_MESSAGE` WHERE (`USER_ID` = '".$user['ID']."' OR `MY_ID` = '".$user['ID']."') AND (`USER_ID` = '".$account['ID']."' OR `MY_ID` = '".$account['ID']."') AND `USER` = '".$user['ID']."' LIMIT 1") == 1 ){
  
  DB::GET_SET("DELETE FROM `MAIL` WHERE `USER_ID` = '".$list['MY_ID']."' AND `MY_ID` = '".$list['USER_ID']."' LIMIT 1");

}
  
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 "<a href='/users/profile/?path=mail'>".LG('Почта')."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "".LG('Переписка с')." ".$account['LOGIN']."";
ECHO "</div>";

REQUIRE(ROOT.'/users/profile/mail/plugins/modal.php');

ECHO "<div class='list'>";
ECHO "<table style='width: 100%;'><tr>";
    
ECHO "<td class='mail-list-avatar'>";  
ECHO "".USER::AVATAR($account['ID'], 50, 0, 1)."";  
ECHO "</td>";
    
ECHO "<td class='mail-list'>";  
ECHO "".USER::LOGIN($account['ID'], 0, 1)."";
      
IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `USERS` WHERE `ID` = '".$account['ID']."' AND `DATE_VISIT` > '".(TIME()-CONFIG('ONLINE_TIME_USERS'))."' LIMIT 1") == 0){
  
  IF ($account['SEX'] == 2){
    
    ECHO "<br /> <font color='gray' style='font-size: 11px;'>".LG('Была в сети')." ".STIME($account['DATE_VISIT'])."</font>";
    
  }ELSE{
    
    ECHO "<br /> <font color='gray' style='font-size: 11px;'>".LG('Был в сети')." ".STIME($account['DATE_VISIT'])."</font>";
  
  }
  
}
    
ECHO "<br />";

ECHO "</td>";
    
ECHO "</tr></table>";
ECHO "</div>";

//Ответ на сообщение
REQUIRE(ROOT.'/users/profile/mail/plugins/reply.php');

IF (POST('ok_message')){
  
  DB_FILTER();
  
  $err_data = 0;

  $message = ADSL(POST('message'));
  
  IF (STR($message) < 1){
    
    ERROR('Сообщение не может быть меньше 1 символа', 'session');
    $err_data = 1;
    
  }
  
  IF (STR($message) > 3000){
    
    ERROR('Сообщение не может быть больше 3000 символов', 'session');
    $err_data = 1;
    
  }
  
  $mess = DB::GET_STRING("SELECT `MESSAGE` FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$account['ID']."' AND `MY_ID` = '".$user['ID']."' ORDER BY `TIME` DESC LIMIT 1");
  
  IF ($message == $mess['MESSAGE']){

    ERROR('Ваше сообщение повторяет предыдущее', 'session');
    $err_data = 1;
  
  }
  
  $mail_set_my = DB::GET_STRING("SELECT * FROM `MAIL_SETTINGS` WHERE `USER_ID` = '".$user['ID']."' LIMIT 1");
  
  IF ($mail_set_my['PRIVATE'] == 2){

    ERROR('Вы не можете писать письма, так как закрыли свою почту от всех', 'session');
    $err_data = 1;
  
  }
  
  $mail_set_user = DB::GET_STRING("SELECT * FROM `MAIL_SETTINGS` WHERE `USER_ID` = '".$account['ID']."' LIMIT 1");
  
  IF ($mail_set_user['PRIVATE'] == 2){

    ERROR('Вы не можете писать письма этому пользователю, так как он закрыл свою почту от всех', 'session');
    $err_data = 1;
  
  }
  
  IF ($mail_set_user['PRIVATE'] == 1 && DB::GET_COLUMN("SELECT COUNT(*) FROM `FRIENDS` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$account['ID']."' AND `ACT` = '0' LIMIT 1") == 0){

    ERROR('Данному пользователю могут писать только друзья', 'session');
    $err_data = 1;
  
  }
  
  IF ($err_data == 1){
    
    REDIRECT('/users/profile/?path=mail&section=message&id='.$account['ID'].'');
    
  }
  
  $ID_my = DB::GET_ADD("INSERT INTO `MAIL_MESSAGE` (`USER_ID`, `MY_ID`, `TIME`, `MESSAGE`, `USER`, `REPLY`) VALUES ('".$account['ID']."', '".$user['ID']."', '".TIME()."', '".$message."', '".$user['ID']."', '".$reply_get."')");
  
  DB::GET_ADD("INSERT INTO `MAIL_MESSAGE` (`USER_ID`, `MY_ID`, `TIME`, `MESSAGE`, `USER`, `REPLY`) VALUES ('".$account['ID']."', '".$user['ID']."', '".TIME()."', '".$message."', '".$account['ID']."', '".$reply_get."')");
  
  DB::GET_SET("UPDATE `ATTACHMENTS` SET `ID_POST` = '".$ID_my."', `TYPE_POST` = 'mail_message', `ACT` = '1' WHERE `USER_ID` = '".$user['ID']."' AND `ACT` = '0' AND `COMMENTS` = '1'");

  REDIRECT('/users/profile/?path=mail&section=message&id='.$account['ID'].'&reply=0');
  
}

HTML::COMMENT('ok_message', '/users/profile/?path=mail&section=message&id='.$account['ID'].$reply, NULL, 'mail');

//Помечаем сообщения как прочитанные
IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$account['ID']."' AND `READ` = '0' LIMIT 1")){
  
  DB::GET_SET("UPDATE `MAIL_MESSAGE` SET `READ` = '1' WHERE `MY_ID` = '".$account['ID']."' AND `USER_ID` = '".$user['ID']."' AND `READ` = '0'");
  
}

ECHO "<div id='mail-upgrade' action='/users/profile/?path=mail&section=message&id=".$account['ID']."&page=".CLEARSPECIALCHARS(GET('page'))."'>";

$column = DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `MAIL_MESSAGE` WHERE (`USER_ID` = '".$user['ID']."' OR `MY_ID` = '".$user['ID']."') AND (`USER_ID` = '".$account['ID']."' OR `MY_ID` = '".$account['ID']."') AND `USER` = '".$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_MESSAGE` WHERE (`USER_ID` = '".$user['ID']."' OR `MY_ID` = '".$user['ID']."') AND (`USER_ID` = '".$account['ID']."' OR `MY_ID` = '".$account['ID']."') AND `USER` = '".$user['ID']."' ORDER BY `TIME` DESC LIMIT ".$limit.", ".$page_settings."");

WHILE ($list = $data->FETCH()){
  
  /*
  -----------------
  Сообщения от меня
  -----------------
  */
  
  IF ($list['MY_ID'] == $user['ID']){
    
    ECHO "<div class='mail-message-my'>";
    
    IF ($list['READ'] == 0){
      
      ECHO "<span class='mail-message-eye'>";
      ECHO "".ICONS('eye-slash', 15, 'fa-fw')."";
      ECHO "</span>";
    
    }
    
    ECHO "<div class='mail-message-my-form'>";
    
    //Ответ на сообщение
    REQUIRE(ROOT.'/users/profile/mail/plugins/reply_message.php');
    
    IF (STR($list['MESSAGE']) > 250){
      
      ECHO "<div id='mail_mess".$list['ID']."' style='margin-top: 5px;'>";
      ECHO TEXT(CROP_TEXT($list['MESSAGE'], 0, 250));
      ECHO "</div>";
      
      ECHO "<div id='mail_mess2".$list['ID']."' style='display: none; margin-top: 5px;'>";
      
      ECHO TEXT($list['MESSAGE']);
      
      ECHO "</div>";
      
      ?><a onclick="show_or_hide('mail_mess<?=$list['ID']?>', 'mail_mess2<?=$list['ID']?>', 'chevron<?=$list['ID']?>', 'opacity_message<?=$list['ID']?>')" load="none"><?
        
      ECHO "<div id='chevron".$list['ID']."' class='chevron'><i class='fa fa-chevron-down fa-fw'></i></div>";
      ECHO "</a>";
    
    }ELSE{
      
      ECHO TEXT($list['MESSAGE']);
    
    }
    
    ECHO "<br />";
    
    /*
    -------------------
    Прикрепленные файлы
    -------------------
    */
    
    $f_data = DB::GET_STRING_ALL("SELECT * FROM `ATTACHMENTS` WHERE `ID_POST` = '".$list['ID']."' AND `TYPE_POST` = 'mail_message' ORDER BY `TIME` DESC");
    
    WHILE ($f_list = $f_data->FETCH()){
      
      REQUIRE (ROOT.'/users/attachments/plugins/inc_files.php');
    
    }
    
    /*-----------------------*/    
    
    ECHO "<span class='mail-time'>".FTIME($list['TIME'])."</span>";
    
    REQUIRE (ROOT.'/users/profile/mail/plugins/menu_mess.php');
    
    ECHO "</div>";
    
    ECHO "<a href='/id".$list['MY_ID']."' class='mail-message-avatar'>";
    USER::AVATAR($list['MY_ID'], 28);
    ECHO "</a>";
    
    ECHO "</div>";
    
  }
  
  /*
  ------------------------
  Сообщения от собеседника
  ------------------------
  */
  
  IF ($list['USER_ID'] == $user['ID']){
    
    ECHO "<div class='mail-message-user'>";
    
    ECHO "<a href='/id".$account['ID']."' class='mail-message-avatar'>";
    USER::AVATAR($account['ID'], 28);
    ECHO "</a>";
    
    ECHO "<div class='mail-message-user-form'>";
    
    //Ответ на сообщение
    REQUIRE(ROOT.'/users/profile/mail/plugins/reply_message.php');
    
    IF (STR($list['MESSAGE']) > 250){
      
      ECHO "<div id='mail_mess".$list['ID']."' style='margin-top: 5px;'>";
      ECHO TEXT(CROP_TEXT($list['MESSAGE'], 0, 250));
      ECHO "</div>";
      
      ECHO "<div id='mail_mess2".$list['ID']."' style='display: none; margin-top: 5px;'>";
      
      ECHO TEXT($list['MESSAGE']);
      
      ECHO "</div>";
      
      ?><a onclick="show_or_hide('mail_mess<?=$list['ID']?>', 'mail_mess2<?=$list['ID']?>', 'chevron<?=$list['ID']?>', 'opacity_message<?=$list['ID']?>')" load="none"><?
        
      ECHO "<div id='chevron".$list['ID']."' class='chevron'><i class='fa fa-chevron-down fa-fw'></i></div>";
      ECHO "</a>";
    
    }ELSE{
      
      ECHO TEXT($list['MESSAGE']);
    
    }
    
    ECHO "<br />";
    
    /*
    -------------------
    Прикрепленные файлы
    -------------------
    */
    
    $mess = DB::GET_STRING("SELECT * FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$list['USER_ID']."' AND `MY_ID` = '".$list['MY_ID']."' AND `USER` = '".$account['ID']."' AND `TIME` = '".$list['TIME']."' AND `MESSAGE` = '".$list['MESSAGE']."' LIMIT 1");
    
    $f_data = DB::GET_STRING_ALL("SELECT * FROM `ATTACHMENTS` WHERE `ID_POST` = '".$mess['ID']."' AND `TYPE_POST` = 'mail_message' ORDER BY `TIME` DESC");
    
    WHILE ($f_list = $f_data->FETCH()){
      
      REQUIRE (ROOT.'/users/attachments/plugins/inc_files.php');
    
    }
    
    /*-----------------------*/ 
    
    ECHO "<span class='mail-time'>".FTIME($list['TIME'])."</span>";
    
    REQUIRE (ROOT.'/users/profile/mail/plugins/menu_mess.php');
    
    ECHO "</div>";
    
    ECHO "</div>";
    
  }
  
}

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

IF ($spage > 1){ ECHO STR_PAGE('/users/profile/?path=mail&section=message&id='.$account['ID'].'&', $spage, $page); }

$msg_my = DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$account['ID']."' AND `MY_ID` = '".$user['ID']."' AND `USER` = '".$user['ID']."'");
$msg_user = DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$account['ID']."' AND `USER` = '".$account['ID']."'");

ECHO "<div class='list'><b>";
ECHO "".LG('Сообщений').": ".LG('от Вас')." - ".$msg_my.", ".LG('от')." ".$account['LOGIN']." - ".$msg_user."";
ECHO "</b></div>";

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 "<a href='/users/profile/?path=mail'>".LG('Почта')."</a>";
ECHO "".ICONS('angle-right', 22, 'fa-fw')."";
ECHO "".LG('Переписка с')." ".$account['LOGIN']."";
ECHO "</div>";
  
?>