<!-- $Id$ -->
{$gps_location_url = ConfigHelper::getConfig('phpui.gps_location_url', 'https://www.google.com/maps/place/%location')}
{$ticketinfo_clipboard_text = ConfigHelper::getConfig('rt.ticketinfo_clipboard_text',"%ticketurl {trans("Subject:")}%subject{if $ticket.location}, {trans("Location:")}%location{/if}")|replace:"%location":$ticket.location|replace:"%ticketid":$ticket.ticketid|replace:"%subject":$ticket.subject|replace:"%ticketurl":"{$url}?m=rtticketview&id={$ticket.ticketid}"}

<TABLE class="lmsbox">
	<THEAD>
	<TR>
		<TD style="width: 100%;" class="bold" colspan="3">
			{icon name="helpdesk" class="lms-ui-button-clipboard" label="{t}Ticket No.{/t} {$ticket.ticketid|string_format:"%06d"}" data_clipboard_text=$ticketinfo_clipboard_text}
		</TD>
	</TR>
	</THEAD>
	<TFOOT>
	<TR>
		<TD style="width: 100%;" class="navigation" colspan="3">
			{if $ticket.prev_ticketid != NULL}
				{button href="?m=rtticketview&id={$ticket.prev_ticketid}" icon="previous" label="Previous ticket"}
			{else}
				{button icon="previous" label="Previous ticket" disabled=true}
			{/if}
			&nbsp;&nbsp;&nbsp;
			{if $ticket.next_ticketid != NULL}
				{button href="?m=rtticketview&id={$ticket.next_ticketid}" label="Next ticket" icon="next"}
			{else}
				{button icon="next" label="Next ticket" disabled=true}
			{/if}
		</TD>
	</TR>
	</TFOOT>
	<TBODY>
	<TR>
		<TD style="width: 100%;" colspan="3">
			<TABLE width="100%" cellpadding="0">
				<TR>
					<TD style="width: 50%;">
						<TABLE>
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="subject" label="Subject"}
								</TD>
								<TD style="width: 99%;" class="nobr" {tip text=$ticket.subject}>
									{$ticket.subject|trunescape:50}
								</TD>
							</TR>
							{if $ticket.service != SERVICE_OTHER}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="service" label="Service type"}
								</TD>
								<TD style="width: 99%;" class="nobr">
                                    {$_SERVICETYPES[$ticket.service]}
								</TD>
							</TR>
							{/if}
							{if $ticket.type != RT_TYPE_OTHER}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="type" label="Ticket type"}
								</TD>
								<TD style="width: 99%;" class="nobr">
                                    {trans($_RT_TYPES[$ticket.type].label)}
								</TD>
							</TR>
							{/if}
							{if $ticket.customerid}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="customer" label="Customer"}
								</TD>
								<TD style="width: 99%;" class="nobr">
									<A href="?m=customerinfo&id={$ticket.customerid}">{$ticket.customername|escape} (#{$ticket.customerid})</A>
								</TD>
							</TR>
							{/if}
							{if $ticket.requestor_userid || $ticket.requestor_name || $ticket.requestor_mail || $ticket.requestor_phone}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="requestor" label="Requestor"}
								</TD>
								<TD style="width: 99%;" class="nobr">
									{if $ticket.requestor_userid}
										<A href="?m=userinfo&id={$ticket.requestor_userid}">{$ticket.requestor_username}</A>
									{else}
										{$ticket.requestor_name}
										{if $ticket.requestor_mail}
											<br><A href="mailto:{$ticket.requestor_mail}">{$ticket.requestor_mail}</A>
										{/if}
										{if $ticket.requestor_phone}
											<br>
											<a class="phone_number" href="tel:{$ticket.requestor_phone|escape}">
												{icon name="phone"}
												{$ticket.requestor_phone|escape}
											</a>
											{Utils::createCallPhoneUrl($ticket.requestor_phone)}
										{/if}
									{/if}
								</TD>
							</TR>
							{/if}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="timetable" label="Create time"}
								</TD>
								<TD style="width: 99%;">
									{$ticket.createtime|date_format:"Y-m-d H:i:s"}
								</TD>
							</TR>
							{if ConfigHelper::checkPrivilege('helpdesk_ticket_custom_times') && isset($ticket.customcreatetime)}
								<tr>
									<td class="nobr">
										<strong>
											{icon name="timetable" label="<!rtticket>Custom create time"}
										</strong>
									</td>
									<td>
										{$ticket.customcreatetime|date_format:"Y-m-d H:i"}
									</td>
								</tr>
							{/if}
							{if $ticket.creatorid}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="user" label="Created by"}
								</TD>
								<TD style="width: 99%;">
									<A href="?m=userinfo&amp;id={$ticket.creatorid}">{$ticket.creator|escape}</a>
								</TD>
							</TR>
							{/if}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="uptime" label="Uptime"}
								</TD>
								<TD style="width: 99%;">
									{$ticket.uptime}
								</TD>
							</TR>
							{if ConfigHelper::checkPrivilege('helpdesk_ticket_custom_times') && isset($ticket.customresolvetime)}
								<tr>
									<td class="nobr">
										<strong>
											{icon name="timetable" label="<!rtticket>Custom resolve time"}
										</strong>
									</td>
									<td>
										{$ticket.customresolvetime|date_format:"Y-m-d H:i"}
									</td>
								</tr>
							{/if}
							{if !empty($ticket.categories)}
							<TR>
								<TD style="width: 1%;" class="valign-top bold nobr">
									{icon name="categories" label="Categories"}
								</TD>
								<TD style="width: 99%;" class="nobr">
									{foreach $ticket.categories as $tc}
										<a href="?m=rtqueueview&persistent-filter=-1&catid={$tc.categoryid}&o=lastmodified,desc">
											<div class="lms-ui-category-label" style="{$tc.style}">{$tc.name}</div>
										</a>
									{/foreach}
								</TD>
							</TR>
							{/if}
						</TABLE>
					</TD>
					<TD style="width: 50%;" class="valign-top">
						<TABLE>
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="owner" label="Owner"}
								</TD>
								<TD style="width: 99%;">
									{if $ticket.owner}
									<A href="?m=userinfo&id={$ticket.owner}">{$ticket.ownername}</A>
									{else}
									{trans("none")}
									{/if}
								</TD>
							</TR>
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="verifier" label="Verifier"}
								</TD>
								<TD style="width: 99%;">
									{if $ticket.verifierid}
									<A href="?m=userinfo&id={$ticket.verifierid}">{$ticket.verifier_username}</A>
									{else}
									{trans("none")}
									{/if}
								</TD>
							</TR>
							{if $ticket.deadline}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="timetable" label="<!rt>Deadline"}
								</TD>
								<TD style="width: 99%;">
									{if $ticket.deadline}
										{$ticket.deadline|date_format:"Y-m-d H:i:s"}
									{else}
										{trans("none")}
									{/if}
								</TD>
							</TR>
							{/if}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="queue" label="Queue"}
								</TD>
								<TD style="width: 99%;">
									<A href="?m=rtqueueview&amp;id={$ticket.queueid}">{$ticket.queuename}</A>
									{if empty($favorite_queues)}
										{button type="link" icon="next" disabled=true tip="No favorite queues defined (check 'rt.favorite_queues' configuration setting)"}
									{else}
										{foreach $favorite_queues as $queueid => $name}
										{if $queueid != $ticket.queueid}
											{button type="link" icon="next" tip=trans('Move ticket to queue $a', $name)
												data_href="?m=rtticketedit&action=queuechange&id={$ticket.ticketid}&queueid=$queueid"
												data_confirmation_text=trans('Are you sure, you want to move ticket to $a?', $name)
											}
										{/if}
										{/foreach}
									{/if}
								</TD>
							</TR>
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="status" label="Status"}
								</TD>
								<TD style="width: 99%;">
									{$_RT_STATES[$ticket.state].label}
								</TD>
							</TR>
							{if $ticket.cause}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="cause" label="Cause"}
								</TD>
								<TD style="width: 99%;">
									{if $ticket.cause==2}{trans("company's side")}
									{elseif $ticket.cause==1}{trans("customer's side")}
									{else}{trans("unknown/other")}{/if}
								</TD>
							</TR>
							{/if}
							{if $ticket.deleted == 1 && (ConfigHelper::checkPrivilege('helpdesk_advanced_operations'))}
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="user" label="Remover"}
								</TD>
								<TD style="width: 99%;">
									<A href="?m=userinfo&id={$ticket.deluserid}">{$ticket.delusername|escape}</A>
								</TD>
							</TR>
							<TR>
								<TD style="width: 1%;" class="bold nobr">
									{icon name="remove" label="Remove date"}
								</TD>
								<TD style="width: 99%;">
									{$ticket.deltime|date_format:"Y-m-d H:i:s"}
								</TD>
							</TR>
							{/if}
							{if $ticket.source}
							<TR>
								<TD class="bold nobr">
									{icon name="source" label="Ticket source"}
								</TD>
								<TD>
									{trans($_RT_SOURCES[$ticket.source])}
								</TD>
							</TR>
							{/if}
							{if isset($ticket.priority)}
							<TR>
								<TD class="bold nobr">
									{icon name="priority" label="Priority"}
								</TD>
								<TD>
									<span style="{$_RT_PRIORITY_STYLES[$ticket.priority]}">{$_RT_PRIORITIES[$ticket.priority]}</span>
								</TD>
							</TR>
							{/if}
							{if $ticket.address_id}
							<TR>
								<TD class="bold nobr">
									{icon name="location" label="Location"}
								</TD>
								<TD>
									{$ticket.location|escape}
									{button
										type="link"
										tip="Show on map"
										icon="location"
										href="{$gps_location_url|replace:"%location":$ticket.location|escape}"
									}
									{show_on_map_button
										type="default"
										cityid=$ticket.city_id
										streetid=$ticket.street_id
										building_num=$ticket.house
									}
									{show_on_map_button
										type="google"
										cityid=$ticket.city_id
										streetid=$ticket.street_id
										building_num=$ticket.house
									}
									{show_on_map_button
										type="geoportal"
										cityid=$ticket.city_id
										streetid=$ticket.street_id
										building_num=$ticket.house
									}
									{show_on_map_button
										type="netstork"
										cityid=$ticket.city_id
										streetid=$ticket.street_id
										building_num=$ticket.house
									}
									{show_on_map_button
										type="sidusis"
										cityid=$ticket.city_id
										streetid=$ticket.street_id
										building_num=$ticket.house
									}
								</TD>
							</TR>
							{/if}
							{if $ticket.location && !empty($ticket.coords.latitude)}
							<TR>
								<TD class="bold nobr">
									{icon name="location"
										class="lms-ui-button-clipboard"
										label="GPS coordinates"
										data_clipboard_text="{$ticket.coords.latitude}, {$ticket.coords.longitude}"
									}
								</TD>
								<TD>
									{button
										type="link"
										class="lms-ui-button-clipboard"
										label="{$ticket.coords.latitude}, {$ticket.coords.longitude}"
										data_clipboard_text="{$ticket.coords.latitude}, {$ticket.coords.longitude}"
									}
								</TD>
							</TR>
							{/if}
							{if $ticket.nodeid}
							<TR>
								<TD class="bold nobr">
									{icon name="node" label="Node"}
								</TD>
								<TD>
									<a href="?m=nodeinfo&id={$ticket.nodeid}">
										{$ticket.node_name} ({$ticket.nodeid})
									</a>
								</TD>
							</TR>
							{/if}
							{if $ticket.netnodeid}
							<TR>
								<TD class="bold nobr">
									{icon name="netnode" label="Network Device Node"}
								</TD>
								<TD>
									<A href="?m=netnodeinfo&id={$ticket.netnodeid}">{$ticket.netnode_name|escape}</A>
								</TD>
							</TR>
							{/if}
							{if $ticket.netdevid}
							<TR>
								<TD class="bold nobr">
									{icon name="netdev" label="Device"}
								</TD>
								<TD>
									<A href="?m=netdevinfo&id={$ticket.netdevid}">{$ticket.netdev_name|escape}</A>
								</TD>
							</TR>
							{/if}
							{if $ticket.invprojectid}
							<TR>
								<TD class="bold nobr">
									{icon name="invproject" label="Investment project"}
								</TD>
								<TD>
									{$ticket.invproject_name|escape}</A>
								</TD>
							</TR>
							{/if}
							{if $ticket.parentid}
							<TR>
								<TD class="bold">
									{icon name="parentticket" label="Primary ticket"}
								</TD>
								<TD>
									#{$ticket.parent.id|string_format:"%06d"} <a href="?m=rtticketview&id={$ticket.parent.id}">{$ticket.parent.name|escape}</a><br>
								</TD>
							</TR>
							{/if}
							{if $ticket.childtickets}
								<TR>
									<TD class="bold">
										{icon name="childticket" label="Secondary tickets"}
									</TD>
									<TD>
										{foreach $ticket.childtickets as $t}
											#{$t.id|string_format:"%06d"}
											<a href="?m=rtticketview&id={$t.id}">{$t.subject|escape}</a>
											<br>
										{/foreach}
									</TD>
								</TR>
							{/if}
							{if $ticket.relatedtickets}
							<TR>
								<TD class="bold nobr">
									{icon name="relatedticket" label="Related tickets"}
								</TD>
								<TD>
									{foreach $ticket.relatedtickets as $i}
										{hint icon="info" mode="toggle" url="?m=rtticketinfo&id={$i.id}"}
										#{$i.id}
										<a href="?m=rtticketview&id={$i.id}">
											{$i.name|escape}
										</a>
										{if !$i@last}
											<br>
										{/if}
									{/foreach}
								</TD>
							</TR>
							{/if}
						</TABLE>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
	<TR>
		<TD class="lms-ui-box-buttons" colspan="3" data-popup-menu-title="{t a=$ticket.ticketid|string_format:"#%06d"}Ticket $a{/t}">
			<label>
				<input type="checkbox" id="messages-chkbox">
				{trans("Messages")}
			</label>
			<label>
				<input type="checkbox" id="notes-chkbox">
				{trans("Notes<!rt>")}
			</label>
			<label>
				<input type="checkbox" id="history-chkbox">
				{trans("History<!rt>")}
			</label>
			{button id="print-anchor" icon="print" label="Print"}

			<br>
			{if $ticket.state != $smarty.const.RT_RESOLVED || ($ticket.state == $smarty.const.RT_RESOLVED && $aet && ($smarty.now - $ticket.resolvetime) < $aet) || (ConfigHelper::checkPrivilege('superuser'))}
				{button type="link-button" icon="edit" tip= "Edit" label="Edit" href="?m=rtticketedit&id={$ticket.ticketid}"}
			{/if}
			{if $ticket.state != $smarty.const.RT_RESOLVED}
				{if ConfigHelper::checkConfig('rt.block_ticket_close_with_open_events', ConfigHelper::checkConfig('phpui.helpdesk_block_ticket_close_with_open_events'))
					&& !empty($ticket['openeventcount'])}
					{button icon="status-resolved" label="Ticket has open assigned events!" tip="Ticket has open assigned events!" class="blend disabled"}
				{else}
					{if empty($ticket['verifier_rtime']) && $ticket['verifierid'] != Auth::GetCurrentUser() && $ticket['verifierid']}
						{button icon="verifier" label="Transfer to verifier" tip="Transfer to verifier" onclick="location.href='?m=rtticketedit&id={$ticket.ticketid}&action=verify';"}
					{else}
						{if !empty($ticket['verifier_rtime']) && $ticket['verifierid'] != Auth::GetCurrentUser()}
							{icon name="verifier"}<span style="color: {$_RT_STATES[$smarty.const.RT_VERIFIED].color}"> {trans("Ticket has been transferred to verifier")}</span>
						{else}
							{button icon="status-resolved" label="Resolve" tip="Resolve" onclick="location.href='?m=rtticketedit&id={$ticket.ticketid}&action=resolve';"}
						{/if}
					{/if}
				{/if}
			{/if}
			{buttons type="button" icon="dropdown2" label="Additional options"}
				{if ($ticket.owner != $layout.logid && $ticket.state != $smarty.const.RT_RESOLVED) || ConfigHelper::checkPrivilege('superuser')}
					{button id="ticket-assign-to-me" icon="user" label="Assign to me" tip="Assign to me" data_href="?m=rtticketedit&id={$ticket.ticketid}&action=assign"}
				{/if}
				{if (!$ticket.verifierid && $ticket.owner != $layout.logid && $ticket.state != $smarty.const.RT_RESOLVED) || ConfigHelper::checkPrivilege('superuser')}
					{button id="ticket-assign2-to-me" type='link-button' icon="user" label="Assign to me as verifier" tip="Assign to me as verifier" href="?m=rtticketedit&id={$ticket.ticketid}&action=assign2"}
				{/if}
				{if $ticket.watching}
				    {button icon="unwatch" tip="Stop watching" label="Stop watching" href="?m=rtqueueview&ticketid={$ticket.ticketid}&ticketwatching=0"}
				{else}
				    {button icon="watch" tip="Start watching" label="Start watching" href="?m=rtqueueview&ticketid={$ticket.ticketid}&ticketwatching=1"}
				{/if}
				{button type="link-button" icon="clone" label="Clone" tip="Clone" href="?m=rtticketadd&ticketid={$ticket.ticketid}"}
				{if $ticket.state != $smarty.const.RT_RESOLVED}
					{button type="link-button" icon="childticket" label="Create secondary ticket" tip="Create secondary ticket" href="?m=rtticketadd&parentid={$ticket.ticketid}"}
				{/if}
				{if count($ticket.images)}
					{button id="ticket-gallery-button" icon="gallery" label="Attachment gallery" tip="Attachment gallery" data_gallery="{base64_encode(json_encode($ticket.images))}"}
				{/if}
				{if $ticket.deleted == 1 && $ticket.deltime != 0 && (ConfigHelper::checkPrivilege('helpdesk_advanced_operations'))}
					{button id="restore-rtticket" icon="restore" label="Restore<!RT>" tip="Restore<!RT>"
						data_href="?m=rtrestore&id={$ticket.ticketid}&taction=restore"
						data_confirmation_text=trans('Are you sure, you want to restore ticket $a?', $ticket.ticketid)
					}
					{button id="remove-rtticket" icon="delete" label="Delete permanently" tip="Delete permanently"
						data_href="?m=rtticketdel&id={$ticket.ticketid}&taction=delperm"
						data_confirmation_text=trans('Are you sure, you want to permanently remove ticket $a?', $ticket.ticketid)
					}
				{else}
					{button id="delete-rtticket" icon="delete" label="Delete" tip="Delete"
						data_href="?m=rtticketdel&id={$ticket.ticketid}&taction=delete"
						data_confirmation_text=trans('Are you sure, you want to remove ticket $a?', $ticket.ticketid)
					}
				{/if}
				{button icon="link" class="lms-ui-button-clipboard" label="Copy link" data_clipboard_text=$ticketinfo_clipboard_text}
			{/buttons}
		</TD>
	</TR>
	</TBODY>
</TABLE>

<div id="rtticketview-sortable" class="lms-ui-sortable-persistent">

{block name="rtticketstats"}
	{if $stats}
	<TABLE class="lmsbox lms-ui-tab-container lms-ui-sortable" id="rtticketstats">
		<TBODY>
		<TR>
			<TD style="width: 1%;" class="dark bold nobr">
				{icon name="helpdesk" label="Requests causes (last year)" class="lms-ui-sortable-handle"}
			</TD>
			<TD style="width: 99%;" class="dark">
				{trans("company's side")}: <span class="bold">{$stats.2.num|default:0}</span>,
				{trans("customer's side")}: <span class="bold">{$stats.1.num|default:0}</span>,
				{trans("unknown/other<!plural>")}: <span class="bold">{$stats.0.num|default:0}</span>,
				{trans("all")}: <span class="bold">{$stats.0.num+$stats.1.num+$stats.2.num}</span>,
				{button href="?m=rtsearch&amp;id={$customerinfo.id}" icon="helpdesk" label="Show tickets"}
			</TD>
		</TR>
		</TBODY>
	</TABLE>
	{/if}

{/block}
{block name="rtticketcustomer"}
	{if $customerinfo}
	<TABLE class="lmsbox lms-ui-tab-container lms-ui-sortable" id="rtticketcustomer">
		<TBODY>
		<TR>
			<TD style="width: 1%;" class="dark bold nobr">
				<a href="?m=customerinfo&amp;id={$ticket.customerid}">{icon name="customer" class="lms-ui-sortable-handle" label=$customerinfo.customername}</a>
			</TD>
			<TD style="width: 1%;" class="dark nobr">
				{icon name="home"} {$customerinfo.address} &nbsp;{$customerinfo.zip} {$customerinfo.city}
				{if $customerinfo.phone}
					<a class="phone_number" href="tel:{$customerinfo.phone|escape}">
						{icon name="phone"}
						{$customerinfo.phone|escape}
					</a>
					{Utils::createCallPhoneUrl($customerinfo.phone)}
				{/if}
				{icon name="money"} {if $customerinfo.balance < 0}<span class="red">{/if}{$customerinfo.balance|money_format}{if $customerinfo.balance < 0}</span>{/if}
			</TD>
			<TD style="width: 98%;" class="dark text-right red bold">
				{if $customerinfo.groups}
				({foreach from=$customerinfo.groups item=item name=groups}{$item.name}{if $smarty.foreach.groups.iteration < count($customerinfo.groups)}, {/if}{/foreach})
				{/if}
			</TD>
		</TR>
		</TBODY>
	</TABLE>
	{/if}
{/block}

{block name="rtticketnodes"}
	{if $customernodes}
		{include file="customer/customernodesbox.html"}
	{/if}
{/block}

{block name="rtticketeventlist"}
	{include file="rt/rtticketeventlist.html"}
{/block}

{block name="rtticketparentinfo"}
	{if $ticket.parentid}
		{include file="rt/rtticketparentinfo.html"}
	{/if}
{/block}

{block name="rtticketchildren"}
	{if $ticket.childtickets}
		{include file="rt/rtticketchildren.html"}
	{/if}
{/block}

{block name="rtticketrelated"}
	{if $ticket.relatedtickets}
		{include file="rt/rtticketrelatedlist.html"}
	{/if}
{/block}

</div>

<script>

	restoreSortable('rtticketview-sortable', '{$rtticketview_sortable_order}');

	$(function() {
		$('#print-anchor').click(function() {
			window.open('?m=rtticketprint&id={$ticket.ticketid}'
				+ ($('#messages-chkbox:checked').length ? '&messages=1' : '')
				+ ($('#notes-chkbox:checked').length ? '&notes=1' : '')
				+ ($('#history-chkbox:checked').length ? '&history=1' : ''),
				'_blank');
		});

		$('#ticket-assign-to-me').click(function() {
			var link = $(this);
			$.ajax(
				link.attr('data-href') + '&check-conflict=1',
				{
					async: true,
					method: "GET",
					dataType: "json",
					success: function(data) {
						if (data) {
							confirmDialog($t("Ticket has already been assigned to other user in mean-time. Are you sure you want to re-assign ticket to you?"), link).done(function() {
								location.href = link.attr('data-href');
							});
						} else {
							location.href = link.attr('data-href');
						}
					}
				}
			);
			return false;
		});

		$('#ticket-gallery-button').click(function() {
			showGallery(JSON.parse(Base64.decode($(this).attr('data-gallery'))));
		});
	});

</script>
