{block name="eventlistbox"}
<style>

    .event-single-sortable-handler {
        cursor: grab;
    }

    a.lms-ui-button.event-info-short-button:not(.lms-ui-link-button) > i:before {
        color: inherit;
    }

</style>
{foreach $daylist as $day}
<TABLE id="event-day-{$day}" class="lmsbox lms-ui-background-cycle event-day-table">
    <COLGROUP>
        <COL style="width: 1%;">
        <COL style="width: 1%; min-width:90px;">
        <COL style="width: 95%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;" class="no-print">
    </COLGROUP>
    <THEAD>
        <TR{if $day == $today} class="superdark"{/if}>
            <TD class="bold nobr">
                {icon name="timetable" label="{t a=trans($day|date_format:"l") b=$day|date_format:"Y-m-d"}<!date-format>\$a (\$b){/t}"}
            </TD>
            <TD colspan="3">
                {if isset($getHolidays[$day]) && $getHolidays[$day]}
                <span class="red">{trans("Holiday:")} {$getHolidays[$day]}</span>
                {/if}
                {button type="link" href="?m=eventsearch&datefrom={$day|date_format:"Y/m/d"}&dateto={$day|date_format:"Y/m/d"}" icon="next"}
            </TD>
            <TD colspan="2" class="text-right nobr">
                <div class="no-print">
					{button icon="add" onclick="window.location.href='?m=eventadd&day={$day|date_format:"d"}&month={$day|date_format:"m"}&year={$day|date_format:"Y"}';"
						external=true label="Add Event"}
					{button icon="print" onclick="window.location.href='?m=eventprint&day={$day}&u={$filter.customerid}{foreach $filter.userid as $userid}&a[]={$userid}{/foreach}{foreach $filter.type as $type}&t[]={$type}{/foreach}&closed={$filter.closed}&privacy={$filter.privacy}';"
						external=true label="Print"}
                </div>
            </TD>
        </TR>
    </THEAD>
    <TBODY class="event-day" data-event-day="{$day}">
        {foreach $eventlist as $event}
            {if $event.date == $day && !isset($event.hide)}
                {include file="event/eventlistboxrow.html" backid="event-day-{$day}"}
            {/if}
        {/foreach}
    </TBODY>
</TABLE>
{/foreach}
<script>

    $('.event-day').sortable({
        axis: 'y',
        items: '.event-single',
        handle: '.event-single-sortable-handler',
        opacity: 0.9,
        placeholder: 'light',
        connectWith: '.event-day',
        helper: function (e,  ui) {
        	$('table.lmsbox').find('.event-single[data-event-id="' + $(ui).attr('data-event-id') + '"]')
                .not(ui).hide();
        	return ui;
        },
		update: function (e, ui) {
			if (ui.sender == null) {
				return;
            }
        	var eventid = ui.item.attr('data-event-id');
			var items = $('.event-single[data-event-id="' + eventid + '"]').not(ui.item);
			var day = ui.item.closest('.event-day-table').next('.event-day-table');
			items.each(function() {
				if (day.length) {
					day.find('.event-day').append(this);
					day = day.next('.event-day-table');
					$(this).show();
				}
            });
			var todate = ui.item.closest('.event-day').attr('data-event-day');
			var fromdate = ui.sender.attr('data-event-day');
			var fd = new Date(fromdate * 1000);
			var td = new Date(todate * 1000);
			if (confirm($t('Are you sure you want to move event from $a to $b?',
				sprintf("%04d/%02d/%02d", fd.getFullYear(), fd.getMonth() + 1, fd.getDate()),
				sprintf("%04d/%02d/%02d", td.getFullYear(), td.getMonth() + 1, td.getDate())
			))) {
				var delta = todate - fromdate;
				$('html,body').css('cursor', 'wait');
				$.ajax('?m={$layout.module}&api=1', {
					method: "GET",
					data: {
						action: 'eventmove',
						id: eventid,
						delta: delta
					},
					complete: function () {
						$('html,body').css('cursor', 'auto');
					}
				});
			} else {
				$(ui.sender).sortable('cancel');
			}
		}
    });

</script>
{/block}
