<? include('checkcookie.php'); ?>
<link rel="stylesheet" type="text/css" media="screen,projection,print" href="../css/mail.css" />
<style type="text/css">
h3{
    text-align: left;
    font: 14px Tahoma, Arial, sans-serif;
    color: #00649E;
    margin-top: 10px;
    margin-bottom: 5px;
    border-bottom: 1px dotted #00649E;
}
</style>
<span id="lot"><h3>Почта</h3>
<a href='#top' onclick="javascript:document.getElementById('mail').style.display = '';" style="color:#006600;">Настройка</a>
<?
if($_GET["option"]=="dellmail")
{
   $data=$_POST["d"]; // дни старit которого удаляем письма
   $nowtime=time();   // Текщая дата
   $timemailout=$nowtime-86400*$data;
   
   $res = "DELETE FROM tb_mail WHERE izb=0 and data<$timemailout";
   $res = mysql_query($res) or die(mysql_error());
   $res = mysql_affected_rows();
	 echo '<br><br><i><b><span style="color:#1fa600;" >&#9758; Писма старше <span style="color:#ff0000;" >'.$data.'</span> дней удалены! Всего сообщений удалено <span style="color:#ff0000;" >'.$res.'</span></span></b></i>';  
}

if(isset($_GET["maildeldialog"]))
{
    $maildell=$_GET["maildeldialog"];
    $res=mysql_query("select sender,recipient,subject from tb_mail where id='$maildell'");
	  $res=mysql_fetch_array($res);
	  $sender=$res["sender"];
	  $recipient=$res["recipient"];
	  $subject=$res["subject"];
    $qw="DELETE from tb_mail where subject='$subject' and (( sender='$sender' and recipient='$recipient' ) or (sender='$recipient' and recipient='$sender') )";
    $res=mysql_query($qw);
    echo '<br><br><i><b><span style="color:#1fa600;" >&#9758; Переписка между <span style="color:#ff0000;" >'.$sender.'</span> и <span style="color:#ff0000;" >'.$recipient.'</span> по теме <span style="color:#ff0000;" >'.$subject.'</span> удалена!</span></b></i>';
 
}

?>
<div  id='mail' style=' display:none; margin: 5px 0 0 0; padding:5px;' align='left'>
<table class="history" width="40%" ><thead>
  <tr>
    <th width="70%">Настройки</th>
    <th width="30%">Действие</th>
  </tr></thead><tbody>
  <tr>
   <form action="adminmain.php?p=adm_mail&option=dellmail" method="post" >
   <td align="left">Очистить письма старше <input class="inputtext" type="text" value="30" size="5" maxlength="5" name="d"> дней</td>
   <td align="left"><input type="submit" value="Очистить почту"> </td>
   </form>
  </tr>
  <tbody></table>
</div>
<h3>Диологи пользователей</h3>

<?
   $tableName="tb_mail";
   $qw="";
   $qw="select * from (select * from $tableName order by data  desc) as Actions group by  subject order by data  desc";
   // Блок листалки --------------------------------------------------------------
   $limit = 10; // количество выводимой информации на одной странице
   $var="&p=adm_mail";
   $res=mysql_query($qw);
   $total_pages=mysql_num_rows($res);  
   include("../page.php");
   // Конец листалки -------------------------------------------------------------
   $res=mysql_query($qw.$qw3); // Реальный запрос
?>

<center><?=$paginate;?>
<table class='history' width='60%' 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>

<?
  while($row=mysql_fetch_array($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_array($restema);
  $kolvo=$restema["kolvo"];
	echo "
    <tr>
      <td class='value'><span class='$strstatus'></span></td>
      <td class='value'>$mail_data</td>";
     
      ?> <td><a href="adminmain.php?p=adm_mail&mail=<?=$mail_id?>&page=<?=$page?>"><?=$mail_subject?></a><div style="float: right; display: block; height: 16px; "><span class="mail-dell"  style="cursor: pointer;" title="Удалить всю переписку" 
      onclick="javascript:
      if (confirm('Вы уверены что хотите удалить диалог?'))
           { 
            top.location = 'adminmain.php?p=adm_mail&page=<?=$page?>&maildeldialog=<?=$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>
<?=$paginate;?>
</center>

<?
if(isset($_GET["mail"]))
{
    $mail=uc(limpiar($_GET["mail"]));
    $res=mysql_query("select subject,sender,recipient from tb_mail where id='$mail'");
	  $res=mysql_fetch_array($res);
	  $subject=$res["subject"];
	  $sender=$res["sender"];
	  $recipient=$res["recipient"];
	  $otkogo='';
    $komu='';
    if($sender==$user) {$otkogo=$sender; $komu=$recipient; } else {$otkogo=$recipient; $komu=$sender;} // Определям кому отправить и откого
    $qw="select sender,recipient,message,data from tb_mail where subject='$subject' and (( sender='$sender' and recipient='$recipient' ) or (sender='$recipient' and recipient='$sender') )  order by data  desc ";
    $res=mysql_query($qw);
    
    echo '<h3>Переписка между <span style="color:#1fa600;" >'.$sender.'</span> и <span style="color:#1fa600;" >'.$recipient.'</span></h3>';
?>
    <center>

    <table class="letter" width="100%" border="0" cellpadding="0" cellspacing="0"><tbody>
    <tr class="top">
      <td class="empty" style="padding-top: 10px;"><?=$subject?></td>
    </tr>
    <?
    
    $i=mysql_num_rows($res)+1;
    while($row=mysql_fetch_array($res))
      {
	$mail_sender=$row["sender"];
	$mail_recipient=$row["recipient"];
	$mail_message=$row["message"];
	$mail_data=$row["data"];

	$mail_data=date("d.m.Y - H:i",$mail_data);
	$i--;
	if($mail_sender!=$user) $l=1; else $l=2;

?>
      <tr>
      <td align="left" style='border:1px solid#ddd;'>
      <span class="mailtext'.$l.'"><?=$mail_message;?><br /><span class="mailtitle'.$l.'"><?=$mail_sender;?>  (<?=$mail_data;?>)<span class="mailnum'.$l.'"><?=$i;?></span></span></span>
      </td>
      </tr><?
      }
    ?>
    <tr class="bot">
      <td class="empty"></td>
    </tr></tbody>
    </table>   
    </center>

<?
}
?>