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

<style>

	.template-attachments {
		list-style-type: none;
		margin-block-start: 0;
		margin-block-end: 0;
		padding-inline-start: 0;
	}

</style>

<form method="post" action="?m=messagetemplatedel" name="messagetemplates" id="messagetemplates">
</form>

<form method="get" name="filter" id="filter">
	<input type="hidden" name="m" value="{$layout.module}">
</form>

<TABLE class="lmsbox lms-ui-background-cycle" id="message-templates">
	<COLGROUP>
		<COL style="width: 1%;">
		<COL style="width: 1%;">
		<COL style="width: 1%;">
		<COL style="width: 96%;">
		<COL style="width: 1%;">
		<COL style="width: 1%;">
		<COL style="width: 1%;">
		<COL style="width: 1%;">
	</COLGROUP>
	<THEAD>
		<TR>
			<TD class="nobr">
				{trans("Type:")}
			</TD>
			<TD class="nobr">
				{trans("Name:")}
			</TD>
			<TD class="nobr">
				{trans("Subject:")}
			</TD>
			<TD class="nobr">
				{trans("Message:")}
			</TD>
			<td class="nobr">
				{trans("Attachments")}
			</td>
			<TD class="nobr">
				{trans("Queues:")}
			</TD>
			<TD class="nobr">
				{trans("<!message-type>Message types:")}
			</TD>
			<TD>
				&nbsp;
			</TD>
		</TR>
		<TR>
			<td colspan="8">
				<span class="bold">{trans("Filter:")}</span> {trans("Type:")}
				<select name="type" id="template-type" form="filter" onchange="javascript:document.filter.submit();">
					<option value="0">{trans("— all —")}</option>
					{foreach $_MESSAGETEMPLATES as $template_type => $template}
					<option value="{$template_type}"
							{if $type == $template_type} selected{/if}>{$template.label}</option>
					{/foreach}
				</select>
			</td>
		</TR>
	</THEAD>
	<TBODY class="lms-ui-multi-check">
		{foreach $templates as $template}
		<TR class="highlight" data-template-id="{$template.id}">
			<TD class="nobr text-center" data-template-type="{$template.type}" data-template-content-type="{$template.contenttype}">
				<i class="{$_MESSAGETEMPLATES[$template.type].class}" title="{$_MESSAGETEMPLATES[$template.type].label}"></i>
			</TD>
			<TD class="nobr" data-template-name="{$template.name|escape}">
				{$template.name|escape}
			</TD>
			<TD class="nobr" data-template-subject="{$template.subject|escape}">
				{$template.subject|escape}
			</TD>
			<TD>
				<span class="data-template-message-area">{$template.message|truncate:5000:"&hellip;"|replace:"\n":"<BR>"}</span>
				<div style="display: none;" class="data-template-message">{$template.message}</div>
			</TD>
			<td data-template-attachments="{if !empty($template.attachments)}{base64_encode(json_encode($template.attachments))}{/if}">
				{if !empty($template.attachments)}
					{capture assign="attachments"}{strip}
						<ul class="template-attachments">
							{foreach $template.attachments as $attachment}
								<li>
									{documentview id="template-attachment-{$attachment.id}" type=$attachment.contenttype name=$attachment.filename
										url="?m=messagetemplatelist&action=attachment-view&id={$attachment.id}" external=true
										text="{$attachment.filename} ({$attachment.size|size_format}) <i class=\"lms-ui-icon-view{if preg_match('/pdf/',$attachment.contenttype)} pdf{elseif preg_match('/csv|excel|spreadsheet/',$attachment.contenttype)} xls{/if}\"></i>"}
								</li>
							{/foreach}
						</ul>
					{/strip}{/capture}
					{hint icon="attachment" text=$attachments data_template_id="message-template-{$template.id}"}
				{/if}
			</td>
			<TD data-template-helpdesk-queues="{$template.queues}">
				{$template.queuenames}
			</TD>
			<TD data-template-helpdesk-message-types="{$template.messagetypes}">
				{if !empty($template.messagetypes)}
					{foreach explode(',', $template.messagetypes) as $type}
						{if $type == $smarty.const.RTMESSAGE_REGULAR}
						{trans("<!message-type>message")}{if !$type@last},{/if}
						{elseif $type == $smarty.const.RTMESSAGE_NOTE}
						{trans("<!message-type>note")}{if !$type@last},{/if}
						{/if}
					{/foreach}
				{/if}
			</TD>
			<TD class="text-right nobr">
				{button type="link" class="edit-template-dialog-btn" icon="edit" tip="Edit"
					onclick="javascript:open_edit_dialog({$template.id});"}
				{button type="link" class="delete-messagetemplate" href="?m=messagetemplatedel&id={$template.id}" icon="delete" tip="Delete"}
				<input type="checkbox" name="marks[{$template.id}]" value="{$template.id}" class="lms-ui-multi-check" form="messagetemplates">
			</TD>
		</TR>
		{foreachelse}
		<tr>
			<td colspan="8" class="empty-table">
				{trans("No message templates in database.")}
			</td>
		</tr>
		{/foreach}
	</TBODY>
	<TFOOT>
		<TR class="lms-ui-button-panel">
			<TD colspan="6" class="nobr">
				{button icon="add" class="add-template-dialog-btn" tip="Add" label="Add"
					onclick="javascript:open_add_dialog();"}
			</TD>
			<TD colspan="2" class="text-right nobr">
				{button icon="delete" id="delete-message-templates" tip="Delete" label="Delete"}
				<label>
					{trans("Check All")}
					<INPUT TYPE="checkbox" NAME="allbox" class="lms-ui-multi-check-all" VALUE="1">
				</label>
			</TD>
		</TR>
	</TFOOT>
</TABLE>

{include file='message/messagetemplatedialog.html' operation="add"}
{include file='message/messagetemplatedialog.html' operation="edit"}

<script>
	$(function() {
		$('#delete-message-templates').click(function() {
			if ($(this).closest('tfoot').siblings('tbody.lms-ui-multi-check').find('input:checked').length) {
				confirmDialog($t("Are you sure, you want to delete all selected message templates?"), this).done(function() {
					$('form#messagetemplates').attr('action', '?m=messagetemplatedel').submit();
				});
				return false;
			}
		});

		$('.delete-messagetemplate').click(function() {
			confirmDialog($t("Are you sure, you want to delete that message template?"), this).done(function() {
				location.href = $(this).attr('href');
			});
			return false;
		});

		$('[data-template-type],[data-template-name],[data-template-subject],.data-template-message-area').click(function() {
			var templateId = $(this).closest('[data-template-id]').attr('data-template-id');
			open_edit_dialog(templateId);
		});

		$('#message-templates .lms-ui-hint-toggle').on('tooltipopen', function(e, ui) {
			initDocumentViewers($(ui.tooltip[0]).find('.documentview'));
		});
	});
</script>
{/block}
