<?
$title = 'Мои тикеты';
include_once  '../../sys/fnc.php';
include_once '../../sys/head.php';
echo '<div id="page-wrapper"><div class="main-page"><h3 class="title1"> '.html($f['name']).' </h3><div class="blank-page widget-shadow scroll" id="style-2 div1"><p>';
if (!isset($user)){header("Location: /scripts/tickets/index.php");exit;}


$id=intval($_GET['id']);
if (!isset($_GET['id'])){header("Location: /scripts/tickets/index.php");
exit;
}
$q=mysql_query("SELECT * FROM `tickets` WHERE `id` = '$id'");
if (mysql_num_rows($q)==0){header("Location: /scripts/tickets/index.php");
exit;
}
$f=mysql_fetch_assoc($q);
if (($user->position=='user') && ($f['user']!=$user->id)){header("Location: /index.php");
exit;
}
/*  изменить комментарий  */
if (isset($_GET['set'])) {
$tiket = mysql_fetch_assoc(mysql_query("SELECT `user` FROM `tkomm` WHERE `id` = '".intval($_GET['id'])."'"));
$post_avt = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `user` WHERE `id` = '".$tiket['user']."'"));

if (isset($user) && isset($_POST['msg'])) {
$msg = mysql_real_escape_string($_POST['msg']);

mysql_query("UPDATE `tkomm` SET `msg` = '".mysql_escape_string($msg)."' WHERE `id`= '".intval($_GET['set'])."'");
unset($_POST['msg']);
unset($msg);
	}
$q=mysql_query("SELECT * FROM `tkomm` WHERE `id` = '".intval($_GET['set'])."'");
while ($post = mysql_fetch_assoc($q))
{
if ($post['time']>time()-600 && $post['user']==$user->id){

if (isset($user) && $post_avt['id'] == $user->id) {
echo "<form method='post' name='message'>\n";
$q=mysql_query("SELECT * FROM `tkomm` WHERE `id` = '".intval($_GET['set'])."'");
echo "<div class='post'>Сообщение:<br />\n";
while ($post = mysql_fetch_assoc($q)){
echo "<textarea name='msg' rows='5' cols='17' style='width:60%'>".html($post['msg'])."</textarea><br />\n";
}
echo "<input name='post' value='Ответить' type='submit' /><br />\n";
echo "</form></div>\n";
echo $div->div_menu.' <img src="'.$HOME.'/style/img/link.png"> <a href="'.$HOME.'/scripts/tickets/ticket.php?id='.intval($_GET['id']).'">Назад в тикет</a></div>'; 

include_once '../../sys/foot.php';
	} else {
header("Location: ".SID);
exit;
	}
exit;
}else{
echo '<div class="err">Время вышло,или не вы автор комментария!</div>';
include_once '../../sys/foot.php';
}
}
}

if (isset($_GET['open'])){
mysql_query("UPDATE `tickets` SET `closed` = '0' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
if (isset($_GET['closed'])){
mysql_query("UPDATE `tickets` SET `closed` = '1' WHERE `id` = '$id'");
header("Location: /scripts/tickets//ticket.php?id=".$id);
}
if (($f['user']==$user->id) && ($f['user_read']==0))
{
mysql_query("UPDATE `tickets` SET `user_read` = '1' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
/*
if (($user->position=='admin') && ($f['read']==0))
{
mysql_query("UPDATE `tickets` SET `read` = '1' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
*/
if (isset($_GET['act']) && $_GET['act']=='transfer_the_administration_of_Chapter_ok')
{
if ($user->position == 'admin' || $user->position == 'mod' && ($f['adm_transferred']==1))
{
mysql_query("UPDATE `tickets` SET `adm_transferred` = '0' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
}
if (isset($_GET['act']) && $_GET['act']=='transfer_the_administration_of_Chapter')
{
if ($user->position=='admin')
{
mysql_query("UPDATE `tickets` SET `read` = '0' WHERE `id` = '$id'");
mysql_query("UPDATE `tickets` SET `adm_transferred` = '1' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
}
if (isset($_GET['act']) && $_GET['act']=='transfer_the_administration_of_Chapter_off')
{
if ($user->position=='admin')
{
mysql_query("UPDATE `tickets` SET `read` = '0' WHERE `id` = '$id'");
mysql_query("UPDATE `tickets` SET `adm_transferred` = '0' WHERE `id` = '$id'");
header("Location: /scripts/tickets/ticket.php?id=".$id);
}
}

$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '".$f['user']."'"));
		
if (isset($_GET['delk'])){
$komm=mysql_fetch_assoc(mysql_query("SELECT `ticket` FROM `tkomm` WHERE `id` = '".intval($_GET['delk'])."'"));
if ($komm['ticket']==$id)
{
mysql_query("DELETE FROM `tkomm` WHERE `id` = '".intval($_GET['delk'])."'");
mysql_query("UPDATE `tickets` SET `read` = '1' WHERE `id` = '$id'");
header("Location: ticket.php?id=".$id);
}
}
if (isset($_POST['msg']) && ($f['closed']==0)){
$msg = html($_POST['msg']);

if ($user->position == 'user'){
$read = 0;
$user_read = 1;
} else {
$read = 1;
$user_read = 0;
}

/* Отправка сообщения на емайл */
if ($ank['email'] != NULL && $ank['id'] != $user->id){
$msgq = 'Вам ответили в тикете перейдите по ссылке: http://'. $_SERVER['HTTP_HOST'] .'/scripts/tickets/ticket.php?id='. $id;  
$headers .= "Content-Type: text/plain; charset=utf-8\n";  
mail($ank['email'], 'Ticket-'.$id.' Новый ответ на '. $_SERVER['HTTP_HOST'], $msgq, $headers); 
} 

mysql_query("INSERT INTO `tkomm` (`user`, `msg`, `time`, `ticket`) values ('$user->id', '$msg', '".time()."', '$id')");
mysql_query("UPDATE `tickets` SET `user_read` = '$user_read', `read` = '$read' WHERE `id` = '$id'");

echo $div->div_err.' Сообщение добавлено</div>';
header("Location: ticket.php?id=".$id);
}
if ($f['closed']==1)echo $div->div_err.'Тикет удален</div>';
else echo $div->div_err.'Если ваш вопрос был решен, то пожалуйста закройте тикет.</div>';

echo "<div class='post'><b>Тема</b>: ".html($f['name'])."<br /><b>Содержание</b>: ".output($f['text'])."<br />
<b>Автор</b>: <a href='/user$f[user]'>$ank[login]</a><br /><b>Время</b>: ".ptime($f['time'])."<br/>";
echo "Тип вопроса: ";
if ($f['type_of_question']==0) echo "<font color='red'>Общие вопросы</font><br/>\n";
if ($f['type_of_question']==1) echo "<font color='red'>Тех. причины</font><br/>\n";
if ($f['type_of_question']==2) echo "<font color='red'>Жалобы/Нарушения</font><br/>\n";
if ($f['type_of_question']==3) echo "<font color='red'>Домены</font><br/>\n";
if ($f['type_of_question']==4) echo "<font color='red'>Оплата</font><br/>\n";
if ($f['type_of_question']==5) echo "<font color='red'>Хостинг</font><br/>\n";
if ($f['type_of_question']==6) echo "<font color='red'>Реселлинг</font><br/>\n";
if ($f['type_of_question']==7) echo "<font color='red'>Dedicated</font><br/>\n";
if ($f['type_of_question']==8) echo "<font color='red'>Игровой хостинг</font><br/>\n";
echo "</div><hr>";

$q=mysql_query("SELECT * FROM `tkomm` WHERE `ticket` = '$id' ORDER BY `time` DESC");
while ($fq=mysql_fetch_assoc($q)){
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '".$fq['user']."'"));


if ($ank['position']=='admin')
{
$ank['login_o']='(Оператор)';
$img='<img src=\'admin.png\'>';
$reply='<b>Ответ</b>: Здравствуйте!<br />';
} else {
$ank['login_o']='';
$img='<img src=\'user.png\'>';
$reply='';
}
echo "<div class='post'>$img <a href='/user$fq[user]'>$ank[login]</a> $ank[login_o] ".ptime($fq['time'])." <br />
".$reply.output($fq['msg'])."<br>\n";
// Удаляем сообщение
if ($user->position == 'admin' || $user->position == 'mod')echo "<a href='/scripts/tickets/ticket.php?id=$id&delk=$fq[id]'>[<font color='red'>x</font> Удалить]</a>";
if ($fq['time']>time()-600 && $fq['user']==$user->id)echo "[ <a href='?id=".$f['id']."&amp;set=".$fq['id']."'>Изменить</a> <small>Осталось: ".($fq['time']+600-time())." сек</small>]\n";
echo '</div><hr>';
}

if ($f['closed']==0){
echo "<div class='post'><form action='?id=$id' method='POST'>Сообщение:<br><textarea name='msg'></textarea><br><input type='submit' value='Отправить'></form></div>";
echo "<div class='post'>.: <a href='?id=$id&closed=1'>Закрыть тикет</a> :.</div>";
} else echo "<div class='post'>.: <a href='?id=$id&open=1'>Открыть тикет</a> :.</div>";
echo "<div class='post'>.: <a href='/scripts/tickets'>Мои тикеты</a> :.</div>";
if ($user->position == 'admin' || $user->position == 'mod')
{
echo "<div class='post'>";
if ($f['adm_transferred']==0)echo "<a href='/scripts/tickets/ticket.php?id=$f[id]&act=transfer_the_administration_of_Chapter'>..:..:Передать Гл.Администраторам</a>";
else echo "<a href='/scripts/tickets/ticket.php?id=$f[id]&act=transfer_the_administration_of_Chapter_off'>..:..:Отменить передачу Гл.Администраторам</a>";
echo "</div>";
}
echo '</p></div></div></div>';
include_once '../../sys/foot.php';
?>