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

<style>

	div.lms-ui-filter-container {
		justify-content: flex-start;
	}

	div.lms-ui-filter-container > div.lms-ui-filter-definition:not(:first-child) {
		flex-direction: column;
		align-items: flex-start;
	}

	div.lms-ui-filter-container > div.lms-ui-filter-definition > label > span {
		margin-right: 0.5em;
	}

</style>

<form method="POST" name="page" id="filter" action="">
	<input type="submit" class="hiddenbtn">
</form>
<table class="lmsbox lms-ui-background-cycle">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 97%;">
	</colgroup>
	<thead>
		<tr>
			<td class="container" colspan="4">
				<div class="lms-ui-filter-container">
					<div class="lms-ui-filter-definition">
						{icon name="filter"}
						<label>
							<span>{trans("From")}</span>
							<input type="text" size="12" name="datefrom" form="filter" data-default-value=""
								value="{$listdata.datefrom|date_format:"Y/m/d"}" placeholder="{trans("yyyy/mm/dd")}"
								{tip class="lms-ui-date" text="Enter date in 'yyyy/mm/dd' format (empty field means current date) or click to choose it from calendar" trigger="datefrom"}>
						</label>
						<label>
							<span>{trans("To")}</span>
							<input type="text" size="12" name="dateto" form="filter" data-default-value=""
								value="{$listdata.dateto|date_format:"Y/m/d"}" placeholder="{trans("yyyy/mm/dd")}"
								{tip class="lms-ui-date" text="Enter date in 'yyyy/mm/dd' format (empty field means current date) or click to choose it from calendar" trigger="dateto"}>&nbsp;
						</label>
					</div>
					<div class="lms-ui-filter-definition">
						<label>
							<span>{trans("User")}</span>
							<select size="1" name="user" form="filter" data-default-value="">
								<option value="">{trans("— all —")}</option>
								{foreach $listdata.users as $idx => $user}
									<option value="{$idx}"{if $listdata.user == $idx} selected{/if}>{$user.login}</option>
								{/foreach}
							</select>
						</label>
						<label>
							<span>{trans("Module")}</span>
							<select name="module" form="filter" class="lms-ui-advanced-select-test" data-default-value="">
								<option value="">{trans("— any —")}</option>
								{foreach $listdata.modules as $module}
									<option value="{$module}"{if $listdata.module == $module} selected{/if}>{$module}</option>
								{/foreach}
							</select>
						</label>
					</div>
					<div class="lms-ui-filter-definition">
						<label>
							<span>{trans("Resource Type")}</span>
							<select size="1" name="resourcetype" form="filter" data-default-value="">
								<option value="">{trans("— all —")}</option>
								{foreach SYSLOG::getAllResources() as $idx => $resourcetype}
									<option value="{$idx}"
										{if $listdata.resourcetype == "{$idx}"} selected{/if}>{$resourcetype}</option>
								{/foreach}
							</select>
						</label>
						<label>
							<span>{trans("Resource ID")}</span>
							<input type="text" size="5" name="resourceid" form="filter" data-default-value=""
								value="{if $listdata.resourceid}{$listdata.resourceid}{/if}"
								{tip text="Enter resource ID (empty means any ID)" trigger="resourceid"}>
						</label>
					</div>
					<div class="lms-ui-filter-definition">
						<label>
							<span>{trans("Property Name")}</span>
							<select size="1" name="propertyname" form="filter" data-default-value="">
								<option value="">{trans("— all —")}</option>
							</select>
						</label>
						<label>
							<span>{trans("Property Value")}</span>
							<input type="text" size="20" name="propertyvalue" form="filter" data-default-value=""
								value="{if $listdata.propertyvalue}{$listdata.propertyvalue}{/if}"
								{tip text="Enter property value (empty means any value)" trigger="propertyvalue"}>
						</label>
					</div>
					{button type="link" icon="clear" name="clear" id="clear-button"}
					{button type="link" icon="next" id="submit-button"}
				</div>
			</td>
		</tr>
		<tr>
			<td>
				{if $listdata.page}<a href="?m=archiveview&page={$listdata.page - 1}">&laquo;&laquo;&laquo; {trans("Next")}</a>{else}&nbsp;{/if}
			</td>
			<td colspan="3" class="text-right">
				{if $listdata.prev}<a href="?m=archiveview&page={$listdata.page + 1}">{trans("Previous")} &raquo;&raquo;&raquo;</a>{else}&nbsp;{/if}
			</td>
		</tr>
		<tr>
			<td class="text-center nobr">
				<strong>{trans("Date")}</strong>
			</td>
			<td class="text-center nobr">
				<strong>{trans("User")}</strong>
			</td>
			<td class="text-center nobr">
				<strong>{trans("Module")}</strong>
			</td>
			<td class="nobr">
				<strong>{trans("Operations")}</strong>
			</td>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<td>
				{if $listdata.page}<a href="?m=archiveview&page={$listdata.page - 1}">&laquo;&laquo;&laquo; {trans("Next")}</a>{else}&nbsp;{/if}
			</td>
			<td colspan="3" class="text-right">
				{if $listdata.prev}<a href="?m=archiveview&page={$listdata.page + 1}">{trans("Previous")} &raquo;&raquo;&raquo;</a>{else}&nbsp;{/if}
			</td>
		</tr>
	</tfoot>
	<tbody>
		{foreach $transactions as $tr}
		<tr class="highlight">
			<td class="valign-bottom nobr">
				{$tr.time|date_format:"Y.m.d H:i:s"}
			</td>
			<td class="text-center valign-bottom nobr">
				<a href="?m=userinfo&id={$tr.userid}">{$tr.login}</a>
			</td>
			<td class="text-center valign-bottom nobr">
				{$tr.module}
			</td>
			<td class="valign-bottom">
				{if $tr.messages}
					{foreach $tr.messages as $messageid => $message}
						<strong>{SYSLOG::getResourceName($message.resource)}: <span style="{SYSLOG::getOperationStyle($message.operation)}">{SYSLOG::getOperationName($message.operation)}</span></strong>
						{foreach $message.keys as $key => $msgkey}{$key}: {if $msgkey.value}<a href="?m=archiveinfo&type={$msgkey.type}&id={$msgkey.value}&amp;date={$tr.time}">{/if}{$msgkey.value}{if $msgkey.value}</a>{/if}{if !$msgkey@last}, {/if}{/foreach}
						{foreach $message.data as $key => $value}{$key}: {$value.value}{if !$value@last}, {/if}{/foreach}
						{if !$message@last}<br>{/if}
					{/foreach}
				{/if}
			</td>
		</tr>
		{foreachelse}
		<tr>
			<td class="empty-table" colspan="4">
				{trans("No such transactions in database.")}
			</td>
		</tr>
		{/foreach}
	</tbody>
</table>
<script>

	function GetPropertyValues(propertyValue) {
		var resourceType = $('[name="resourcetype"]').val();
		var propertyName = $('[name="propertyname"]').val();
		if (typeof(propertyValue) == 'undefined') {
			propertyValue = '';
		}
		$.ajax({
			method: "GET",
			dataType: "json",
			data: {
				api: 1,
				action: "get-property-values",
				"resource-type": resourceType,
				"property-name": propertyName
			},
			success: function (data) {
				var html;
				if (data.length > 19) {
					html = '<input type="text" size="20" name="propertyvalue" form="filter" data-default-value=""' +
							(propertyValue.length ? ' value="' + propertyValue + '"' : '') + '>';
				} else {
					html = '<select size="1" name="propertyvalue" form="filter" data-default-value="">' +
						'<option value="">{trans('— all —')}</option>';
						$.each(data, function(index, elem) {
							html += '<option value="' + elem.value + '"' +
								(propertyValue.length && propertyValue == elem.value ? ' selected' : '') + '>' +
								elem.label + '</option>';
						});
					html += '</select>';
				}
				$('[name="propertyvalue"]').replaceWith(html);
			}
		});
	}

	function GetPropertyNames(params) {
		var resourceType = $('[name="resourcetype"]').val();
		if (resourceType.length) {
			$('[name="propertyname"],[name="propertyvalue"]').prop('disabled', true);
			$.ajax({
				method: "GET",
				dataType: "json",
				data: {
					api: 1,
					action: "get-property-names",
					"resource-type": resourceType
				},
				success: function(data) {
					var html = '<option value="">{trans("— all —")}</option>';
					$.each(data, function(index, value) {
						html += '<option value="' + value + '"' +
							(typeof(params) == 'object' && params.hasOwnProperty('propertyname') && params.propertyname == value ? ' selected' : '') +
							'>' + value + '</option>';
					});
					$('[name="propertyname"]').html(html).prop('disabled', false);
					if (typeof(params) == 'object' && params.hasOwnProperty('propertyvalue')) {
						GetPropertyValues(params.propertyvalue);
					}
				}
			});
		}
	}

	$(function() {
		$("#submit-button").click(function() {
			$("#filter").submit();
		});

		$("#clear-button").click(function() {
			$("[data-default-value]").each(function() {
				$(this).val($(this).attr("data-default-value"));
			});
			$("#filter").submit();
		});

		$('[name="resourcetype"]').change(function() {
			GetPropertyNames();
		})

		$('[name="propertyname"]').change(function() {
			GetPropertyValues();
		});
	});

	GetPropertyNames( { propertyname: '{$listdata.propertyname}', propertyvalue: '{$listdata.propertyvalue}' } );

</script>
{/block}
