<?include('header.php');

if(!(isset($_SESSION["username"]) && isset($_SESSION["password"])))
{
	?>
	<script type="text/javascript">
	location.replace("login.php");
	</script>
	<noscript>
	<meta http-equiv="refresh" content="0; url=login.php">
	</noscript>
	<?
	exit();
}
$user=text($_SESSION["username"]);
?>
<div class="menucenter">
<table align='center'>
<tr>
<td>
<span class="as" style="color:#4F4F4F;font-size:11px;border:none;">Администратор: 
<?
$res=mysql_query("select count(*) as kolvo from users_online where user='admin'");
$res=mysql_fetch_array($res);
if($res["kolvo"]>0)
{
     echo "<span style=\"color:#00dd00\">on-line</span>";
}else{
     echo "<span style=\"color:#ff0000\">off-line</span>";
}
?>
</span>
</td>
<td>
<span class="as" style="color:#4F4F4F;font-size:11px;border:none;">
Модератор:
<?
$res=mysql_query("select count(*) as kolvo from users_online where user='porezon'");
$res=mysql_fetch_array($res);
if($res["kolvo"]>0)
{
     echo "<span style=\"color:#00dd00\">on-line</span>";
}else{
     echo "<span style=\"color:#ff0000\">off-line</span>";
}
?>
</span>
</td></tr></table>
<script type="text/javascript" language="javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" language="JavaScript">
            function gofilter(mode)
            {
                setCookie("filtermail",mode,"Mon, 01-Jan-2020 00:00:00 GMT");
                top.location = '/mail.php';
                return false;
            }
            function setCookie (name, value, expires, path, domain, secure) {
            document.cookie = name + "=" + escape(value) +
              ((expires) ? "; expires=" + expires : "") +
              ((path) ? "; path=" + path : "") +
              ((domain) ? "; domain=" + domain : "") +
              ((secure) ? "; secure" : "");
            }
</script>
<h2>Внутренняя почта</h2>
<p align="justify">Всегда хорошо держать связь с другими участниками проекта. Пожалуйста, помните, что в сообщениях запрещён СПАМ, нецензурная лексика и оскорбления. Так же запрещено испольвать почту в рекламных целях. Администрация имеет право просматривать сообщения и наказывать нарушителей.</p>

<div class='mail-write'><a href='/mailnew.php'>Новое сообщение</a></div>
<div class='filterlines'>
  <?
   $filtermail=0;
   if(isset($_COOKIE['filtermail'])) $filtermail=$_COOKIE['filtermail'];
   if($filtermail==0) echo "<span class='filterlineactive'>Все письма</span>"; else echo "<span class='filterline' onclick='javascript:gofilter(0);'>Все письма</span>";
   if($filtermail==1) echo "<span class='filterlineactive'>Непрочитанные</span>"; else echo "<span class='filterline' onclick='javascript:gofilter(1);'>Непрочитанные</span>";
   if($filtermail==2) echo "<span class='filterlineactive'>Я пишу</span>"; else echo "<span class='filterline' onclick='javascript:gofilter(2);'>Я пишу</span>";
   if($filtermail==3) echo "<span class='filterlineactive'>Мне пишут</span>"; else echo "<span class='filterline' onclick='javascript:gofilter(3);'>Мне пишут</span>";
   if($filtermail==4) echo "<span class='filterlineactive'>Избранное</span>"; else echo "<span class='filterline' onclick='javascript:gofilter(4);'>Избранное</span>";

   $tableName="tb_mail";
   $qw="";

   if($filtermail==0) {    $qw="select * from (select * from $tableName where izb=0 and (sender='$user' or recipient='$user') order by data  desc) as Actions group by  subject order by data  desc";}
   elseif($filtermail==1){ $qw="select * from (select * from $tableName where izb=0 and recipient='$user' and status=0 order by data  desc) as Actions group by sender,recipient,subject order by data  desc";}
   elseif($filtermail==2){ $qw="select * from (select * from $tableName where izb=0 and sender='$user' order by data  desc ) as  Actions group by sender,recipient,subject order by data  desc";}
   elseif($filtermail==3){ $qw="select * from (select * from $tableName where izb=0 and recipient='$user' order by data  desc) as Actions group by sender,recipient,subject order by data  desc";}
   elseif($filtermail==4){ $qw="select * from (select * from $tableName where izb=1 and (sender='$user' or recipient='$user') order by data  desc) as Actions group by  subject order by data  desc";}
   else {}

   // Блок листалки --------------------------------------------------------------
   $limit = 25; // количество выводимой информации на одной странице
   $res=mysql_query($qw);
   $total_pages=mysql_num_rows($res);
   include("page.php");
  // Конец листалки -------------------------------------------------------------
   $res=mysql_query($qw.$qw3); // Реальный запрос
  ?>
</div>
<center><?=$paginate;?></center>
<table class='reyt' width='100%' border='0' cellpadding='0' cellspacing='0'><thead>
<tr>
  <th></th>
  <th align='center' nowrap='nowrap'>Дата</th>
  <th align='center' width='60%'>Тема</th>
  <th></th>
  <th align='center' nowrap='nowrap'>Диалог</th>
</tr></thead>
<tbody>
<script type="text/javascript" src="js/ajax_mail.js"></script>
<?
  while($row=mysql_fetch_assoc($res))
  {
  $mail_id=$row["id"];
	$mail_sender=$row["sender"];
	$mail_recipient=$row["recipient"];
	$mail_subject=$row["subject"];
	$mail_status=$row["status"];
	$mail_data=$row["data"];
	$mail_izb=$row["izb"];
	$mail_data=date("d.m.Y",$mail_data);

	if($mail_status==1||$mail_sender==$user) $strstatus='mail-empty'; elseif($mail_status==0) $strstatus='mail-new';

	$restema=mysql_query("select count(id) as kolvo from tb_mail where subject='$mail_subject' and (( sender='$mail_sender' and recipient='$mail_recipient' ) or (sender='$mail_recipient' and recipient='$mail_sender') ) ");
  $restema=mysql_fetch_assoc($restema);
  $kolvo=$restema["kolvo"];
	echo "
    <tr>
      <td class='value'><span class='$strstatus'></span></td>
      <td class='value'>$mail_data</td>";
      if(!$mail_izb)
      {
      ?> <td><a href="/mailread.php?mail=<?=$mail_id?>"><?=$mail_subject?></a><div style="float: right; display: block; height: 16px; " id="izbr<?=$mail_id?>"><span id="izbra<?=$mail_id?>" class="mail-favorite" style="cursor: pointer;"  title="Добавить переписку в избранное" onclick="send_izb_mail('<?=$mail_id?>');"></span></div></td> <?
      }
      else {
      ?> <td><a href="/mailread.php?mail=<?=$mail_id?>"><?=$mail_subject?></a><div style="float: right; display: block; height: 16px; " id="izbr<?=$mail_id?>"><span id="izbra<?=$mail_id?>" class="mail-favoriteout" style="cursor: pointer;" title="Исключить переписку из избранного" onclick="send_izb_mail('<?=$mail_id?>');"></span></div></td> <?
      }
      echo "<td class='value'>$kolvo</td><td nowrap='nowrap'>$mail_sender <span class='textgray'> </span> &rarr; <b>$mail_recipient</b></td>
   </tr>
	";
	}
?>
</tbody>
</table>
<? include('footer.php'); ?>