<h1>Тикеты</h1>
<?php
if(isset($_GET["delid"])){
	db_connect();
	$res = db_query("DELETE FROM `".DBpref."tickets` WHERE `id` = ".intval($_GET["delid"]));
	db_close();
	die("<h2>Удаленно</h2>");
}

if(isset($_GET["id"])){
	db_connect();
		if(isset($_POST["newok"])){
				$err = NULL;
				if($_POST["text"] == NULL)
					$err .= "<li>Введите текст</li>";
					
					
				if($err == NULL){
				db_query("INSERT INTO `".DBpref."tickets_mes` (`id_ticket`, `text`, `dat`, `sender`) VALUES (".intval($_GET["id"]).", '".db_string($_POST["text"])."', '".date("Y-m-d H:i:s")."', '".db_string($_SESSION["login"])."')");
				db_query("UPDATE `".DBpref."tickets` SET `answers` = `answers` + 1 WHERE `id` = ".intval($_GET["id"]));
				echo "
				<script type='text/javascript'>
					location.replace('?p=tickets&id=".intval($_GET["id"])."');
				</script>
			";
				}else{
					echo "<ul>".$err."</ul>";
				}
				
			}
	$res = db_query("SELECT *  FROM `".DBpref."tickets` WHERE `id` = ".intval($_GET["id"]));
	$row = db_assoc($res);
	echo "<strong>Категория:</strong> ".$ticketsCat[$row["category"]]."; <strong>Тема:</strong> ".htmlspecialchars($row["subject"])."; <strong>Дата создания:</strong> ".$row["dat"];
	echo '
		<form action="" method="post">
			<input name="recipient" type="hidden" size="30" maxlength="30" value="'.htmlspecialchars($_GET["h"]).'" /><br />
				Текст: <br />
				<textarea name="text" cols="30" rows="5">'.htmlspecialchars($_POST["text"]).'</textarea><br />
				<input name="newok" type="submit" value="Отправить" />
		</form>
		';
	$res2 = db_query("SELECT * FROM `".DBpref."tickets_mes` WHERE `id_ticket` = ".intval($_GET["id"])." ORDER BY `id` DESC");
	db_close();
	while($row2 = db_assoc($res2)){
		echo "<br />
		<strong>".htmlspecialchars($row2["sender"])."</strong>(".$row2["dat"]."):<br />
		".htmlspecialchars($row2["text"])."<br />
			";
	}
	exit();
}

db_connect();
$res = db_query("SELECT * FROM `".DBpref."tickets` ORDER BY `id` DESC");
db_close();
echo '
<table width="100%" border="1" cellspacing="0" cellpadding="0" class="table1">
  <tr>
    <td>Логин</td>
    <td>Категория</td>
    <td>Тема</td>
    <td>Время</td>
	<td></td>
  </tr>
';
while($row = db_assoc($res)){
echo '
  <tr>
    <td>'.htmlspecialchars($row["login"]).'</td>
    <td>'.$ticketsCat[$row["category"]].'</td>
    <td>'.htmlspecialchars($row["subject"]).'</td>
    <td>'.$row["dat"].'</td>
	<td><a href="?p=tickets&id='.$row["id"].'">Ответить</a> / <a href="?p=tickets&delid='.$row["id"].'">Удалить</a></td>
  </tr>
';
}
echo '</table>';
?>