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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Deviation.DeviationDataModel>

<script>
	var deleteId;
	var gridScrollHeight;
	var gridScrollOffset = 185;
	var resizeFinished;
	var customFilters = {};
	var customFilterDelay = 800;
	var timer;
	var setFilterTimer = function (timeout, func) {
		if (timer != null) {
			clearTimeout(timer);
			timer = null;
		}
		if (timer == null) {
			timer = setTimeout(function () {
				func();
			}, timeout);
		}
	}

	$(document).ready(function () {
		gridScrollHeight = calculateGridScrollHeight();
		setTimeout(function () {
			devGridViewDeviation.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 = $("#devGridViewDeviation_DXHeadersRow0").height();
		var gridFooterHeight = $("#devGridViewDeviation_DXFooterRow").height();
		return windowHeight - gridHeaderHeight - gridFooterHeight - gridScrollOffset;
	}

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

	function onToolbarItemClick(s, e) {
		if (!s || !e) return;
		if (e.item.name == "Print") {
			parent.$(".printPopupContainer").remove();
			$.ajax({
				type: "POST",
				url: '@Url.Action("ExportPartialDeviations", "Deviation")',
				data: { customFilters: JSON.stringify(customFilters), displayMode: "popup", exportFormat: "" },
				success: function (response) {
					parent.$("body").append(response);
					parent.exportFormat = "pdf";
				}
			});
		} else if (e.item.name == "ToggleColumnChooser") {
			if (devGridViewDeviation.IsCustomizationWindowVisible())
				devGridViewDeviation.HideCustomizationWindow();
			else
				devGridViewDeviation.ShowCustomizationWindow();
		} else if (e.item.name == "ResetSettings") {
			$.ajax({
				type: "POST",
				url: '@Url.Action("DeleteCookiesSessionVariablesAndUserConfigs", "Global")',
				data: {
					cookies: ["deviationGridStateCookie"],
					sessionVariables: ["DeviationGridState", "DeviationGridViewState"],
					userConfigItemNames: ["DeviationGridLayoutState"]
				},
				success: function (response) {
					if (response == "success") {
						window.location = window.location;
					}
				}
			});
		}
	}

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

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

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

@Html.Partial("~/Views/Deviations/_DeviationGridPartial.cshtml", Model)
@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlDeleteDeviation",
	Content = "<div class='dialogText' style='padding: 12px'>Sind Sie sicher, dass Sie die Vertragsabweichung \"{deviation}\" löschen möchten?</div>",
	HeaderText = "\"{deviation}\" löschen",
	YesFunction = "function (s, e) { deleteDeviation(); }"
})