<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');



switch($sd){
default:

$q = mysql_query("SELECT * FROM `ticket` WHERE `user`='$uid' ORDER BY `id` DESC LIMIT 5");
$e = mysql_num_rows($q);
if($e == '0'){
    echo "Вы ещё не обращались в поддержку...<br><br><br>";
    
    echo "<form action='ticket.php?sd=send' method='post'>
    Тема:<br>
    <select name='theme' class='enter'>
    <option value='1'>Ошибки</option>
    <option value='2'>Вопросы по оплате</option>
    <option value='3'>Общая тема</option>
    </select><br>
    <textarea rows='4' name='text' class='enter'></textarea><br>
    <input type='submit' name='submit' value='Отправить' class='enter'></form>";
    
    include ('tmp/foot.php');
    exit;
}else{
    
    While($w = mysql_fetch_assoc($q)){
        
        if($w['status']=='on'){$status = "<font color=lime>ON</font>";}
        if($w['status']=='off'){$status = "<font color=ref>OFF</font>";}
    
        
        echo "<a href='ticket.php?sd=view&id=$w[id]'>$w[theme]</a> ($w[time])($status)<br>";
        
    }
    
    
    echo "<br><hr><br><form action='ticket.php?sd=send' method='post'>
    Тема:<br>
    <select name='theme' class='enter'>
    <option value='1'>Ошибки</option>
    <option value='2'>Вопросы по оплате</option>
    <option value='3'>Общая тема</option>
    </select><br>
    <textarea rows='4' name='text' class='enter'></textarea><br>
    <input type='submit' name='submit' value='Отправить' class='enter'></form>";
    
}



break;


case 'view':

$q = mysql_query("SELECT * FROM `ticket` WHERE `user`='$uid'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == '0'){
    header('Location: ticket.php');
    exit;
}

if(isset($_GET['off'])){
    if($w['user']!==$uid){
        header('Location: main.php');
        exit;
    }
    $time1 = date('d.m.y');
    $time2 = date('H:i:s');
    
    mysql_query("INSERT INTO `ticket_msg` SET `ticket`='$w[id]',`user`='$uid',`text`='Топик закрыт автором.',`time`='$time1 в $time2'");
    mysql_query("UPDATE `ticket` SET `status`='off' WHERE `id`='$w[id]'");
    header('Location: ticket.php?sd=view&id='.$w['id'].'');
    exit;
}
if(isset($_GET['on'])){
    if($w['user']!==$uid){
        header('Location: main.php');
        exit;
    }
    $time1 = date('d.m.y');
    $time2 = date('H:i:s');
    
    mysql_query("INSERT INTO `ticket_msg` SET `ticket`='$w[id]',`user`='$uid',`text`='Топик открыт автором.',`time`='$time1 в $time2'");
    mysql_query("UPDATE `ticket` SET `status`='on' WHERE `id`='$w[id]'");
    header('Location: ticket.php?sd=view&id='.$w['id'].'');
    exit;
}


if($w['status']=='off'){$sts = "<a href='ticket.php?sd=view&id=$w[id]&on'>Открыть</a>";}else{$sts = "<a href='ticket.php?sd=view&id=$w[id]&off'>Закрыть</a>";}
echo "Тема: <b>$w[theme]</b> [$w[time]] [$sts]<br>- - -<br>$w[text]<br>- - -<br>";

$q1 = mysql_query("SELECT * FROM `ticket_msg` WHERE `ticket`='$w[id]' ORDER BY `id` DESC LIMIT 10");

While($w1 = mysql_fetch_assoc($q1)){
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w1[user]'"));
    if($us['access']=='3'){$cl="lime";}
    if($us['access']=='2'){$cl="blue";}
    if($us['access']<'2'){$cl="";}
    echo "<hr><font color=$cl>$us[login]</font> ($w1[time]):<br>$w1[text]";
    
}

echo "<br><br><font size=1 color=grey>В топике выводятся 10 последних сообщений</font>";

if($w['status'] == 'on'){
    echo "<br><br><form action='ticket.php?sd=send2&id=$w[id]' method='post'>
    <textarea cols='50' rows='7' name='text' class='enter'></textarea><br>
    <input type='submit' name='submit' value='Отправить' class='enter'></form>";
}else{
    echo "<br>Данный топик закрыт.";
}


break;

case 'send2':

$text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

$time1 = date('d.m.y');
$time2 = date('H:i:s');

mysql_query("INSERT INTO `ticket_msg` SET `ticket`='$id',`user`='$uid',`text`='$text',`time`='$time1 в $time2'");
header('Location: ticket.php?sd=view&id='.$id.'');
exit;

break;



case 'send':

$theme = intval(htmlspecialchars($_POST['theme']));
$text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

if($theme=='1'){$thm = "Ошибки";}
if($theme=='2'){$thm = "Вопросы по оплате";}
if($theme=='3'){$thm = "Общая тема";}

$time1 = date('d.m.y');
$time2 = date('H:i:s');

mysql_query("INSERT INTO `ticket` SET `user`='$uid',`theme`='$thm',`text`='$text',`time`='$time1 в $time2',`status`='on'");
header('Location: ticket.php');
exit;

break;


}
include ('tmp/foot.php');
?>