{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!-- $Id$ -->
{$xajax}

{include file="rt/rtticketshared.html"}

{$group_action=is_array($message.ticketid)}

<style>

	.lms-ui-box-container {
		margin-bottom: 0.5em;
	}

	#message.lms-ui-box-container .lms-ui-box-row-label {
		width: 8em;
	}

	#properties.lms-ui-box-container .lms-ui-box-row.categories .lms-ui-box-row-field {
		flex-wrap: wrap;
	}

	#properties.lms-ui-box-container .lms-ui-box-row-label {
		min-width: 14em;
	}

	#smsnotify-contacts .lms-ui-box-row-field {
		flex-direction: column;
		align-items: flex-start;
	}

	#mailnotify-contacts .lms-ui-box-row-field {
		flex-direction: column;
		align-items: flex-start;
	}

	#mailnotify-row > *,
	#smsnotify-row > * {
		align-self: center;
	}

</style>
<H1>{$layout.pagetitle}</H1>
<FORM method="POST" name="message" id="message-form" action="?m=rtmessageadd">
<INPUT type="submit" class="hiddenbtn">
<INPUT type="hidden" name="message[inreplyto]" value ="{$message.inreplyto|default:"0"}">
<INPUT type="hidden" name="message[references]" value ="{$message.references|escape|default:""}">
{if $group_action}
	{foreach $message.ticketid as $ticketid}
	<INPUT type="hidden" name="message[ticketid][]" value="{$ticketid}">
	{/foreach}
{else}
<INPUT type="hidden" name="message[ticketid]" value ="{$message.ticketid}">
{/if}
<INPUT type="hidden" name="message[customerid]" value ="{$message.customerid}">
{box_container id="message"}
	{box_header icon="subject" icon_class="fa-fw" label="Subject"}
		<INPUT type="text" size="75" name="message[subject]" maxlength="255" value="{$message.subject|escape}" {tip text="Enter message subject" trigger="subject"}>
	{/box_header}

	{box_contents}
		{box_panel}

{*
{if !$group_action}

			{box_row icon="user" icon_class="fa-fw" label="Requestor"}
				<INPUT class="radio light" type="radio" name="message[sender]" id="sender0" value="user" CHECKED><label for="sender0">{trans("user")}</label>
				<INPUT class="radio light" type="radio" name="message[sender]" id="sender1" value="customer" {if $message.sender == 'customer'}CHECKED{/if}><label for="sender1">{trans("customer")}</label>
			{/box_row}

			{box_row icon="mail" icon_class="fa-fw" label="Recipient's e-mail"}
				<INPUT type="text" size="25" name="message[destination]"
					value="{$message.destination|escape}" class="lms-ui-autogrow"
					{tip text="Enter recipient's e-mail - if sender is an user and message should be sent (optional)" trigger="destination"}
				>
			{/box_row}
{/if}
*}

			{box_row icon="message" icon_class="fa-fw" label="Message template" id="message-template-row" field_id="message-templates"
				visible=!empty($messagetemplates)}
				{include file="rt/rtmessagetemplates.html" templates=$messagetemplates}
			{/box_row}

			{block name="rtmessageadd-content"}
			{box_row icon="edit" icon_class="fa-fw" label="<!rt>Content"}
				<div class="valign-middle">
					<textarea name="message[body]" id="message-body" cols="75" rows="10"
						{tip class="lms-ui-wysiwyg-editor lms-ui-helpdesk" text="Enter message content" trigger="body"}
						{if isset($message.wysiwyg.body)} data-wysiwyg="{$message.wysiwyg.body}"{/if}
						>{$message.body}</textarea>
					{speech_recognition target="#message-body"}
				</div>
			{/box_row}
			{/block}
			{box_row icon="attachment" icon_class="fa-fw" label="Attachments"}
				{fileupload id="files" fileupload=$fileupload form="message-form"}
			{/box_row}

		{/box_panel}
	{/box_contents}

	{box_buttons}
		{button type="submit" icon="save" accesskey="s" label="Submit/Send"}
		{if $group_action}
			{button icon="cancel" onclick="location.href = '?m=rtqueueview';"
				label="Cancel"}
		{else}
			{button icon="cancel" onclick="location.href = '?m=rtticketview&id={$message.ticketid}';"
				label="Cancel"}
		{/if}
	{/box_buttons}
{/box_container}

{box_container id="properties"}
	{box_contents}
		{box_panel}

			{box_row icon="user" icon_class="fa-fw"  label="Notify users" labelid="notify"}
				<INPUT type="checkbox" name="message[notify]" id="notify" value="1"{if $message.notify} CHECKED{/if}>
			{/box_row}

			{box_row id="customernotify-row" icon="notify" icon_class="fa-fw" label="Notify customer" labelid="customernotify"}
				<INPUT type="checkbox" name="message[customernotify]" id="customernotify" value="1"{if isset($message.customernotify)} checked{/if}>
			{/box_row}

{if $group_action || !empty($contacts.mails)}
			{box_row icon="mail" icon_class="fa-fw" id="mailnotify-row" label="Notify customer via e-mail" labelid="mailnotify"}
				<input type="checkbox" name="message[mailnotify]" id="mailnotify" value="1"{if $message.mailnotify} checked{/if}>
			{/box_row}
{/if}

{if !$group_action && !empty($contacts.mails)}
			{box_row id="mailnotify-contacts" visible=!empty($message.mailnotify)}
				{foreach $contacts.mails as $contact}
					<label>
						<input type="checkbox" name="message[contacts][mails][{$contact.contact|escape}]" value="{$contact.contact|escape}"
							class="mail-contact"
							{if $contact.checked} checked{/if}>
						{$contact.contact}{if $contact.name} ({$contact.name}){/if}
						<input type="hidden" name="message[contacts][mailsources][{$contact.contact|escape}]" value="{$contact.source}">
						<input type="hidden" name="message[contacts][mailnames][{$contact.contact|escape}]" value="{$contact.name|escape}">
						<input type="hidden" name="message[contacts][maildisplays][{$contact.contact|escape}]" value="{$contact.display|escape}">
					</label>
				{/foreach}
			{/box_row}
{/if}

{if $group_action || !empty($contacts.phones)}
			{box_row icon="sms" icon_class="fa-fw" id="smsnotify-row" label="Notify customer via SMS" labelid="smsnotify"}
				<input type="checkbox" name="message[smsnotify]" id="smsnotify" value="1"
					{if $message.smsnotify} checked{/if}>
			{/box_row}
{/if}

{if !$group_action || !empty($contacts.phones)}
			{box_row id="smsnotify-contacts" visible=!empty($message.smsnotify)}
				{foreach $contacts.phones as $contact}
					<label>
						<input type="checkbox" name="message[contacts][phones][{$contact.contact}]" value="{$contact.contact}"
							class="sms-contact"
							{if $contact.checked} checked{/if}>
						{$contact.contact}{if $contact.name} ({$contact.name}){/if}
					</label>
				{/foreach}
			{/box_row}
{/if}

			{box_row icon="status" icon_class="fa-fw" label="Status"}
				<SELECT SIZE="1" name="message[state]" id="state" {tip text="Select status" trigger="state"}>
					{if $group_action}
					<option value="-1"{if $message.state == -1} selected{/if}>{trans("— no changes —")}</option>
					{/if}
					{foreach $_RT_STATES as $stateidx => $state}
					<OPTION value="{$stateidx}"{if $message.state == $stateidx} selected{/if}>{$state.label}</OPTION>
					{/foreach}
				</SELECT>
				&nbsp;
				<label>
					<INPUT type="checkbox" name="message[resolve]" id="resolve" value="{$smarty.const.RT_RESOLVED}"{if isset($message.resolve) || $message.state == $smarty.const.RT_RESOLVED} CHECKED{/if}{tip trigger="resolve"}>
					{trans("resolve ticket")}
				</label>
			{/box_row}

			{box_row icon="watch" icon_class="fa-fw" label="Obserwowanie zgłoszeń"}
				<SELECT size="1" name="message[watching]">
					<option value="-100" selected>{trans("— no changes —")}</option>
					<option value="1">{trans("Start watching")}</option>
					<option value="0">{trans("Stop watching")}</option>
				</SELECT>
			{/box_row}

			{box_row icon="cause" icon_class="fa-fw" label="Cause"}
				<SELECT SIZE="1" name="message[cause]" {tip text="Select ticket cause" trigger="cause"}>
					{if $group_action}
					<option value="-1"{if $message.cause == -1} selected{/if}>{trans("— no changes —")}</option>
					{/if}
					<OPTION value="0" {if !$message.cause}SELECTED{/if}>{trans("unknown/other")}</OPTION>
					<OPTION value="1" {if $message.cause == 1}SELECTED{/if}>{trans("customer's side")}</OPTION>
					<OPTION value="2" {if $message.cause == 2}SELECTED{/if}>{trans("company's side")}</OPTION>
				</SELECT>
			{/box_row}

{if !$group_action}
			{box_row icon="source" icon_class="fa-fw" label="Ticket source"}
				<SELECT size="1" name="message[source]" {tip text="Select ticket source" trigger="source"}>
					{foreach Localisation::arraySort($_RT_SOURCES) as $sourceidx => $status}
					<OPTION value="{$sourceidx}"{if $message.source == $sourceidx} selected{/if}>{$status}</OPTION>
					{/foreach}
				</SELECT>
			{/box_row}
{/if}

			{box_row icon="priority" icon_class="fa-fw" label="Priority"}
				<SELECT size="1" name="message[priority]" {tip text="Set ticket priority" trigger="priority"}>
					{if $group_action}
					<option value="-100"{if $message.priority == -100} selected{/if}>{trans("— no changes —")}</option>
					{/if}
					{foreach $_RT_PRIORITIES as $priorityidx => $priority}
					<OPTION value="{$priorityidx}"{if $message.priority == $priorityidx} selected{/if}>{$priority}</OPTION>
					{/foreach}
					<OPTION value="" {if !isset($message.priority)} selected{/if}>{trans("undefined")}</OPTION>
				</SELECT>
			{/box_row}

			{box_row icon="queue" icon_class="fa-fw" label="Queue"}
				<select size="1" name="message[queueid]" id="queue-selection"
					{tip text="Select queue" trigger="queue" class="lms-ui-advanced-select-test"}>
					{if $group_action}
						<option value="-100"{if $message.queueid == -100} selected{/if} data-newmessage-notify="1">{trans("— no changes —")}</option>
					{/if}
					{foreach $queuelist as $item}
						<option value="{$item.id}"
							{if $item.newmessagesubject && $item.newmessagebody || $item.newmessagesmsbody} data-newmessage-notify="1"{/if}
							{if $item.resolveticketsubject && $item.resolveticketbody || $item.resolveticketsmsbody} data-resolveticket-notify="1"{/if}
							{if $item.id == $message.queueid} selected{/if}>{$item.name}</option>
					{/foreach}
				</select>
			{/box_row}

			{box_row icon="owner" icon_class="fa-fw" label="Owner"}
				<select size="1" name="message[owner]" id="owner"
					{tip text="Select user" trigger="owner" class="lms-ui-advanced-select-test"}>
					<option value="0">{trans("— select user —")}</option>
					{if $group_action}
						<option value="-100"{if $message.owner == -100} selected{/if}>{trans("— no changes —")}</option>
					{/if}
					{foreach $userlist as $user}
						{if $user.id != $message.owner
							&& ($hide_disabled_users && !$user.access
								|| $hide_deleted_users && $user.deleted)}
							{continue}
						{/if}
						<option value="{$user.id}"
							class="{if !$user.access} lms-ui-disabled{/if}{if $user.deleted} lms-ui-crossed{/if}"
							{if $user.id == $message.owner} selected{/if}
						>{strip}
							{$user.rname|trunescape:40} ({$user.login})
						{/strip}</option>
					{/foreach}
				</select>
				&nbsp;
				<label>
					<input type="checkbox" id="assign-to-me" data-userid="{$layout.logid}">
					{trans("assign to me")}
				</label>
			{/box_row}

			{box_row icon="verifier" icon_class="fa-fw" label="Verifier" field_id="rtverifiers"}
				{if $group_action || !$ticket.verifierid || $ticket.verifierid == $layout.logid}
					<select size="1" name="message[verifierid]"
						{tip text="Select user" trigger="verifierid" class="lms-ui-advanced-select-test"}>
						<option value="0">{trans("— select user —")}</option>
						{if $group_action}
							<option value="-1"{if $message.verifierid == -1} selected{/if}>{trans("— no changes —")}</option>
						{/if}
						{foreach $userlist as $user}
							{if $user.id != $message.verifierid
								&& ($hide_disabled_users && !$user.access
									|| $hide_deleted_users && $user.deleted)}
								{continue}
							{/if}
							<option value="{$user.id}"
								class="{if !$user.access} lms-ui-disabled{/if}{if $user.deleted} lms-ui-crossed{/if}"
								{if $user.id == $message.verifierid} selected{/if}
							>{strip}
								{$user.rname|trunescape:40} ({$user.login})
							{/strip}</option>
						{/foreach}
					</select>
				{else}
					<input type="hidden" name="message[verifierid]" value="{$ticket.verifierid}">
					<span title="{trans("Only verifier can change this")}">{$ticket.verifier_username}</span>
				{/if}
			{/box_row}

			{box_row icon="deadline" icon_class="fa-fw" label="<!rt>Deadline"}
				{if $group_action
					|| ConfigHelper::checkConfig('rt.allow_all_users_modify_deadline', ConfigHelper::checkConfig('phpui.helpdesk_allow_all_users_modify_deadline'))
					|| !$ticket.verifierid || $ticket.verifierid == $layout.logid}
					<INPUT TYPE="TEXT" NAME="message[deadline]" VALUE="{if $message.deadline}{$message.deadline|date_format:"Y/m/d H:i"}{/if}"
							SIZE="20" {tip text="Enter deadline in YYYY/MM/DD HH:MM format or click to select it from applet" class="lms-ui-datetime" trigger="deadline"}>
				{else}
					<INPUT TYPE="hidden" NAME="message[deadline]" VALUE="{if $message.deadline}{$message.deadline|date_format:"Y/m/d H:i"}{/if}">
					<span title="{trans("Only verifier can change this")}">{if $message.deadline}{$message.deadline|date_format:"Y-m-d H:i"}{else}{trans("<!rt>— none —")}{/if}</span>
				{/if}
			{/box_row}

			{box_row icon="queue" icon_class="fa-fw" label="Category changes"}
				<SELECT name="message[category_change]" {tip text="Select category change type" trigger="category_change"}>
					<option value="0"{if $message.category_change == 0} selected{/if}>{trans("— no changes —")}</option>
					<option value="1"{if $message.category_change == 1} selected{/if}>{trans("— replace —")}</option>
					<option value="2"{if $message.category_change == 2} selected{/if}>{trans("— append —")}</option>
					<option value="3"{if $message.category_change == 3} selected{/if}>{trans("— remove —")}</option>
				</SELECT>
			{/box_row}

			{box_row id="categories" icon="categories" icon_class="fa-fw" label="Categories" class="categories"}
				{include file="rt/rtcategoryselection.html" name_prefix="message[categories]" categories=$categories}
			{/box_row}

		{/box_panel}
	{/box_contents}
{/box_container}
</FORM>

{if !$group_action}
{include file="rt/rtticketmessagesbox.html"}
{/if}

<SCRIPT>

	$(function() {
		$('form[name="message"] [name="message[subject]"]').focus();

		$('[name="message[queueid]"]').change(function() {
			var newmessage_notify = $(this).find(':selected').attr('data-newmessage-notify');
			var resolveticket_notify = $(this).find(':selected').attr('data-resolveticket-notify');
			if (newmessage_notify === undefined && resolveticket_notify === undefined) {
				$('#customernotify-row').hide();
				$('#customernotify').attr('checked', false);
			} else {
				$('#customernotify-row').show();
				$('#customernotify').attr('checked', true);
			}
		});

		{if !$group_action}
		var newmessage_notify = $('[name="message[queueid]"]').find(':selected').attr('data-newmessage-notify');
		var resolveticket_notify = $('[name="message[queueid]"]').find(':selected').attr('data-resolveticket-notify');
		if (newmessage_notify === undefined && resolveticket_notify === undefined) {
			$('#customernotify-row').hide();
		} else {
			$('#customernotify-row').show();
		}
		{/if}

		$('#mailnotify').change(function() {
			$('#mailnotify-contacts').toggle($(this).prop('checked'));
		});

		$('#smsnotify').change(function() {
			$('#smsnotify-contacts').toggle($(this).prop('checked'));
		});

		$('[name="message[category_change]"]').change(function() {
			if (parseInt($(this).val())) {
				$('#categories').show();
			} else {
				$('#categories').hide();
			}
		}).change();

		$('body').on('change', '.template-selector', function() {
			var selected = $('option:selected', this);
			if (parseInt(selected.val()) == 0) {
				return;
			}
			$('[name="message[subject]"]').val(Base64.decode(selected.attr('data-subject')));

			var message = Base64.decode(selected.attr('data-message'));
			var editor = tinyMCE.get(0);
			if (editor) {
				editor.setContent(message);
			}
			$('[name="message[body]"]').val(message);
		});

		{if !$group_action}
		$('#message-form').submit(function() {
			var errorMessages = [];
			var errorElem = null;
			if ($('#mailnotify').prop('checked') && !$('.mail-contact:checked').length) {
				errorElem = $('.mail-contact').get(0);
				errorMessages.push($t("No email address is selected while customer email notification is checked!"));
			}
			if ($('#smsnotify').prop('checked') && !$('.sms-contact:checked').length) {
				errorElem = $('.sms-contact').get(0);
				errorMessages.push($t("No phone number is selected while customer sms notification is checked!"));
			}
			if (errorMessages.length) {
				alertDialog(errorMessages.join('<br>'), errorElem);
				$(this).find('button[type="submit"]').prop('disabled', false);
				return false;
			}
		});
		{/if}

		{if isset($citing)}
		var textarea = $('[name="message[body]"]');
		textarea.scrollTop(textarea[0].scrollHeight - textarea.height());
		textarea[0].setSelectionRange(textarea.html().length, textarea.html().length);
		{/if}
	});

</SCRIPT>

{/block}
