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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Appendix.AppendixDataModel>

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

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

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

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

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

	function onToolbarItemClick(s, e) {
		if (!s || !e) return;
		if (e.item.name == "Print") {
			parent.$(".printPopupContainer").remove();
			$.ajax({
				type: "POST",
				url: '@Url.Action("ExportPartialAppendices", "Appendix")',
				data: { displayMode: "popup", exportFormat: "" },
				success: function (response) {
					parent.$("body").append(response);
					parent.exportFormat = "pdf";
				}
			});
		} else if (e.item.name == "ToggleColumnChooser") {
			if (devGridViewAppendix.IsCustomizationWindowVisible())
				devGridViewAppendix.HideCustomizationWindow();
			else
				devGridViewAppendix.ShowCustomizationWindow();
		} else if (e.item.name == "ResetSettings") {
			$.ajax({
				type: "POST",
				url: '@Url.Action("DeleteCookiesAndSessionVariables", "Global")',
				data: { cookies: [ "appendixGridStateCookie" ], sessionVariables: [ "AppendixGridState", "AppendixGridViewState" ] },
				success: function (response) {
					if (response == "success") {
						window.location = window.location;
					}
				}
			});
		}
	}

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

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

	function confirmDelete(id) {
		if (!id) return;
		deleteId = id;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetAppendix", "Appendix")',
			data: { Id: id },
			success: function (response) {
				if (response == "notFound") return;
				var appendix = JSON.parse(response);
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteAppendix);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{appendix}", appendix.CustomNumber));
				$(".dialogText").text($(".dialogText").text().replace("{appendix}", appendix.CustomNumber));
				popupControl.Show();
			}
		});
	}

	function deleteAppendix() {
		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteAppendix", "Appendix")',
			data: { Id: deleteId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteAppendix);
				popupControl.Hide();
				setTimeout(function () {
					devGridViewAppendix.PerformCallback();
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

	function editOrderInvoiceCreated(id, callback) {
		if (!id) return;
		editOrderInvoiceCreatedCallback = callback;
		$.ajax({
			url: '@Url.Action("EditOrderInvoiceCreated", "Appendix")',
			data: { id: id },
			success: function (response) {
				$(".editOrderInvoiceCreatedContainer").remove();
				$("body").append(response);
			}
		});
	}
</script>

@Html.Partial("~/Views/Appendices/_AppendixGridPartial.cshtml", Model)
@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlDeleteAppendix",
	Content = "<div class='dialogText' style='padding: 12px'>Sind Sie sicher, dass Sie den Nachtrag \"{appendix}\" löschen möchten?</div>",
	HeaderText = "\"{appendix}\" löschen",
	YesFunction = "function (s, e) { deleteAppendix(); }"
})