<?php
// ************************************* //
// * Предварительная обработка         * //
// ************************************* //


$IN_ENGINE = true;
require_once("init.php");
if ($logstatus != 3) header("Location: index.php");
require_once("includes/functions.php");
include("includes/WebIcqPro.class.php");
$icq = new WebIcqPro();
	


// ************************************* //
// * Применение изменений              * //
// ************************************* //

if (isset($_GET['newticket'])) {

if (isset($_POST['addticket'])) {
	if (empty($_POST['title']) || empty($_POST['text']) || empty($_POST['category'])) {
		header("Location: ?newticket&status=fill");
		die;
	}
	
	$icq->connect('640323795', 'LOXYut8e');
	$admins=array('65642077','701299');
	$text='New Ticket';
	foreach ($admins as $subscriber)
	{
	$result=$icq->sendMessage($subscriber, $text);
	sleep(2);
	}
	$icq->disconnect();
	
	//$icq->sendMessage('65642077', 'New Ticket');
	//$icq->sendMessage('569024925', 'New Ticket');
	//$icq->sendMessage('701299', 'New Ticket');
	//$icq->sendMessage('607741831', 'New Ticket');
	//$icq->sendMessage('479970100', 'New Ticket');
	//$icq->sendMessage('7576939', 'New Ticket');
	//$icq->disconnect();
	//echo $icq->error;
	$query = mysql_query("INSERT INTO dgh_tickets VALUES ('', '".$_SESSION['login']."', '".$_POST['title']."', '".$_POST['category']."', '".time()."', '1', '0', '0', '')");
	$query2 = mysql_query("INSERT INTO dgh_tickets_posts VALUES ('', 'user', '".mysql_insert_id()."', '".addslashes($_POST['text'])."', '".time()."', '".$_SESSION['login']."')");
	
	if ($query && $query2) header("Location: ?status=ok");
}


// ************************************* //
// * Добавление тикета                 * //
// ************************************* //


require_once("template/header.php");

if (isset($_GET['status']) && $_GET['status'] == "fill") {
	$result = "Не все поля заполнены, тикет не добавлен";
	include("template/query_result.php");
}

echo "<table cellpadding='0' cellspacing='0' border='0' class='cont_header_txt'><tr><td><a href='tickets.php'>Вернуться назад</a></td></tr></table>";

$content .= "<form action='' method='POST'>";
$content .= "<table cellpadding='5' cellspacing='0' width='100%'>";
$content .= "<tr><td width='20%'><b>Заголовок:</b></td><td><input class='field' type='text' name='title'></td></tr>\n";

$options = array();
$content .= "<tr><td width='20%'><b>Категория:</b></td><td>";
$content .= "<select class='field' name='category'>";

$options[''] = "Выберите категорию";
$query = mysql_query("SELECT * FROM dgh_tickets_categories");
while($result = mysql_fetch_array($query)) {
	$options[$result['cid']] = $result['name'];
}
$content .= showoptions($options, false);

$content .= "</select>";
$content .= "</td></tr>\n";

$content .= "<tr><td width='20%'><b>Сообщение:</b></td><td><textarea class='field' rows='10' name='text'></textarea></td></tr>\n";

$content .= "<tr><td colspan='2'><input class='button' type='submit' name='addticket' value='Добавить'></td></tr>\n";
$content .= "</table>";
	
$title = "Добавление тикета";
include("template/table_content.php");

} elseif (isset($_GET['id'])) {


// ************************************* //
// * Редактирование сообщения          * //
// ************************************* //


if (isset($_GET['pid']) && isset($_GET['act']) && $_GET['act'] == "edit") {

$query = mysql_query("SELECT * FROM dgh_tickets JOIN dgh_tickets_posts WHERE tid = '".$_GET['id']."' AND pid = '".$_GET['pid']."'");
$query = mysql_fetch_array($query);

if ($query['author'] != "user" || $query['status'] != "1" || $query['account'] != $_SESSION['login']) header("Location: ?id=".$_GET['id']);

if (isset($_POST['edit'])) {
	if (!empty($_POST['text'])) {
		$query = mysql_query("UPDATE dgh_tickets_posts SET text = '".addslashes($_POST['text'])."' WHERE pid = '".$_GET['pid']."'");
		$query2 = mysql_query("UPDATE dgh_tickets SET date = '".time()."' WHERE tid = '".$_GET['id']."'");
		
		if ($query && $query2) header("Location: ?id=".$_GET['id']."&status=edited");
	}
}


require_once("template/header.php");
echo "<table cellpadding='0' cellspacing='0' border='0' class='cont_header_txt'><tr><td><a href='tickets.php?id=".$_GET['id']."'>Вернуться назад</a></td></tr></table>";

$query = mysql_query("SELECT * FROM dgh_tickets_posts WHERE pid = '".$_GET['pid']."'");
$query = mysql_fetch_array($query);

$content .= "<table cellpadding='5' cellspacing='0' width='100%'>";
$content .= "<tr><td><form action='' method='POST'><textarea class='field' name='text' rows='7' style='width: 50%'>".stripslashes($query['text'])."</textarea></td></tr>";
$content .= "<tr><td><input type='submit' name='edit' value='Сохранить изменения'></form></td></tr>";
$content .= "</table>";


$title = "Редактирование сообщения";
$style_padding = 6;
include("template/table_content.php");


} else {


// ************************************* //
// * Просмотр тикета                   * //
// ************************************* //


if (isset($_POST['anwser'])) {
	if (empty($_POST['text'])) {
		header("Location: ?id=".$_GET['id']."&status=fill");
		die;
	}
	
	$query = mysql_query("INSERT INTO dgh_tickets_posts VALUES ('', 'user', '".$_GET['id']."', '".addslashes($_POST['text'])."', '".time()."', '".$_SESSION['login']."')");
	$query2 = mysql_query("UPDATE dgh_tickets SET date = '".time()."' WHERE tid = '".$_GET['id']."'");
	
	if ($query && $query2) header("Location: ?id=".$_GET['id']);
}

$query = mysql_query("SELECT user, new, status FROM dgh_tickets WHERE tid = '".$_GET['id']."'");
$query = mysql_fetch_array($query);

if ($query['user'] != $_SESSION['login']) header("Location: tickets.php");
$status = $query['status'];

if ($query['new'] == "1") {
	mysql_query("UPDATE dgh_tickets SET new = '0' WHERE tid = '".$_GET['id']."'");
}

require_once("template/header.php");

if (isset($_GET['status']) && $_GET['status'] == "fill") {
	$result = "Не заполнено поле сообщения";
	include("template/query_result.php");
}

if (isset($_GET['status']) && $_GET['status'] == "edited") {
	$result = "Сообщение успешно изменено";
	include("template/query_result.php");
}

echo "<table cellpadding='0' cellspacing='0' border='0' class='cont_header_txt'><tr><td><a href='tickets.php'>Вернуться назад</a></td></tr></table>";

$content .= "<table cellpadding='5' cellspacing='0' width='100%'>";

$i = 1;
$query = mysql_query("SELECT * FROM dgh_tickets_posts WHERE ticket = '".$_GET['id']."' ORDER BY date");
while($result = mysql_fetch_array($query)) {
	if ($result['author'] == "user") { 
		$author = "Ваше сообщение";
		$class = "user";
	} else { 
		$author = "Техническая поддержка";
		$class = "admin";
	}
	
	$result['text'] = str_replace("\n", "<br>", $result['text']);
	
	if ($result['author'] == "user" && $status == "1") { $result['author'] = "<a href='?id=".$_GET['id']."&act=edit&pid=".$result['pid']."'>изменить</a> | "; } else { $result['author'] = ""; }
	
	$content .= "<tr><td class='title".$class."'><b>#".$i." ".$author.":</b></td><td class='title".$class."' align='right'>".$result['author'].date("d.m.Y - H:i:s", $result['date'])."</td></tr>";
	$content .= "<tr><td class='message".$class."' colspan='2'>".stripslashes($result['text'])."</td></tr>\n";
	$i++;
}

$query = mysql_query("SELECT title, status FROM dgh_tickets WHERE tid = '".$_GET['id']."'");
$query = mysql_fetch_array($query);

if ($query['status'] == "1") {
$content .= "<tr><td align='center' colspan='2'><form action='' method='POST'><br><textarea class='field' rows='7' name='text' style='width: 50%'></textarea></td></tr>";
$content .= "<tr><td align='center' colspan='2'><input class='button' type='submit' name='anwser' value='Добавить сообщение'></form></td></tr>\n";
} else {
$content .= "<tr><td align='center' colspan='2'><br><input class='button' type='submit' value='Тикет закрыт' disabled></td></tr>";
}

$content .= "</table>";

$title = $query['title'];
$style_padding = 6;
include("template/table_content.php");

}

} else {


// ************************************* //
// * Просмотр тикетов                  * //
// ************************************* //


require_once("template/header.php");

if (isset($_GET['status']) && $_GET['status'] == "ok") {
	$result = "Тикет успешно создан";
	include("template/query_result.php");
}

echo "<table cellpadding='0' cellspacing='0' border='0' class='cont_header_txt'><tr><td>Техническая поддержка</td></tr></table>";

$content = "";

$content .= "<table cellpadding='5' cellspacing='0' width='100%'>";

$content .= "<tr>\n";
$content .= "<td width='40%' class='tabletitle'><b>Заголовок</b></td>\n";
$content .= "<td width='25%' class='tabletitle'><b>Категория</b></td>\n";
$content .= "<td width='25%' class='tabletitle'><b>Последнее обновление</b></td>\n";
$content .= "<td width='10%' class='tabletitle'><b>Статус</b></td>\n";
$content .= "</tr>";

$query = mysql_query("SELECT * FROM dgh_tickets JOIN dgh_tickets_categories WHERE user = '".$_SESSION['login']."' AND category = cid ORDER BY status DESC, date DESC");

$i = 0;
while($result = mysql_fetch_array($query)) {
	if ($result['status'] == "1") { $result['status'] = "Открытый"; } else { $result['status'] = "Закрытый"; }
	if ($result['new'] == "1") { $result['new'] = "(обновлено)"; } else { $result['new'] = ""; }
	
	$content .= "<tr>";
	$content .= "<td class='table'><a href='?id=".$result['tid']."'>".$result['title']."</a> ".$result['new']."</td>";
	$content .= "<td class='table'>".$result['name']."</td>";
	$content .= "<td class='table'>".date("d.m.Y - H:i:s", $result['date'])."</td>";
	$content .= "<td class='table'>".$result['status']."</td>";
	$content .= "</tr>";
	$i++;
}

if ($i == 0) $content .= "<td colspan='4' align='center'>Нет заявок в техническую поддержку</td>";

$content .= "<tr>";
$content .= "<td colspan='4' align='center'><form action='?newticket' method='POST'><input class='button' type='submit' name='newticket' value='Задать вопрос'></form></td>";
$content .= "</tr>";

$content .= "</table>";

$style_padding = 0;
include("template/table_content.php");
}

require_once("template/footer.php");
?>
