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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Config.ConfigItemDataModel>

<script>
	var deleteId;
	var gridScrollHeight;
	var gridScrollOffset = 130;
	var resizeFinished;

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

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

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

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

	function showFullName(text, element) {
		if (!element) return;
		devPopupControlFullName.Hide();
		$("#fullNameContent").text(text);
		devPopupControlFullName.ShowAtElement(element);
	}

	function editConfigItem(id) {
		if (!id) return;
		$.ajax({
			url: '@Url.Action("EditConfigItem", "Misc")',
			data: { Id: id },
			success: function (response) {
				setTimeout(function () {
					$(".configItemEditContainer").remove();
					$("body").append(response);
					parent.addCustomEventListener("ConfigItemDataCallbackEventReceiver", function () {
						devGridViewConfigItem.PerformCallback();
					});
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

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

	function deleteConfigItem() {
		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteConfigItem", "Misc")',
			data: { Id: deleteId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteConfigItem);
				popupControl.Hide();
				setTimeout(function () {
					devGridViewConfigItem.PerformCallback();
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}
</script>

@Html.Partial("~/Views/Config/_ConfigItemGridPartial.cshtml", Model)

@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlDeleteConfigItem",
	Content = "<div class='dialogTextConfigItem' style='padding: 12px'>Sind Sie sicher, dass Sie die Benachrichtigung " +
			  "\"{configItem}\" löschen möchten?</div>",
	HeaderText = "\"{configItem}\" löschen",
	YesFunction = "function (s, e) { deleteConfigItem(); }",
	YesButtonName = "devButtonDeleteConfigItemYes",
	NoButtonName = "devButtonDeleteConfigItemNo"
})

@Html.DevExpress().PopupControl(p =>
{
	p.Name = "devPopupControlFullName";
	p.ShowHeader = false;
	p.ShowFooter = false;
	p.ShowShadow = false;
	p.PopupAnimationType = AnimationType.None;
	p.Width = new Unit(300, UnitType.Pixel);
	p.SetContent("<div id=\"fullNameContent\"></div>");
	p.CloseAction = CloseAction.OuterMouseClick;
	p.PopupHorizontalAlign = PopupHorizontalAlign.LeftSides;
	p.PopupVerticalAlign = PopupVerticalAlign.Below;
}).GetHtml()