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

<style>

	@media screen and (max-width: 1200px) {
		.ticket-deadline,
		.ticket-events {
			display: none;
		}
	}

	.ticket-list-buttons > div {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		flex-wrap: wrap;
	}

	@media screen and (max-width: 1000px) {
		.ticket-list-buttons > div {
			flex-direction: column;
			align-items: flex-end;
		}
	}

	.ticket-list-buttons > div > * {
		margin-left: 0.2em;
		margin-right: 0.2em;
	}

	.ticket-list-buttons > div > :last-child {
		margin-right: 0.5em;
	}

</style>

<TABLE class="lmsbox lms-ui-background-cycle lms-ui-multi-check">
    <COLGROUP>
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 50%;">
        <COL style="width: 45%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
        <COL style="width: 1%;">
    </COLGROUP>
    <THEAD>
        {include file="rt/rtfilter.html"}
        <TR>
            <TD class="lms-ui-pagination ticket-list-pagination" COLSPAN="6">
                {if $pagination->getTotal() != 0}
                {include file="pagination.html" form="filter-form"}
                {/if}
            </TD>
			<TD class="lms-ui-pagination text-right ticket-list-buttons" colspan="4">
				<div>
					<label>
						<input type="checkbox" class="lms-ui-multi-check-all">
						{trans("Check All")}
					</label>
					{button icon="fas fa-reply-all" class="group-action-run" label="Run group action"}
					{if ConfigHelper::checkPrivilege('helpdesk_advanced_operations')}
						{button icon="delete" class="group-action-delete" label="Delete"}
						{button icon="delete" class="group-action-delete-permanently" label="Delete permanently"}
					{/if}
					{button icon="add" href="?m=rtticketadd{if $filter.ids}{if !is_array($filter.ids)}&id={$filter.ids}{else}{if count($filter.ids) == 1}&id={$filter.ids[0]}{/if}{/if}{/if}"
						label="New Ticket"}
				</div>
			</TD>
		</TR>
        {include file="rt/rtheader.html"}
    </THEAD>
    <TBODY>
        {include file="rt/rtqueueviewbox.html"}
    </TBODY>
    <TFOOT>
        <TR>
            <TD class="lms-ui-pagination ticket-list-pagination" COLSPAN="6">
                {if $pagination->getTotal() != 0}
                {include file="pagination.html" form="filter-form"}
                {/if}
            </TD>
			<TD class="lms-ui-pagination text-right ticket-list-buttons" colspan="4">
				<div>
					<label>
						<input type="checkbox" class="lms-ui-multi-check-all">
						{trans("Check all")}
					</label>
					{button icon="read" class="group-action-mark-as-read" label="Mark as read"}
					{button icon="status-resolved" class="group-action-status-resolved" label="Resolve"}
					{button icon="reply-all" class="group-action-run" label="Run group action"}
					{if ConfigHelper::checkPrivilege('helpdesk_advanced_operations')}
						{button icon="delete" class="group-action-delete" label="Delete"}
						{button icon="delete" class="group-action-delete-permanently" label="Delete permanently"}
					{/if}
					{button icon="add" href="?m=rtticketadd{if $filter.ids}{if !is_array($filter.ids)}&id={$filter.ids}{else}{if count($filter.ids) == 1}&id={$filter.ids[0]}{/if}{/if}{/if}"
						label="New Ticket"}
				</div>
			</TD>
		</TR>
    </TFOOT>
</TABLE>

<form id="ticket-list" method="GET">
	<input type="hidden" name="m" value="rtticketedit">
	<input type="hidden" name="action" value="resolve">
</form>

<script>

	function get_backid(elem) {
		var ticket = $(elem).closest('.single-ticket').prev('.single-ticket');
		if (ticket.length) {
			return '&backid=' + ticket.attr('id');
		}
		return '';
	}

	$(function() {
		function disableLink() {
			$('.group-action-run,.group-action-delete,.group-action-status-resolved').prop('disabled', !$('.lms-ui-multi-check:checked').length);
			$('.group-action-delete-permanently').prop('disabled', !$('.lms-ui-multi-check:checked[data-deleted="1"]').length);
			$('.group-action-mark-as-read').prop('disabled', !$('.lms-ui-multi-check:checked[data-unread="1"]').length);
		};
		disableLink();

		$('.lms-ui-multi-check:checkbox,.lms-ui-multi-check-all:checkbox').click(function () {
			disableLink();
		});

		$('.group-action-run').click(function () {
			var ticketid = [];
			$('[name="ticketid[]"]:checked').each(function () {
				ticketid.push($(this).val());
			});
			location.href = '?m=rtmessageadd&ticketid[]=' + ticketid.join('&ticketid[]=');
		});

		$('.group-action-status-resolved').click(function () {
			var ticketid = [];
			var form = $("#ticket-list");
			$('[name="ticketid[]"]:checked').each(function () {
				form.append('<input type="hidden" name="id[]" value="' + $(this).val() + '">');
			});
			form.submit();
		});

		$('.group-action-delete').click(function () {
			confirmDialog($t('Are you sure, you want to remove all selected tickets?'), this).done(function() {
				var ticketid = [];
				$('[name="ticketid[]"]:checked').each(function () {
					ticketid.push($(this).val());
				});
				location.href = '?m=rtticketdel&taction=delete&id[]=' + ticketid.join('&id[]=');
			});
		});

		$('.group-action-delete-permanently').click(function () {
			confirmDialog($t('Are you sure, you want to permanently remove all selected tickets?'), this).done(function() {
				var ticketid = [];
				$('[name="ticketid[]"]:checked[data-deleted="1"]').each(function () {
					ticketid.push($(this).val());
				});
				location.href = '?m=rtticketdel&taction=delperm&id[]=' + ticketid.join('&id[]=');
			});
		});

		$('.group-action-mark-as-read').click(function () {
			var ticketid = [];
			$('[name="ticketid[]"][data-unread="1"]:checked').each(function () {
				ticketid.push($(this).val());
			});
			location.href = '?m=rtticketedit&action=read&id[]=' + ticketid.join('&id[]=');
		});

		$('.delete-ticket').click(function() {
			var link = $(this).attr('href');
			var fulllink = link + get_backid(this);
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to remove ticket $a?", name), this).done(function() {
				location.href = fulllink;
			});
			return false;
		});

		$('.restore-ticket').click(function() {
			var link = $(this).attr('href');
			var fulllink = link + get_backid(this);
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to restore ticket $a?", name), this).done(function() {
				location.href = fulllink;
			});
			return false;
		});

		$('.remove-ticket').click(function() {
			var link = $(this).attr('href');
			var fulllink = link + get_backid(this);
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to permanently remove ticket $a?", name), this).done(function() {
				location.href = fulllink;
			});
			return false;
		});

		$('.ticket-image-gallery').click(function() {
			var btn = this;
			$.ajax({
				url: "?m=rtticketview&api=1&ajax=1&op=get-image-gallery&id=" +
					parseInt($(this).closest('tr').attr('data-ticket-id')),
				method: "GET",
				dataType: "json",
				success: function(data) {
					if (data.length) {
						showGallery(data);
					} else {
						$(btn).hide();
					}
				}
			});
			return false;
		});

		$(window).resize(function() {
			if (parseInt($(this).outerWidth()) <= 1200) {
				$('.ticket-list-filter').attr('colspan', 8);
				$('.ticket-list-pagination').attr('colspan', 4);
				$('.ticket-list-buttons').attr('colspan', 4);
			} else {
				$('.ticket-list-filter').attr('colspan', 10);
				$('.ticket-list-pagination').attr('colspan', 6);
				$('.ticket-list-buttons').attr('colspan', 4);
			}
		}).resize();
	});

</script>
{/block}
