<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";

if (isset($submit))
	{
	if (strlen(trim($id))==0&&!isset($delete)) 
		{
		$err=true;
		} 
	else 
		{
		if (isset($delete))
			{
			@mysql_query("delete from ticket where id='".addslashes(trim($delete))."'");
			@mysql_query("delete from ticket_reply where id='".addslashes(trim($delete))."'");
			}
		else
			{
			$cont=true;
			}
		}
	}
if (isset($close))
	{
	@mysql_query("update ticket set status='1' where id='".addslashes(trim($close))."'");
	$closed=true;
	}
if (isset($submitx))
	{
	@mysql_query("insert into ticket_reply set id='".addslashes(trim($id))."', reply_status='1', reply='".addslashes(trim($tech_reply))."', ogcreate='".time()."'");
	
	@mysql_query("update ticket set switch='1' where id='".addslashes(trim($id))."'");
	
	tt_admin_reply($id);
	}

include $server_admin_inc."/header.php";

if (isset($cont))
	{
	# $rs0=mysql_fetch_row(mysql_query("select subject, message, status, uid, UNIX_TIMESTAMP(ogcreate) from ticket where id='".addslashes(trim($id))."'"));
	$rs0=mysql_fetch_row(mysql_query("select subject, message, status, uid, ogcreate from ticket where id='".addslashes(trim($id))."'"));
	$rs1=mysql_fetch_row(mysql_query("select email from user where uid='".addslashes(trim($rs0[3]))."'"));
	echo("
		<form action='".$PHP_SELF."' method='POST'>
		<input type='hidden' name='sid' value='".trim($sid)."'>
		<input type='hidden' name='id' value='".trim($id)."'>
		<input type='hidden' name='cont' value='5'>
		<input type='hidden' name='email' value='".trim($rs1[0])."'>
		<table width='725' cellpadding='4' cellspacing='1' border='0'>
			<tr>
				<td><font color='#990000'><b>Trouble Ticket System</b></font></td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='4'></td>
			</tr>
			<tr>
				<td><font color='#990000' size='1'>Tech Reply:</font></td>
			</tr>
			<tr>
				<td><textarea name='tech_reply' ".$input_style." rows='6' cols='85'></textarea></td>
			</tr>
			<tr>
				<td><input ".$button_style." type='submit' name='submitx' value='Reply to Client &gt&gt'></td>
			</tr>
			<tr>
				<td background='".$switch."/bar_bkg.gif'><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
			<tr>
				<td><font color='#990000'>Dialog:</font></td>
			</tr>
		");
	$ctd=mysql_fetch_row(mysql_query("select count(*) from ticket_reply where id='".addslashes(trim($id))."'"));
	if ($ctd[0]<=0)
		{
		echo("
			<tr>
				<td>No response to ticket yet.</td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='6'></td>
			</tr>
			");
		}
	else
		{
		$x=1;
		# $row3=mysql_query("select reply, reply_status, UNIX_TIMESTAMP(ogcreate) from ticket_reply where id='".addslashes(trim($id))."' order by rid desc");
		$row3=mysql_query("select reply, reply_status, ogcreate from ticket_reply where id='".addslashes(trim($id))."' order by rid desc");
		while ($rs3=mysql_fetch_row($row3))
			{
			if ($x==1) {$bgcolor="#ECEBF0"; $x=0;} else {$bgcolor="#E2E1E6"; $x=1;}
			echo("
				<tr>
					<td bgcolor='".$bgcolor."'>
						<table width='100%' cellpadding='0' cellspacing='0' border='0'>
							<tr>
								<td align='left' valign='top'>[Reply Submitted By] <font color='#990000'>".(($rs3[1]==1)?"Admin":"Client")."</font></td>
								<td align='right' valign='top'>[Reply Submitted On] <font color='#990000'>".date("m/d/Y h:i:s a", $rs3[2])."</font></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td bgcolor='".$bgcolor."'><font color='#000000'>".stripslashes(str_replace("\n", "<br>", $rs3[0]))."</font></td>
				</tr>
				<tr>
					<td><img src='".$switch."/the_space.gif' width='1' height='4'></td>
				</tr>
				");
			}
		}
	echo("
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='8'></td>
			</tr>
			<tr>
				<td align='left' valign='top' bgcolor='#ECEBF0'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td align='left' valign='top'>[Created] <font color='#990000'>".date("m/d/Y h:i:s a",$rs0[4])."</font></td>
							<td align='right' valign='top'>[Ticket] <font color='#990000'>#".trim($id)."</font></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td align='left' valign='top' bgcolor='#ECEBF0'>[Original Issue] <a href='mailto:".trim($rs1[0])."?subject=Tech Support'><font color='#990000'><b>".nl2br(trim(stripslashes($rs0[0])))."</b></font></a></td>
			</tr>
			<tr>
				<td bgcolor='#ECEBF0'><font color='#990000'>".nl2br(trim(stripslashes($rs0[1])))."</font></td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
			<tr>
				<td>[<a href='".$http_admin."/trouble_ticket_active.php?sid=".trim($sid)."'>go back</a>]</td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
		</table>
		");
	// End Main Process
	mysql_close($dblink);
	include $server_admin_inc."/footer.php";
	die();
	}
$ctd=mysql_fetch_row(mysql_query("select count(*) from ticket where status='0'"));
if ($ctd[0]<=0)
	{
	echo("
		<table width='725' cellpadding='2' cellspacing='0' border='0'>
			<tr>
				<td><font color='#990000'><b>Trouble Ticket System</b></font></td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
			<tr>
				<td>No open trouble tickets.</td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
			<tr>
				<td>[<a href='".$http_admin."/index2.php?sid=".trim($sid)."'>go back</a>]</td>
			</tr>
			<tr>
				<td><img src='".$switch."/the_space.gif' width='1' height='1'></td>
			</tr>
		</table>
	</form>
		");
	// End Main Process
	mysql_close($dblink);
	include $server_admin_inc."/footer.php";
	DIE();
	}
echo("
	<table width='725' cellpadding='2' cellspacing='0' border='0'>
		<tr>
			<td><img src='".$http_images."/menu_arrow.gif'><b>Trouble Ticket System</b></td>
		</tr>
		<form action='".$PHP_SELF."' method='POST'>
		<input type='hidden' name='sid' value='".trim($sid)."'>
		<tr>
			<td>
				<table width='100%' cellpadding='2' cellspacing='1' border='0'>
			");
		if (isset($err))
			{
			echo("
				<tr>
					<td colspan='5'><font color='#990000'><b>*</b> Please choose a ticket to view:</font></td>
				</tr>
				<tr>
					<td colspan='5'><img src='".$switch."/the_space.gif' width='1' height='8'></td>
				</tr>
				");
			}
	echo("
				<tr>
					<td bgcolor='#D9D9DD' align='center'><b>View</b></td>
					<td bgcolor='#D9D9DD' align='center'><b>User</b></td>
					<td bgcolor='#D9D9DD' align='center'><b>Issue</b></td>
					<td bgcolor='#D9D9DD' align='center'><b>Submitted</b></td>
					<td bgcolor='#D9D9DD' align='center'><font color='#990000'><b>Delete!</b></font></td>
					<td bgcolor='#D9D9DD' align='center'><font color='#990000'><b>Close!</b></font></td>
				</tr>
		");
$x=1;
# $row0=mysql_query("select id, uid, subject, UNIX_TIMESTAMP(ogcreate) from ticket where status='0'");
$row0=mysql_query("select id, uid, subject, ogcreate from ticket where status='0'");
while($rs0=mysql_fetch_row($row0))
	{
	if ($x==1) {$bgcolor="#ECEBF0"; $x=0;} else {$bgcolor="#E2E1E6"; $x=1;}
	$rs2=mysql_fetch_row(mysql_query("select first_name, last_name, sid from user where uid='".addslashes(trim($rs0[1]))."'"));
	echo("
		<tr>
			<td align='center' bgcolor='".$bgcolor."'><input type='radio' name='id' value='".$rs0[0]."'></td>
			<td align='center' bgcolor='".$bgcolor."'><a href='".$http_web."/client_area.php?sid=".$rs2[2]."' target='_blank'>".$rs2[0]." ".$rs2[1]."</a></td>
			<td align='center' bgcolor='".$bgcolor."'>".stripslashes($rs0[2])."</td>
			<td align='center' bgcolor='".$bgcolor."'><font color='#990000' size='1'>".date("m/d/Y h:i:s a", $rs0[3])."</font></td>
			<td align='center' bgcolor='".$bgcolor."'><a href='".$PHP_SELF."?sid=".trim($sid)."&delete=".$rs0[0]."&submit=5'><font color='#990000'>Delete Now</font></a></td>
			<td align='center' bgcolor='".$bgcolor."'><a href='".$PHP_SELF."?sid=".trim($sid)."&close=".$rs0[0]."'><font color='#990000'>Close Now</font></a></td>
		</tr>
		");
	}
echo("
				</table>
			</td>
		</tr>
		<tr>
			<td><input ".$button_style." type='submit' name='submit' value='View Ticket Now &gt&gt'></td>
		</tr>
	</table>
	</form>
	");
// End Main Process
mysql_close($dblink);
include $server_admin_inc."/footer.php";
?>