<?php
/**
* @version 1.0.0
* @package RStickets! 1.0.0
* @copyright (C) 2007-2008 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// DIE()
defined('_JEXEC') or die('Restricted access');

if (!rst_is_logged()) rst_redirect('index.php?option=com_rstickets&page=tickets');

$ticket = rst_get_ticket_messages($_GET['id']);
if (!isset($ticket['TicketId']))
	rst_redirect('index.php?option=com_rstickets&page=tickets');

$document =& JFactory::getDocument();
$oldtitle = $document->getTitle();
$document->setTitle($oldtitle.' - '.$ticket['TicketSubject']);

$ticketdepartment = rst_get_departments($ticket['DepartmentId']);
if (rst_is_staff_logged())
{
	if ($_SESSION['RST_User']['Group']['CanSeeUnallocatedTickets'] == 0 && $ticket['UserId'] == '0' && $ticket['CustomerId'] != $_SESSION['RST_User']['UserId'])
		rst_redirect('index.php?option=com_rstickets&page=tickets');
	if ($_SESSION['RST_User']['Group']['CanSeeOtherTickets'] == 0 && $ticket['UserId'] > 0 && $ticket['UserId'] != $_SESSION['RST_User']['UserId'] && $ticket['UserId'] != $_SESSION['RST_User']['UserId'])
		rst_redirect('index.php?option=com_rstickets&page=tickets');
}
else
	if ($ticket['CustomerId'] != $_SESSION['RST_User']['UserId'])
		rst_redirect('index.php?option=com_rstickets');
?>
<style type="text/css">.custom_fields { display: table-row !important; _display: block !important; @display: block !important; }</style>
<h4 class="rst_h4"><?php echo LBL_VIEWING_TICKET; ?> <span><?php echo $ticket['TicketCode']; ?></span> <?php if (!isset($_GET['print'])) { ?><a href="<?php echo JRoute::_('index2.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&print=1'); ?>" target="_blank"><?php echo LBL_PRINTABLE_VERSION; ?></a><?php } else { ?> <a href="javascript:window.print();" class="printIcon"><?php echo LBL_PRINT; ?></a><?php } ?></h4>
<?php
if (isset($_SESSION['RST_User']['Group']['CanMoveTicket']) && $_SESSION['RST_User']['Group']['CanMoveTicket'] == 1)
{
	$departments = rst_get_departments();
	$departments[] = array('DepartmentId' => 0, 'DepartmentName' => LBL_UNASSIGNED_DEPARTMENT, 'DepartmentPrefix' => 'UNASSIGNED', 'TicketAssignmentType' => 'static', 'TicketGenerationRule' => 'random', 'UserEmailTicket' => 0, 'StaffEmailTicket' => 0, 'FileUpload' => 'no');
	echo '<form method="post" action="'.JRoute::_('index.php?option=com_rstickets&page=move_ticket&id='.$ticket['TicketId']).'" id="rst_move">
	'.LBL_MOVE_TICKET.' <select name="NewDepartmentId">';
	if (count($departments) > 0)
		foreach ($departments as $department)
			 echo '<option '.($ticket['DepartmentId'] == $department['DepartmentId'] ? 'selected="selected"' : '').' value="'.$department['DepartmentId'].'">'.$department['DepartmentName'].'</option>';
	echo '</select>
	<button type="submit">'.BTN_MOVE_TICKET.'</button>
	</form>';
}

if (rst_is_staff_logged() && ($_SESSION['RST_User']['Group']['CanAssignTickets'] == 1 || $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1 || $_SESSION['RST_User']['Group']['CanChangeTicketStatus'] == 1))
	echo '<form method="post" action="'.JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=update_ticket').'" id="rst_update">';
?>
<table cellpadding="4" cellspacing="0" class="tableGrey wide">
	<thead>
	<tr class="tableHead">
		<td class="tdGrey"><?php echo LBL_SUBJECT; ?></td>
		<td class="tdGrey"><?php echo LBL_STAFF; ?></td>
		<td class="tdGrey"><?php echo LBL_STATUS; ?></td>
		<td class="tdGrey"><?php echo LBL_PRIORITY; ?></td>
	</tr>
	</thead>
	<tbody>
	<tr>
		<td class="tdGrey tdBigInput">
		<?php
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1 && !isset($_GET['print']))
			echo '<input type="text" name="TicketSubject" value="'.$ticket['TicketSubject'].'" />';
		else
			echo $ticket['TicketSubject'];
		?>
		</td>
		<td class="tdGrey">
		<?php
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanAssignTickets'] == 1 && !isset($_GET['print']))
		{
			echo '<select name="UserId"><option value="0">'.LBL_UNASSIGNED.'</option>';
			$users = rst_get_staff(0,array($ticket['DepartmentId']),array('CanAnswerTicket'));
			if (count($users) > 0)
				foreach ($users as $user)
					echo '<option '.($ticket['UserId'] == $user['UserId'] ? 'selected="selected"' : '').' value="'.$user['UserId'].'">'.$user['Username'].' ('.$user['Fullname'].')</option>';
			echo '</select>';
		}
		else
			echo $ticket['Fullname'];
		?>
		</td>
		<td class="tdGrey">
		<?php
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanChangeTicketStatus'] == 1 && !isset($_GET['print']))
		{
			echo '<select name="TicketStatus" style="width: 60px;">';
			$status_array = array('open','on-hold','closed');
			foreach ($status_array as $status)
				echo '<option '.($ticket['TicketStatus'] == $status ? 'selected="selected"' : '').' value="'.$status.'">'.$status_array_lang[$status].'</option>';
			echo '</select>';
		}
		else
			echo '<strong>'.$status_array_lang[$ticket['TicketStatus']].'</strong>';
		?>
		</td>
		<td class="tdGrey">
		<?php
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1 && !isset($_GET['print']))
		{
			echo '<select name="TicketPriority" style="width: 60px;">';
			$priority_array = array('low','normal','high');
			foreach ($priority_array as $priority)
				echo '<option '.($ticket['TicketPriority'] == $priority ? 'selected="selected"' : '').' value="'.$priority.'">'.$priority_array_lang[$priority].'</option>';
			echo '</select>';
		}
		else
			echo $priority_array_lang[$ticket['TicketPriority']];
		?>
		</td>
		</tr>
		<tr>
		<td colspan="4" class="tdGrey">
		<?php
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1)
			echo '<button type="submit">'.BTN_UPDATE.'</button> ';
		if ($ticket['TicketStatus'] != 'closed')
		{
		if ((rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanChangeTicketStatus'] == 1) || (!rst_is_staff_logged() && $ticket['CustomerId'] == $_SESSION['RST_User']['UserId']))
			echo '<a href="'.JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=close_ticket').'" onclick="return (confirm(\''.LBL_SURE.'\'))" class="closeButton">'.BTN_CLOSE.'</a> ';
		}
		if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanDeleteTicket'] == 1)
			echo '<a href="'.JRoute::_('index.php?option=com_rstickets&page=tickets&action=delete_ticket&id='.$ticket['TicketId']).'" onclick="return (confirm(\''.LBL_SURE.'\'))" class="deleteButton">'.BTN_DELETE.'</a> ';
		$notified = rst_is_notified($ticket['TicketId']);
		$overdued = rst_is_overdue($ticket['TicketId']);
		if (rst_is_staff_logged() && !$notified && $overdued)
			echo '<a href="'.JRoute::_('index.php?option=com_rstickets&page=ticket&action=notify_customer&id='.$ticket['TicketId']).'" class="notifyButton">'.BTN_NOTIFY.'</a>';
		?>
		</td>
		</tr>
	</tbody>
</table>

<?php
if (rst_is_staff_logged() && ($_SESSION['RST_User']['Group']['CanAssignTickets'] == 1 || $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1 || $_SESSION['RST_User']['Group']['CanChangeTicketStatus'] == 1))
	echo '</form>';

if (rst_is_staff_logged() && strlen($ticket['TicketInfo']) > 0) { ?>
<table cellpadding="4" cellspacing="0" class="tableGrey wide">
<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_SUBMITTER_INFO; ?></td>
	</tr>
	</thead>
	<tbody>
	<tr>
	<td><?php echo rst_replace_placeholder(array('{agent}','{referer}','{ip}','{logged}','{yes}','{no}'),array(LBL_USER_AGENT,LBL_HTTP_REFERER,LBL_REMOTE_ADDR,LBL_WAS_LOGGED,LBL_YES,LBL_NO),$ticket['TicketInfo']); ?></td>
	</tr>
	</tbody>
</table>
<?php }

$custom_fields = rst_get_ticket_custom_fields($ticket['TicketId']);

if (count($custom_fields) > 0)
{
if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1)
	echo '<form method="post" action="'.JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=update_ticket_custom_fields').'">';
?>
<table cellpadding="4" cellspacing="0" class="tableGrey wide">
	<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_CUSTOM_FIELDS; ?></td>
	</tr>
	</thead>
	<tbody>
<?php
	if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1)
	{
		foreach ($custom_fields as $key => $custom_field)
		{
			if (is_array($custom_field['CustomFieldValues']) && count($custom_field['CustomFieldValues']) == 1)
				$_POST['rstcf_'.$key] = $custom_field['CustomFieldValues'][0];
			elseif (is_array($custom_field['CustomFieldValues']) && count($custom_field['CustomFieldValues']) > 1)
				$_POST['rstcf_'.$key] = $custom_field['CustomFieldValues'];
		}
		echo rst_gen_custom_fields($ticket['DepartmentId']);
	}
	else
	{
		foreach ($custom_fields as $custom_field)
			echo '<tr>
			<td>'.$custom_field['CustomFieldLabel'].'</td>
			<td>'.implode('<br />',$custom_field['CustomFieldValues']).'</td>
			</tr>';
	}
?>
	</tbody>
</table>
<?php
if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanUpdateTicket'] == 1)
	echo '<button type="submit">'.BTN_UPDATE.'</button></form>';
}
?>

<?php
if ($ticket['TicketStatus'] != 'closed' && ((isset($_SESSION['RST_User']['UserId']) && !isset($_SESSION['RST_User']['Group']['GroupId'])) || (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanAnswerTicket'] == 1)))
{
	if (!isset($_POST['TicketMessage']) || $_GET['action'] != 'add_ticket_reply')
	{
?>
<br /><button type="button" onclick="document.getElementById('divreply').style.display=''; this.style.display='none'; return false;"><?php echo BTN_CLICK_REPLY; ?></button>
<?php
	}
?>
<form method="post" action="<?php echo JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=add_ticket_reply'); ?>" onsubmit="return validate_reply(this);" enctype="multipart/form-data" id="divreply" style="<?php echo isset($_POST['TicketMessage']) && $_GET['action'] == 'add_ticket_reply' ? '' : 'display: none' ?>">
<table cellpadding="4" cellspacing="0" class="tableGrey tableFullWidth">
	<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_ADD_NEW_REPLY; ?></td>
	</tr>
	</thead>
	<tbody>
	<tr>
		<td class="tdGrey tdBigWidth alignRight" valign="top"><?php echo LBL_MESSAGE; ?></td>
		<td><textarea name="TicketMessage" rows="5" cols="20" class="rst_textareaFullWidth" tabindex="1"><?php echo isset($_POST['TicketMessage']) && $_GET['action'] == 'add_ticket_reply' ? strip_tags(stripslashes($_POST['TicketMessage'])) : '' ?></textarea></td>
	</tr>
	<?php
	$db = JFactory::getDBO();
	$db->setQuery("SELECT `FileUpload` FROM `#__rstickets_departments` WHERE `DepartmentId`='".$ticket['DepartmentId']."' LIMIT 1");
	$file_upload = $db->loadResult();
	if ($file_upload != 'no') {
	?>
	<tr>
		<td class="tdGrey tdBigWidth alignRight" valign="top"><?php echo LBL_ATTACHMENT; ?></td>
		<td><input type="file" name="FileName" tabindex="3" /><br />
		<?php
		echo LBL_ATTACHMENT_EXT;
		$ext = '*';
		if (count($ticketdepartment[0]['FileUploadExtensions']) == 1 && $ticketdepartment[0]['FileUploadExtensions'][0] == '') $ext = '*';
		if (count($ticketdepartment[0]['FileUploadExtensions']) > 0 && $ticketdepartment[0]['FileUploadExtensions'][0] != '') $ext = implode(', ',$ticketdepartment[0]['FileUploadExtensions']);
		echo $ext;
		?></td>
	</tr>
	<?php }	?>
	<tr>
		<td class="tdGrey tdBigWidth">&nbsp;</td>
		<td><button type="submit" tabindex="2"><?php echo BTN_SUBMIT; ?></button></td>
	</tr>
	</tbody>
</table>
<input type="hidden" name="TicketId" value="<?php echo $ticket['TicketId']; ?>" />
</form>
<?php
}
else
	echo '<em>'.LBL_REPLIES_CLOSED.'</em><br/>';
?>

<table cellpadding="4" cellspacing="0" class="tableGrey tableFullWidth">
	<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_MESSAGES_HISTORY; ?></td>
	</tr>
	</thead>
	<tbody>
	<?php
	if (isset($ticket['messages']) && count($ticket['messages']) > 0)
		foreach ($ticket['messages'] as $key => $ticketmessage)
		{
			echo '
			<tr class="'.($key % 2 == 0 ? 'trEven' : 'trOdd').'">
				<td class="tdGrey tdBigWidth">
				<strong><a class="searchButton" title="'.BTN_SEARCH.'" href="#" onclick="document.getElementById(\'filterContainer\').'.($ticketmessage['UserId'] == $ticket['CustomerId'] ? 'CustomerUsername' : 'StaffUsername').'.value=\''.$ticketmessage['Username'].'\'; document.getElementById(\'filterContainer\').submit();return false;"> </a>'.(!empty($T_SETTINGS['RStickets_Custom_User_Link']) ? '<a href="'.rst_replace_placeholder('{userid}',$ticketmessage['UserId'],$T_SETTINGS['RStickets_Custom_User_Link']).'">'.$ticketmessage['Fullname'].'</a>' : $ticketmessage['Fullname']).'</strong> '.LBL_WROTE.'<br />'
				.($_SESSION['RST_User']['UserId'] != $ticket['CustomerId'] ? $ticketmessage['Email'].'<br />' : '').
				'<em>'.rst_full_date($ticketmessage['TicketTime']).'</em>
				</td>
				<td>
				'.(isset($_SESSION['RST_User']['Group']['CanUpdateTicketReplies']) && $ticketmessage['UserId'] == $_SESSION['RST_User']['UserId'] && $_SESSION['RST_User']['Group']['CanUpdateTicketReplies'] == 1 && !isset($_GET['print']) ? '<form method="post" action="'.JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=update_ticket_reply&rid='.$ticketmessage['TicketMessageId']).'" style="display:inline" /><input type="hidden" name="TicketMessageId" value="'.$ticketmessage['TicketMessageId'].'" /><textarea name="TicketMessage" rows="5" cols="20" style="width: 450px;">'.strip_tags($ticketmessage['TicketMessage']).'</textarea>' : nl2br($ticketmessage['TicketMessage'])).($ticketmessage['FileName'] != '' ? '<br/>---<br/><img class="rst_img" src="'.RST_FRONTEND_REL_PATH.'/img/attach.gif" alt="Attachment" /><a href="'.RST_FRONTEND_REL_PATH.'/files/'.$ticketmessage['FileName'].'">'.BTN_CLICK_ATTACHMENT.'</a>' : '').'<br/>'
				.(isset($_SESSION['RST_User']['Group']['CanUpdateTicketReplies']) && $ticketmessage['UserId'] == $_SESSION['RST_User']['UserId'] && $_SESSION['RST_User']['Group']['CanUpdateTicketReplies'] == 1 ? ' <button type="submit">'.BTN_UPDATE.'</button></form>' : '')
				.(isset($_SESSION['RST_User']['Group']['CanDeleteTicketReplies']) && $ticketmessage['UserId'] == $_SESSION['RST_User']['UserId'] && $_SESSION['RST_User']['Group']['CanDeleteTicketReplies'] == 1 ? ' <a onclick="return (confirm(\''.LBL_SURE.'\'))" href="'.JRoute::_('index.php?option=com_rstickets&page=ticket&id='.$ticket['TicketId'].'&action=delete_ticket_reply&rid='.$ticketmessage['TicketMessageId']).'" class="deleteButton">'.BTN_DELETE.'</a>' : '')
				.($ticketmessage['TicketNote'] != '' ? '' : (isset($_SESSION['RST_User']['Group']['CanAddTicketNote']) && $_SESSION['RST_User']['Group']['CanAddTicketNote'] == 1 ? ' <a href="#" onclick="document.getElementById(\'trnote_'.$ticketmessage['TicketMessageId'].'\').style.display=\'\'; this.style.display=\'none\'; return false;" class="noteButton">'.BTN_ADD_NOTE.'</a>' : ''))
				.'</td>
			</tr>';
			
			echo '<tr id="trnote_'.$ticketmessage['TicketMessageId'].'" style="display: none">
			<td class="tdGrey"></td>
			<td>'.(rst_is_staff_logged() ? (isset($_SESSION['RST_User']['Group']['CanUpdateTicketNote']) && $_SESSION['RST_User']['Group']['CanUpdateTicketNote'] == 1 ? '<textarea style="width: 450px;">'.$ticketmessage['TicketNote'].'</textarea>' : $ticketmessage['TicketNote']) : '').'</td>
			</tr>';
		}
	?>
	</tbody>
</table>

<form action="<?php echo JRoute::_('index.php?option=com_rstickets&page=tickets'); ?>" method="post" id="filterContainer">
<input type="hidden" name="StaffUsername" value="" />
<input type="hidden" name="CustomerUsername" value="" />
<input type="hidden" name="Search" value="" />
</form>