{block name=eventschedulefilter}
{$hide_disabled_users = ConfigHelper::checkConfig('timetable.hide_disabled_users', ConfigHelper::checkConfig('phpui.timetable_hide_disabled_users'))}
{$hide_deleted_users = ConfigHelper::checkConfig('timetable.hide_deleted_users', ConfigHelper::checkConfig('phpui.timetable_hide_deleted_users'))}
	<TR>
		<TD class="text-center nobr no-print">
			<A href="?m=eventschedule&day=1&month={if $filter.month == 1}12{else}{math equation="x-1" x=$filter.month}{/if}&amp;year={if $filter.month == 1}{math equation="x-1" x=$filter.year}{else}{$filter.year}{/if}">&laquo;&laquo;&laquo;&nbsp;</A>
			{section name=days loop=$days.day}
				{if !$days.dow[days]}<span class="light">{/if}
				<A href="?m=eventschedule&day={$days.day[days]}&month={$filter.month}&year={$filter.year}">&nbsp;{if $days.sel[days]}
				<B>*{/if}{$days.day[days]|string_format:"%d"}{if $days.sel[days]}*</B>{/if}
				</A>{if !$days.dow[days]}</span>{/if}
			{/section}
			<A href="?m=eventschedule&day=1&month={if $filter.month == 12}1{else}{math equation="x+1" x=$filter.month}{/if}&amp;year={if $filter.month == 12}{math equation="x+1" x=$filter.year}{else}{$filter.year}{/if}">&raquo;&raquo;&raquo;&nbsp;</A>
		</TD>
	</TR>
	<TR>
		<TD class="nobr no-print">
			<div class="lms-ui-filter-container">
				<div class="lms-ui-filter-definition">
					<FORM method="POST" action="?m=eventschedule" name="filter" id="filter">
						<INPUT type="submit" class="hiddenbtn"> <INPUT type="HIDDEN" name="m" value="eventschedule">
						<span class="bold">{trans("Filter:")} </span>
						{trans("Type:")}
						<SELECT name="type[]" onchange="document.filter.submit();" class="lms-ui-multiselect lms-ui-multiselect-filter" multiple data-default-value="{trans("— all —")}">
							{foreach Localisation::arraySort($_EVENTTYPES, 'label') as $id => $eventtype}
								<OPTION value="{$id}"{if is_array($filter.type) && in_array($id, $filter.type)} selected{/if}>{$eventtype.label}</OPTION>
							{/foreach}
						</SELECT>

						{trans("User groups")}
						<select name="usergroups[]" multiple
							class="lms-ui-advanced-select-test auto-width"
							<option value="">{trans("- any -")}</option>
							{foreach $usergroups as $usergroup}
								<option value="{$usergroup.id}" data-users="{$usergroup.users}"
									{if !empty($filter.usergroups) && in_array($usergroup.id, $filter.usergroups)} selected{/if}
									>{$usergroup.name|escape}</option>
							{/foreach}
						</select>

						{trans("User")}
						<label>
							{trans("AND")}
							<input type="hidden" name="userand" value="0">
							<input type="checkbox" name="userand" value="1" onchange="document.filter.submit();"{if isset($filter.userand) && $filter.userand} checked{/if}>
						</label>
						<SELECT size="1" name="userid[]" OnChange="document.filter.submit();"
								class="lms-ui-multiselect lms-ui-multiselect-filter" multiple
								data-default-value="{trans("— all —")}">
							<OPTION value="-1" {if is_array($filter.userid) && in_array('-1', $filter.userid)} selected{/if}
									data-exclusive>— {trans("unassigned")} —</OPTION>
							{foreach $userlist as $user}
								{if (!is_array($filter.owner) || !in_array($user.id, $filter.owner))
									&& ($hide_disabled_users && !$user.access
										|| $hide_deleted_users && $user.deleted)}
									{continue}
								{/if}
								<option value="{$user.id}"
									{if isset($visible_users) && !isset($visible_users[$user.id])} style="display: none;"{/if}
									{if is_array($filter.userid) && in_array($user.id, $filter.userid)}} selected{/if}
									{if !$user.access} data-blend{/if}
									{if $user.deleted} data-crossed{/if}
								>{strip}
									{$user.rname|trunescape:40} ({$user.login})
								{/strip}</option>
							{/foreach}
						</SELECT>

						{trans("Division")}
						<select name="divisionid[]" onchange="document.filter.submit();" multiple
							class="lms-ui-multiselect"
							data-default-value="{trans("— any —")}"
							{tip text="Divisions"}>
							{foreach $divisions as $division}
								{if !$division.status}
									<option value="{$division.id}"
										{if !empty($filter.divisionid) && in_array($division.id, $filter.divisionid)} selected{/if}
										>{$division.label|escape}</option>
								{/if}
							{/foreach}
						</select>

						{trans("Privacy:")}
						<SELECT name="privacy" onchange="document.filter.submit();">
							<OPTION value="0"{if $filter.privacy == "0"} selected{/if}>{trans("— all —")}</OPTION>
							<OPTION value="1"{if $filter.privacy == "1"} selected{/if}>{trans("public")}</OPTION>
							<OPTION value="2"{if $filter.privacy == "2"} selected{/if}>{trans("private")}</OPTION>
						</SELECT>
						{trans("Customer:")}
						{customerlist form="filter" customers=$customers selected=$filter.customerid selectname="customerid" inputname="customerid" customOnChange="document.filter.submit();" customername=false}
						{button label="Today" onclick="location.href='?m=eventschedule&day={$smarty.now|date_format:"d"}&month={$smarty.now|date_format:"m"}&year={$smarty.now|date_format:"Y"}';"}
						<input type="text" name="date" class="lms-ui-date" value="{if $filter.year}{sprintf("%04d/%02d/%02d", $filter.year, $filter.month, $filter.day)}{/if}" onchange="document.filter.submit();">
						{trans("Status:")}
						<SELECT name="closed" onchange="document.filter.submit();">
							<OPTION value="" {if $filter.closed == ""} selected{/if}>{trans("— all —")}</OPTION>
							<OPTION value="0"{if $filter.closed == "0"} selected{/if}>{trans("opened<!plural:event>")}</OPTION>
							<OPTION value="1"{if $filter.closed == "1"} selected{/if}>{trans("closed<!plural:event>")}</OPTION>
						</SELECT>
					</FORM>
				</div>
				{persistent_filter}
			</div>
		</TD>
	</TR>

	<script>

		$(function() {
			$('[name="usergroups[]"]').change(function() {
				var that = $(this);
				var userInGroups = [];
				var users = that.find('option:selected').attr('data-users');
				if (typeof(users) !== 'undefined') {
					userInGroups = that.find('option:selected').attr('data-users').split(',');
				}
				userInGroups = [...new Set(userInGroups)];
				$('[name="userid[]"] option').each(function() {
					$(this).toggle(!userInGroups.length || !that.val().length || $(this).val() == '-1' || userInGroups.indexOf($(this).val()) !== -1);
					$(this).prop('selected', $(this).prop('selected') && (!userInGroups.length || $(this).val() == '-1' || userInGroups.indexOf($(this).val()) !== -1));
				});
				$('[name="userid[]"]').trigger('lms:multiselect:updated').trigger('change');
			});
		});

	</script>
{/block}
