@using GreenTree.Nachtragsmanagement.Web.Models.Global;

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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Admin.Plugins.PluginDataModel>

<script>
	var gridScrollHeight;
	var gridScrollOffset = 100;
	var resizeFinished;

	$(document).ready(function () {
		gridScrollHeight = calculateGridScrollHeight();
		setTimeout(function () {
			devGridViewPlugin.PerformCallback();
		}, 500);
		$.ajaxSetup({
			cache: false
		});
	});

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

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

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

	function toggleInstallation(pluginSystemName, installationType) {
		if (!pluginSystemName || !installationType) return;
		if (installationType == "install") {
			$.ajax({
				type: "POST",
				url: '@Url.Action("InstallPlugin", "Admin")',
				data: { pluginName: pluginSystemName },
				success: function (response) {
					if (response == "success") {
						devLoadingPanelRestart.Show();
						$.ajax({
							type: "GET",
							url: '@Url.Action("GetUser", "Admin")',
							data: { Id: -1 },
							success: function (response) {
								devLoadingPanelRestart.Hide();
								devGridViewPlugin.PerformCallback();
							}
						});
					} else {
						$("body").append(reponse);
					}
				},
				error: function () {
					alert("error occured");
				}
			});
		} else if (installationType == "uninstall") {
			$.ajax({
				type: "POST",
				url: '@Url.Action("UninstallPlugin", "Admin")',
				data: { pluginName: pluginSystemName },
				success: function (response) {
					if (response == "success") {
						devLoadingPanelRestart.Show();
						$.ajax({
							type: "GET",
							url: '@Url.Action("GetUser", "Admin")',
							data: { Id: -1 },
							success: function (response) {
								devLoadingPanelRestart.Hide();
								devGridViewPlugin.PerformCallback();
							}
						});
					} else {
						$("body").append(reponse);
					}
				},
				error: function () {
					alert("error occured");
				}
			});
		}
	}
</script>

@Html.DevExpress().LoadingPanel(t =>
{
	t.Name = "devLoadingPanelRestart";
	t.Text = "Anwendung wird neugestartet... bitte warten!";
	t.Modal = true;
	t.Styles.LoadingDiv.Opacity = 0;
}).GetHtml()

@Html.Partial("~/Views/Admin/Plugins/_PluginsGridPartial.cshtml", Model)