@{
	Layout = "~/Views/Shared/_FunctionLayout.cshtml";
}

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Misc.MailNotificationDataModel>

<script>
	var deleteId;
	var processId;
	var gridScrollHeight;
	var gridScrollOffset = 240;
	var resizeFinished;

	$(document).ready(function () {
		gridScrollHeight = calculateGridScrollHeight();
		setTimeout(function () {
			devGridViewMailNotifications.PerformCallback();
		}, 500);
	});

	$(window).resize(function () {
		clearTimeout(window.resizedFinished);
		window.resizedFinished = setTimeout(function () {
			setGridScrollHeight();
		}, 250);
	});

	function calculateGridScrollHeight() {
		var windowHeight = $(window).height();
		var gridHeaderHeight = $("#devGridViewMailNotifications_DXHeadersRow0").height();
		var gridFooterHeight = $("#devGridViewMailNotifications_DXFooterRow").height();
		return windowHeight - gridHeaderHeight - gridFooterHeight - gridScrollOffset;
	}

	function setGridScrollHeight() {
		gridScrollHeight = calculateGridScrollHeight();
		devGridViewMailNotifications.PerformCallback();
	}

	function onToolbarItemClick(s, e) {
		if (!s || !e) return;
		if (e.item.name == "ToggleColumnChooser") {
			if (devGridViewMailNotifications.IsCustomizationWindowVisible())
				devGridViewMailNotifications.HideCustomizationWindow();
			else
				devGridViewMailNotifications.ShowCustomizationWindow();
		} else if (e.item.name == "ResetSettings") {
			$.ajax({
				type: "POST",
				url: '@Url.Action("DeleteCookiesAndSessionVariables", "Global")',
				data: { cookies: ["mailNotificationGridStateCookie"], sessionVariables: [ "MailNotificationsGridState", "MailNotificationsGridViewState" ] },
				success: function (response) {
					if (response == "success") {
						window.location = window.location;
					}
				}
			});
		}
	}

	function IsExportToolbarCommand(command) {
		return command == "Pdf" || command == "Xlsx" || command == "Xls";
	}

	function editMailNotification(id) {
		if (!id) return;
		$.ajax({
			url: '@Url.Action("EditMailNotification", "Misc")',
			data: { Id: id },
			success: function (response) {
				setTimeout(function () {
					$(".mailNotificationEditContainer").remove();
					$("body").append(response);
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

	function confirmDelete(id) {
		if (!id) return;
		deleteId = id;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetMailNotification", "Misc")',
			data: { Id: id },
			success: function (response) {
				if (response == "notFound") return;
				var mailNotification = JSON.parse(response);
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteMailNotification);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{mailNotification}", mailNotification.NotificationPluginSystemNameDescription));
				$(".dialogTextMailNotification").text($(".dialogTextMailNotification").text().replace("{mailNotification}", mailNotification.NotificationPluginSystemNameDescription));
				popupControl.Show();
			}
		});
	}

	function deleteMailNotification() {
		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteMailNotification", "Misc")',
			data: { Id: deleteId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteMailNotification);
				popupControl.Hide();
				setTimeout(function () {
					devGridViewMailNotifications.PerformCallback();
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

	function confirmProcess(id) {
		if (!id) return;
		processId = id;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetMailNotification", "Misc")',
			data: { Id: id },
			success: function (response) {
				if (response == "notFound") return;
				var mailNotification = JSON.parse(response);
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlProcessMailNotification);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{mailNotification}", mailNotification.NotificationPluginSystemNameDescription));
				$(".dialogTextMailNotificationProcess").text($(".dialogTextMailNotificationProcess").text().replace("{mailNotification}", mailNotification.NotificationPluginSystemNameDescription));
				popupControl.Show();
			}
		});
	}

	function processMailNotification() {
		$.ajax({
			type: "POST",
			url: '@Url.Action("ProcessMailNotification", "Misc")',
			data: { Id: processId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlProcessMailNotification);
				popupControl.Hide();
			},
			error: function () {
				 alert("error occured");
			}
		});
	}
</script>

@Html.Partial("~/Views/Misc/_MailNotificationGridPartial.cshtml", Model)
@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlDeleteMailNotification",
	Content = "<div class='dialogTextMailNotification' style='padding: 12px'>Sind Sie sicher, dass Sie die Benachrichtigung " +
			  "\"{mailNotification}\" löschen möchten?</div>",
	HeaderText = "\"{mailNotification}\" löschen",
	YesFunction = "function (s, e) { deleteMailNotification(); }",
	YesButtonName = "devButtonDeleteMailNotificationYes",
	NoButtonName = "devButtonDeleteMailNotificationNo"
})

@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlProcessMailNotification",
	Content = "<div class='dialogTextMailNotificationProcess' style='padding: 12px'>Sind Sie sicher, dass Sie die Benachrichtigung " +
			  "\"{mailNotification}\" sofort durchführen möchten?</div>",
	HeaderText = "\"{mailNotification}\" löschen",
	YesFunction = "function (s, e) { processMailNotification(); }",
	YesButtonName = "devButtonProcessMailNotificationYes",
	NoButtonName = "devButtonProcessMailNotificationNo"
})