<?
include '../config.php';
include '../func.php';

head();
aut();
ban();

echo head . 'Личные сообщения' . div;

switch ($_GET['mod']) {
    
    default:
    if ($_GET['err']==9)echo '<b>Не чего не выделено.</b>';
    if ($_GET['err']==10)echo '<b>Ошибка при удаление выделеных писем.</b>';
    if (isset($_GET['ok']))echo '<b>Выделеные письма успешно удалены.</b>';
    
    echo '<form action="mail.php?mod=del2" method="post">';
    $all = mysql_result(mysql_query("SELECT count(*) FROM `mail`"), 0);
    
    if ( $all > 0 ) {
        
    $onpage = 10;
    $allpage = ceil($all/$onpage);
    $this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
    if($this_page < 1 || $this_page > $allpage)$this_page = 1;
    $start = $this_page*$onpage - $onpage;
    $result = mysql_query("SELECT * FROM mail order by id desc  limit ".num($start).",".num($onpage)."");
    while($a = mysql_fetch_assoc($result)){
    
    echo link1 . '<input type="checkbox"  name="check_del[]" value="'. $a['id'] .'"> От: '; echo user($a['ot_user']) .'<br/>Кому: '; echo user($a['to_user']) .' [<a href="?act=dialog&amp;uid='.$a['ot_user'].'">Диалог</a>]' . div;
    
    $usinfo = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$a['ot_user']."'"));
    if ($usinfo['vip'] > time())
    echo link2 . 'Сообщение от VIP Пользователя: <b>'.smile($a['msg']).'</b></div>';
    else
    echo link2 . 'Текст: '.smile($a['msg']).'<br/>';
    echo '</div>';
    }
    echo link1 . '<b>Страницы:</b> ';
    navigation($allpage,"mail.php?p={p}",1,1);
    
    
    
    echo '<br/ ><input type="submit" class="ibutton" value="Удалить выделеное"/></form><br /><br />';
    
    } else {
        echo 'Еще ничего не писали';
    }
    break;
    
    case del2:
    $p = num($_GET['p']);
    
    if  (!isset($_POST['check_del'])) {
    header('Location: mail.php?p='.$p.'&err=9');
    }
    
    $check_del = $_POST['check_del'];
    $col = 0;
    foreach  ($check_del as $check) {
    $col  = $col + 1;
    
    if(mysql_query("DELETE FROM `mail` WHERE `id` = '".(int)$check."' and `to_user` = '".$id_session."'"))
    {
    header('Location: mail.php?p='.$p.'&ok');
    }
     else
    {
    header('Location: mail.php?p='.$p.'&err=10');
    }
    }
    break;
}
////////////////////////////////////////////////////////////////////////////////
navig();
foot();