<?php include('inc/header.php'); ?>
<h1>Ticketsystem - Backend</h1>
<div class="container">
	<script type="text/javascript">
        function toggleMe(a){var e=document.getElementById(a);
          if(!e)return true;
              if(e.style.display=="none"){e.style.display=""}
              else {e.style.display="none"}
          return true;
        }
	</script>
	<table width="100%" cellpadding="2" cellspacing="0">
<?php
$ticket_a = @odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
if (isset($_GET['edit'])) {
	$ticket_a = @odbc_fetch_array(@odbc_exec($odbc_connect, 'SELECT * FROM [TICKET_THREADS] WHERE [id] = '.$_GET['edit'].' ORDER BY [status] ASC, [time] DESC'));
	$ticket_answer_q = @odbc_exec($odbc_connect, 'SELECT [text], [time], [account] FROM [TICKET_POSTS] WHERE [tid] = '.$_GET['edit'].' ORDER BY [id] ASC');

	if(($ticket_a['status'] == 0 OR $ticket_a['status'] == 2) AND (empty($ticket_a['byadmin']) OR $ticket_a['byadmin'] == $_SESSION['user'])) {
		@odbc_exec($odbc_connect, 'UPDATE [TICKET_THREADS] SET [status] = 2, [modtime] = '.time().', [byadmin] = \''.$_SESSION['user'].'\', [byadminip] = \''.$_SERVER['REMOTE_ADDR'].'\' WHERE [id] = '.$ticket_a['id']);
		
			if (isset($_POST['answer']) OR isset($_POST['close'])) {
				if ($_POST['problem'] == '') {echo '<div class="fail">Du musst das Feld ausfüllen, um eine Antwort auf das Ticket abzusenden.</div>';}
				else {
					$problem = @mssql_escape_string(str_replace('script','',$_POST['problem']));
					$ip = $_SERVER['REMOTE_ADDR'];
					
					if (isset($_POST['answer'])) {$statusid = 1;} if (isset($_POST['close'])) {$statusid = 3;}
					@odbc_exec($odbc_connect, 'UPDATE [TICKET_THREADS] SET [status] = '.$statusid.', [byadmin] = \''.$_SESSION['user'].'\', [byadminip] = \''.$ip.'\', [modtime] = '.time().' WHERE [id] = '.$ticket_a['id']);
					@odbc_exec($odbc_connect, 'INSERT INTO [TICKET_POSTS] ([tid], [account], [text], [ip], [time]) VALUES ('.$ticket_a['id'].', \''.$_SESSION['user'].'\', \''.$problem.'\', \''.$ip.'\', \''.time().'\')');
					print '<meta http-equiv="refresh" content="0; URL=ticketsys.php">';
				}
			}
echo '		<tr>
				<td>
					<table><tr>
						<td><b>'.$ticket_a['account'].'</b> übermittelte am <b>'.date('d.m.Y', $ticket_a['time']).' um '.date('H:i', $ticket_a['time']).'</b> Uhr folgendes Ticket:
					</tr></table>
					<table width="100%" style="border-bottom: #707070 1px dotted; padding:10px 0px 10px 20px;">
						<tr><td><b>'.$ticket_a['title'].'</b></td></tr>
						<tr><td>'.nl2br(html_entity_decode($ticket_a['text'])).'</td></tr>
					</table>';
		if (@odbc_num_rows($ticket_answer_q) > 0) {
			while ($ticket_answer_a = @odbc_fetch_array($ticket_answer_q)) {
				if ($ticket_answer_a['account'] == $_SESSION['user']) {$ticket_answer_a['account'] = 'Du';} else {$ticket_answer_a['account'] = $ticket_answer_a['account'];}
			
			echo '	<table width="100%" style="padding:10px 0px 10px 20px; border-bottom: #707070 1px dotted;">
						<tr><td><b>'.$ticket_answer_a['account'].':</b></td><td align="right"><i>'.date('d.m.Y', $ticket_answer_a['time']).', '.date('H:i', $ticket_answer_a['time']).' Uhr</i></td></tr>
						<tr><td style="padding-left:30px;" colspan="2">'.nl2br(html_entity_decode($ticket_answer_a['text'])).'</td></tr>
					</table>';
			}
		}
echo '			</td>
			</tr>
			<tr>
				<td width="100%" align="center" valign="top">
					<form method="post"><br />
						<textarea name="problem" cols="90" rows="5" style="width:100%; height:300px;"></textarea>
						<br /><input style="margin-top:5px;" type="submit" name="answer" value="Antworten und Ticket geöffnet lassen"/> <input style="margin-top:5px;" type="submit" name="close" value="Antworten und Ticket schließen"/>
					</form>
				</td>
			</tr>';
	} else {print '<meta http-equiv="refresh" content="0; URL=ticketsys.php">';}	
} 
else {
	if(isset($_GET['new']))		{echo '<a href="ticketsys.php">Normale Ansicht</a> | <b>Nur neue Tickets</b>'; $ticket_q = @odbc_exec($odbc_connect, 'SELECT * FROM [TICKET_THREADS] WHERE ([byadmin] is NULL OR [byadmin] = \''.$_SESSION['user'].'\') AND [status] = 0 ORDER BY [status] ASC, [time] DESC, [modtime] DESC');}
	else						{echo '<b>Normale Ansicht</b> | <a href="ticketsys.php?new">Nur neue Tickets</a>'; $ticket_q = @odbc_exec($odbc_connect, 'SELECT * FROM [TICKET_THREADS] WHERE ([byadmin] is NULL OR [byadmin] = \''.$_SESSION['user'].'\') AND [status] < 3 ORDER BY [status] ASC, [time] DESC, [modtime] DESC');}

	echo '	<tr><td colspan="5"><h2>Tickets</h2></td></tr>
		<tr>
			<td valign="top" align="left">Account</td>
			<td width="30%" align="left">Betreff</td>
			<td valign="top" align="left">IP Addresse</td>
			<td valign="top" align="center">Letztes Update</td>
			<td valign="top" align="center">Status</td>
			<td valign="top" align="center"></td>
		</tr>';

	while ($ticket_a = @odbc_fetch_array($ticket_q)) {
		$ticket_answer_q = @odbc_exec($odbc_connect, 'SELECT [text], [time], [account] FROM [TICKET_POSTS] WHERE [tid] = '.$ticket_a['id'].' ORDER BY [id] ASC');
	
		if (!empty($_GET['close']) AND $_GET['close'] == $ticket_a['id']) {
			$id = @odbc_fetch_array(@odbc_exec($odbc_connect, 'SELECT [id] FROM [TICKET_POSTS] order by [id] DESC')); $id = $id['id']+1; $ip = $_SERVER['REMOTE_ADDR']; $time = time(); $problem = 'Das Ticket wurde vom Team geschlossen. Solltest du das Gefühl haben, dass dein Problem dennoch nicht gelöst ist, so eröffne bitte ein neues Ticket.';
			@odbc_exec($odbc_connect, 'UPDATE [TICKET_THREADS] SET [status] = 3, [byadmin] = \''.$_SESSION['user'].'\', [byadminip] = \''.$ip.'\', [modtime] = '.time().' WHERE [id] = '.$ticket_a['id'].'');
			@odbc_exec($odbc_connect, 'INSERT INTO [TICKET_POSTS] ([tid], [id], [account], [text], [ip], [time]) VALUES ('.$ticket_a['id'].', '.$id.', \''.$_SESSION['user'].'\', \''.$problem.'\', \''.$ip.'\', '.$time.')');
			print '<meta http-equiv="refresh" content="0; URL=ticketsys.php">';
		}
	
		$schloss = ''; $bearbeitung = ''; $onclick = "return toggleMe('".$ticket_a['id']."')";
		
		if ($ticket_a['status'] == 0) {$status = '<font class="red">Offen</font>';$schloss = '<a href="ticketsys.php?close='.$ticket_a['id'].'"><img src="/images/icons/misc/schloss.png" title="Ticket schließen"/></a>'; $bearbeitung = '<a href="ticketsys.php?edit='.$ticket_a['id'].'"><img src="../images/edit.png" title="Ticket bearbeiten"/></a>';}
		elseif ($ticket_a['status'] == 1) {$status = 'Auf Antwort wartend';} 
		elseif ($ticket_a['status'] == 2) {$status = '<font class="red blink">Von dir in Bearbeitung</font>'; $bearbeitung = '<a href="ticketsys.php?edit='.$ticket_a['id'].'"><img src="../images/edit.png" title="Ticket bearbeiten"/></a>';} 
		elseif ($ticket_a['status'] == 3) {$status = '<i>Geschlossen</i>';}
	
	echo '	<tr onclick="'.$onclick.'" style="height:27px;" class="row">
				<td width="12%" align="left" style="border-top: #c0c0c0 1px solid; cursor:pointer;">'.$ticket_a['account'].'</td>
				<td width="48%" align="left" style="border-top: #c0c0c0 1px solid; font-weight:bold; cursor:pointer;">'.$ticket_a['title'].'</td>
				<td width="10%" align="left" style="border-top: #c0c0c0 1px solid; cursor:pointer;">'.$ticket_a['ip'].'</td>
				<td width="10%" align="center" style="border-top: #c0c0c0 1px solid; font-weight:bold; cursor:pointer;">'.date('d.m.Y, H:i', $ticket_a['time']).' Uhr</td>
				<td width="10%" align="center" style="border-top: #c0c0c0 1px solid; font-weight:bold; cursor:pointer;">'.$status.'</td>
				<td width="10%" align="center" style="border-top: #c0c0c0 1px solid; font-weight:bold;">'.$bearbeitung.' '.$schloss.'</td>
			</tr>
			<tr style="display:none;" id="'.$ticket_a['id'].'">
				<td></td>
				<td colspan="3" valign="top" style="font-size:11px;">
					<table width="100%" style="border-top: #707070 1px dotted; padding:10px 0px;">
						<tr><td><i>'.nl2br(html_entity_decode($ticket_a['text'])).'</i></td></tr>
					</table>';
					
		if (@odbc_num_rows($ticket_answer_q) > 0) {
			while ($ticket_answer_a = @odbc_fetch_array($ticket_answer_q)) {
				if ($ticket_answer_a['account'] == $_SESSION['user']) {$ticket_answer_a['account'] = 'von dir';} else {$ticket_answer_a['account'] = 'von <b>'.$ticket_answer_a['account'].'</b>';}
			
			echo '	<table width="100%" style="padding:10px 0px; border-top: #707070 1px dotted;">
						<tr><td><b>'.date("d.m.Y", $ticket_answer_a['time']).'</b>, <b>'.date("H:i", $ticket_answer_a['time']).' Uhr</b> -  Antwort '.$ticket_answer_a['account'].':</td></tr>
						<tr><td style="padding-left:30px;"><i>'.nl2br(html_entity_decode($ticket_answer_a['text'])).'</i></td></tr>
					</table>';
			}
		}
	echo '										<br /><br /><br /><br />
											</td>
										</tr>';
	}
}
	echo '							</table>
								</td>
							</tr>'; 
?>
</div>
<?php include('inc/footer.php'); ?>