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

	.lms-ui-button-fileupload {
		margin-top: 0.5em;
	}

	#messageitems-filter > *:not(:first-child) {
		margin-left: 0.5em;
	}

	#summary-table {
		width: auto;
	}

	#summary-table thead {
		border: 1px dotted grey;
	}

	#summary-table thead td {
		font-weight: bold;
	}

	#summary-table tbody {
		border: 1px dotted grey;
	}

	#summary-table td:not(:first-child) {
		border-left: 1px dotted grey;
	}

	#summary-table tr:not(:first-child) td {
		border-top: 1px dotted grey;
	}

	#resend-popup .title {
		font-weight: bold;
		font-size: 120%;
		margin-bottom: 0.5em;
	}

	#resend-popup ul {
		list-style-type: none;
		margin-block-start: 0;
		margin-block-end: 0;
		padding-inline-start: 0;
	}

	#resend-popup li {
		/*font-weight: bold;*/
		margin-top: 0.3em;
		margin-bottom: 0.3em;
	}

	#resend-popup .action-buttons {
		margin-top: 0.7em;
		margin-bottom: 0;
		display: flex;
		justify-content: center;
	}

</style>

<form id="resend-message-form" method="post" action="?m=messageadd&messageid={$message.id}">
	{$customers = array()}
	{foreach $itemlist as $item}
		{if !empty($item.customerid) && !isset($customers[$item.customerid])}
			{$customers[$item.customerid] = true}
			<input type="hidden" name="customers[{$item.customerid}]" value="{$item.customerid}"
				{if $item.status == $smarty.const.MSG_ERROR}data-message-error="1"{/if}
				{if $item.status == $smarty.const.MSG_NEW || $item.status == $smarty.const.MSG_ERROR || $item.status == $smarty.const.MSG_BOUNCED}data-message-unsent="1"{/if}
			>
		{/if}
	{/foreach}
</form>

<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 98%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{if $message.type == $smarty.const.MSG_SMS}
					{$icon = "sms"}
				{elseif $message.type == $smarty.const.MSG_WWW}
					{$icon = "www"}
				{elseif $message.type == $smarty.const.MSG_USERPANEL || $message.type == $smarty.const.MSG_USERPANEL_URGENT}
					{$icon = "userpanel"}
				{else}
					{$icon = "mail"}
				{/if}
				{icon name=$icon}
			</td>
			<td class="nobr">
				<strong>{trans("Subject")}</strong>
			</td>
			<td>
				{$message.subject|escape}
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="user"}
			</td>
			<td>
				<strong>{trans("User")}</strong>
			</td>
			<td>
				{if $message.userid}
					<a href="?m=userinfo&id={$message.userid}">{$message.username|escape}</a>
				{else}
					{LMS::SOFTWARE_NAME}
				{/if}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="calendar"}
			</td>
			<td>
				<strong>{trans("Creation date")}</strong>
			</td>
			<td>
				{$message.cdate|date_format:"Y-m-d H:i"}
			</td>
		</tr>
		{if !empty($message.startdate)}
			<tr>
				<td>
					{icon name="calendar"}
				</td>
				<td class="nobr">
					<strong>{trans("Start date")}</strong>
				</td>
				<td{if $message.startdate < $smarty.now} class="blend"{/if}>
					{$message.startdate|date_format:"Y-m-d H:i"}
				</td>
			</tr>
		{/if}
		{if !empty($lastchanges[$smarty.const.MSG_SENT])}
			<tr>
				<td>
					{icon name="success"}
				</td>
				<td class="nobr">
					<strong>{trans("Last success")}</strong>
				</td>
				<td>
					{$lastchanges[$smarty.const.MSG_SENT]|date_format:"Y-m-d H:i"}
				</td>
			</tr>
		{/if}
		{if !empty($lastchanges[$smarty.const.MSG_DELIVERED])}
			<tr>
				<td>
					{icon name="mail"}
				</td>
				<td class="nobr">
					<strong>{trans("Last delivery")}</strong>
				</td>
				<td>
					{$lastchanges[$smarty.const.MSG_DELIVERED]|date_format:"Y-m-d H:i"}
				</td>
			</tr>
		{/if}
		{if !empty($lastchanges[$smarty.const.MSG_ERROR])}
			<tr>
				<td>
					{icon name="error"}
				</td>
				<td class="nobr">
					<strong>{trans("Last error")}</strong>
				</td>
				<td>
					{$lastchanges[$smarty.const.MSG_ERROR]|date_format:"Y-m-d H:i"}
				</td>
			</tr>
		{/if}
		{if $message.sender}
			<tr>
				<td>
					{icon name="mail"}
				</td>
				<td>
					<strong>{trans("Sender")}</strong>
				</td>
				<td>
					{$message.sender|escape:"html"}
				</td>
			</tr>
		{/if}
		<tr>
			<td>
				{icon name="edit"}
			</td>
			<td class="nobr">
				<strong>{trans("Message body")}</strong>
			</td>
			<td>
				<table style="width: 100%;" cellpadding="5">
					<tr>
						<td class="fall superlight">
							{if $message.contenttype == 'text/plain'}
								{nl2br(htmlspecialchars($message.body), false)}
							{elseif $message.contenttype == 'text/html'}
								{$message.body}
							{else}
								{nl2br($message.body, false)}
							{/if}
						</td>
					</tr>
				</table>
			</td>
		</tr>
		{if $filecontainers}
			<tr>
				<td class="valign-top">
					{icon name="attachment"}
				</td>
				<td class="valign-top">
					<strong>{trans("Attachments")}</strong>
				</td>
				<td>
					{foreach $filecontainers as $container}
							{foreach $container.files as $file}
								{documentview id="{$container.id}-{$file.id}" type=$file.contenttype name=$file.filename
									url="?m=attachments&attachmentaction=viewfile&type=messageid&id={$container.id}&fileid={$file.id}" external=true}
								<br>
							{/foreach}
						{button id="downloadzippedcontainer" onclick="location.href = '?m=attachments&attachmentaction=downloadzippedcontainer&type=messageid&id={$container.id}';" label="Download as zip archive" icon="download"}
					{/foreach}
				</td>
			</tr>
		{/if}
		{if !empty($summary)}
			<tr>
				<td class="valign-top">
					{icon name="stats"}
				</td>
				<td class="valign-top">
					<strong>{trans("Summary")}</strong>
				</td>
				<td>
					<table id="summary-table" class="lmsbox lms-ui-background-cycle">
						<thead>
							<tr>
								<td>
									{trans("Status")}
								</td>
								<td>
									{trans("Volume")}
								</td>
							</tr>
						</thead>
						<tbody>
							{foreach $summary as $status => $count}
								<tr class="highlight {$_MESSAGESTATUSES[$status].class}">
									<td>
										{$_MESSAGESTATUSES[$status].label}
									</td>
									<td>
										{$count}
									</td>
								</tr>
							{/foreach}
						</tbody>
					</table>
				</td>
			</tr>
		{/if}
		<tr>
			<td colspan="3" class="lms-ui-box-buttons">
				{if ConfigHelper::checkPrivileges('messaging', 'messaging_removal')}
					{button id="delete-message" icon="delete" label="Delete"}
				{/if}
				{if (!empty($summary[$smarty.const.MSG_NEW]) || !empty($message.startdate) && !empty($summary[$smarty.const.MSG_ERROR]))
					&& ConfigHelper::checkPrivileges('messaging', 'messaging_modification')}
					{button icon="cancel" label="Cancel" href="?m=messagecancel&id={$message.id}"}
				{/if}
				{if !empty($itemerrorlist) && ConfigHelper::checkPrivileges('messaging', 'messaging_modification')}
					{button icon="reload" label="Increase attempts" href="?m={$layout.module}&id={$message.id}&action=increase-attempts"}
				{/if}
				{if ConfigHelper::checkPrivileges('messaging', 'messaging_creation')}
					{capture assign="content"}
						<div id="resend-popup">
							<div class="title">
								{icon name="customer"}
								<strong>{trans("Recipients")}</strong>
							</div>
							<div>
								<ul>
									<li>
										<label>
											<input type="radio" name="recipients" value="all">
											{trans("selection from filter")}
										</label>
									</li>
									<li>
										<label>
											<input type="radio" name="recipients" value="message-all">
											{trans("all recipient of this message")}
										</label>
									</li>
									<li>
										<label>
											<input type="radio" name="recipients" value="message-unsent" checked>
											{trans("recipients who haven't received this message")}
										</label>
									</li>
									<li>
										<label>
											<input type="radio" name="recipients" value="message-error">
											{trans("recipients with delivery errors")}
										</label>
									</li>
								</ul>
							</div>
							<div class="action-buttons">
								{button icon="submit" label="<!next-step>Next" id="resend-button"}
							</div>
						</div>
					{/capture}
					{hint icon="reload" label="Resend" class="lms-ui-link-button" id="resend-message-button" content=$content}
				{/if}
			</td>
		</tr>
	</tbody>
</table>

<br>

<form method="POST" name="page" id="messageitems" action="?m=messageinfo&id={$message.id}">
</form>

{if empty($message.startdate)}
	{$colspan=6}
{else}
	{$colspan=7}
{/if}
<table class="lmsbox lms-ui-background-cycle">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 94%;">
		<col style="width: 1%;">
		{if !empty($message.startdate)}
			<col style="width: 1%;">
		{/if}
		<col style="width: 1%;">
		<col style="width: 1%;">
	</colgroup>
	<thead>
		<tr>
			<td class="nobr">
				<a href="?m=messageinfo&id={$message.id}&o=customer{if $listdata.direction == "asc" && $listdata.order == "customer"},desc{/if}">
					{trans("Customer")}
				</a>
				{if $listdata.order == "customer"}
					<img src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif">
				{/if}
			</td>
			<td class="nobr">
				<a href="?m=messageinfo&id={$message.id}&o=destination{if $listdata.direction == "asc" && $listdata.order == "destination"},desc{/if}">
					{trans("Destination")}
				</a>
				{if $listdata.order == "destination"}
					<img src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif">
				{/if}
			</td>
			<td class="text-right nobr">
				<a href="?m=messageinfo&id={$message.id}&o=status{if $listdata.direction == "asc" && $listdata.order == "status"},desc{/if}">
					{trans("Status")} ({trans("Description")})
				</a>
				{if $listdata.order == "status"}
					<img src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif">
				{/if}
			</td>
			<td class="text-right nobr">
				{trans("Last change")}
			</td>
			{if !empty($message.startdate)}
				<td class="text-right nobr">
					{trans("Attempts left")}
				</td>
			{/if}
			<td class="text-right nobr">
				{trans("Last read")}
			</td>
			<td class="nobr">
				<strong>{t a=$listdata.total}Total: $a{/t}</strong>
			</td>
		</tr>
		<tr>
			<td colspan="{$colspan}" class="nobr" id="messageitems-filter">
				{icon name="filter"}
				<input type="text" name="search" form="messageitems" value="{$listdata.search}" {tip text="Enter value of searching data and select category"}>
				<select size="1" name="cat" form="messageitems">
					<option value=""{if $listdata.cat == ""} SELECTED{/if}>{trans("— select filter category —")}</option>
					<option value="name"{if $listdata.cat == "name"} SELECTED{/if}>{trans("customer name")}</option>
					<option value="customerid"{if $listdata.cat == "customerid"} SELECTED{/if}>{trans("customer ID")}</option>
					<option value="destination"{if $listdata.cat == "destination"} SELECTED{/if}>{trans("destination number/email")}</option>
				</select>
				<span>{trans("Status")}</span>
				<select size="1" name="status" form="messageitems">
					<option value=""{if !$listdata.status} selected{/if}>{trans("— all statuses —")}</option>
					<option value="{$smarty.const.MSG_NEW}"{if $listdata.status == $smarty.const.MSG_NEW} selected{/if}>{trans("waiting<!plural>")}</option>
					<option value="{$smarty.const.MSG_READY_TO_SEND}"{if $listdata.status == $smarty.const.MSG_READY_TO_SEND} selected{/if}>{trans("ready to send<!plural>")}</option>
					{*
					<option value="{$smarty.const.MSG_DRAFT}"{if $listdata.status == $smarty.const.MSG_DRAFT} selected{/if}>{trans("drafts")}</OPTION>
					*}
					<option value="{$smarty.const.MSG_SENT}"{if $listdata.status == $smarty.const.MSG_SENT} selected{/if}>{trans("sent<!plural>")}</option>
					<option value="{$smarty.const.MSG_ERROR}"{if $listdata.status == $smarty.const.MSG_ERROR} selected{/if}>{trans("errornous<!plural>")}</option>
					<option value="{$smarty.const.MSG_DELIVERED}"{if $listdata.status == $smarty.const.MSG_DELIVERED} selected{/if}>{trans("delivered<!plural>")}</option>
					<option value="{$smarty.const.MSG_CANCELLED}"{if $listdata.status == $smarty.const.MSG_CANCELLED} selected{/if}>{trans("cancelled<!plural>")}</option>
					<option value="{$smarty.const.MSG_BOUNCED}"{if $listdata.status == $smarty.const.MSG_BOUNCED} selected{/if}>{trans("bounced<!plural>")}</option>
				</select>
				<a href="javascript: document.page.submit();">&raquo;&raquo;&raquo;</a>
			</td>
		</tr>
		{if $listdata.total != 0}
			<tr>
				<td class="lms-ui-pagination" colspan="{$colspan}">
					{include file="scroller.html" loop=$itemlist scrollerno=1}
				</td>
			</tr>
		{/if}
	</thead>
	<tbody>
		{section name=itemlist loop=$itemlist start=$start max=$pagelimit}
			{$item = $itemlist[itemlist]}
			<tr class="highlight{if $item.body} showitem{/if} {$_MESSAGESTATUSES[$item.status].class}" data-itemid="{$item.id}">
				<td class="nobr valign-top">
					{if $item.customerid}
						<a href="?m=customerinfo&id={$item.customerid}">{$item.customer|truncate:45:"&hellip;":true} ({$item.customerid|string_format:"%04d"})</a>
					{else}
						{trans("none")}
					{/if}
				</td>
				<td class="nobr valign-top">
					{trans("{$item.destination|escape}")}
					{if $item.body}
						<div id="{$item.id}" style="display: none;" class="fall superlight">
							{if $message.contenttype == 'text/plain'}
								{nl2br(htmlspecialchars($item.body), false)}
							{elseif $message.contenttype == 'text/html'}
								{$item.body}
							{else}
								{nl2br($item.body, false)}
							{/if}
						</div>
					{/if}
				</td>
				<td class="text-right valign-top">
					{if $item.status == $smarty.const.MSG_SENT}
						{trans("sent")}
						{if $item.error} ({$item.error|escape}){/if}
					{elseif $item.status == $smarty.const.MSG_ERROR}
						{trans("errornous")}
						{if $item.error} ({$item.error|escape}){/if}
					{elseif $item.status == $smarty.const.MSG_DELIVERED}
						{trans("delivered")}
					{elseif $item.status == $smarty.const.MSG_CANCELLED}
						{trans("cancelled")}
					{elseif $item.status == $smarty.const.MSG_BOUNCED}
						{trans("bounced")}
					{elseif $item.status == $smarty.const.MSG_READY_TO_SEND}
						{trans("ready to send")}
					{else}
						{trans("waiting")}
					{/if}
				</td>
				<td class="text-right nobr">
					{if $item.lastdate}{$item.lastdate|date_format:"Y-m-d H:i"}{else} - {/if}
				</td>
				{if !empty($message.startdate)}
					<td class="text-right nobr">
						{if !empty($item.attempts)}
							{icon name="reload" class="fa-fw"}
						{/if}
						{$item.attempts}
					</td>
				{/if}
				<td class="text-right valign-top nobr">
					{if $item.lastreaddate}{$item.lastreaddate|date_format:"Y-m-d H:i"}{else} - {/if}
				</td>
				<td class="text-right nobr valign-top">
					{if $item.body}
						{button type="link" icon="info" tip="Show message body"}
					{/if}
				</td>
			</tr>
		{sectionelse}
			<tr>
				<td colspan="{$colspan}" class="empty-table">
					{trans("No such recipients in database.")}
				</td>
			</tr>
		{/section}
	</tbody>
	<tfoot>
		{if $listdata.total != 0}
			<tr>
				<td class="lms-ui-pagination" colspan="{$colspan}">
					{include file="scroller.html" loop=$itemlist scrollerno=2}
				</td>
			</tr>
		{/if}
		<tr>
			<td colspan="{$colspan}" class="text-right">
				<strong>{t a=$listdata.total}Total: $a{/t}</strong>
			</td>
		</tr>
	</tfoot>
</table>

<script>

	$(function() {
		$('.showitem').on('click', function () {
			var itemid = $(this).attr('data-itemid');
			$('#'+itemid).toggle();
		});

		$('#delete-message').click(function() {
			confirmDialog($t("Are you sure, you want to delete that message?"), this).done(function() {
				location.href =  '?m=messagedel&id={$message.id}';
			});
			return false;
		});

		var customers = $('#resend-message-form [name^="customers"]');

		$(document).on('click', '#resend-button', function() {
			var recipients = $('[name="recipients"]:checked').val();

			switch (recipients) {
				case 'all':
					customers.prop('disabled', true);
					break;
				case 'message-all':
					customers.prop('disabled', false);
					break;
				case 'message-unsent':
					customers.each(function() {
						$(this).prop('disabled', !$(this).is('[data-message-unsent]'));
					});
					break;
				case 'message-error':
					customers.each(function() {
						$(this).prop('disabled', !$(this).is('[data-message-error]'));
					});
					break;
			}

			$('#resend-message-form').submit();
		});

		$('#resend-message-button').on('lms:hint:ready', function() {
			$('[name="recipients"]').filter('[value="message-unsent"],[value="message-error"]').each(function() {
				var itemStatus = $(this).val();
				var disabled = !customers.filter('[data-' + itemStatus + ']').length;
				$(this).prop('disabled', disabled).closest('label').toggleClass('lms-ui-disabled', disabled);
				if (itemStatus === 'message-unsent' && disabled) {
					$(this).closest('li').prev('li').find('input').prop('checked', true);
				}
			});
		});
	});

</script>
{/block}
