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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Site.SiteDataModel>

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

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

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

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

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

	function onToolbarItemClick(s, e) {
		if (!s || !e) return;
		if (e.item.name == "Print") {
			parent.$(".printPopupContainer").remove();
			$.ajax({
				type: "POST",
				url: '@Url.Action("ExportPartialSites", "Site")',
				data: { displayMode: "popup", exportFormat: "" },
				success: function (response) {
					parent.$("body").append(response);
					parent.exportFormat = "pdf";
				}
			});
		} else if (e.item.name == "AddSite") {
			editSite(-1);
		} else if (e.item.name == "ToggleColumnChooser") {
			if (devGridViewSite.IsCustomizationWindowVisible())
				devGridViewSite.HideCustomizationWindow();
			else
				devGridViewSite.ShowCustomizationWindow();
		}
	}

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

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

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

@Html.Partial("~/Views/Sites/_SiteGridPartial.cshtml", Model)
@Html.Partial("~/Views/Shared/_PopupDialogYesNo.cshtml", new GreenTree.Nachtragsmanagement.Web.Models.Global.YesNoDialogModel
{
	PopupName = "devPopupControlDeleteSite",
	Content = "<div class='dialogTextSite' style='padding: 12px'>Sind Sie sicher, dass Sie die Baustelle \"{site}\" " +
			  "und alle zugehörigen Nachträge / Vertragsabweichungen löschen möchten? " +
			  "Der Vorgang kann nicht rückgängig gemacht werden. </div>",
	HeaderText = "\"{site}\" löschen",
	YesFunction = "function (s, e) { deleteSite(); }",
	YesButtonName = "devButtonDeleteSiteYes",
	NoButtonName = "devButtonDeleteSiteNo"
})

@Html.Partial("~/Views/Sites/_SiteDeviationDescriptionPartial.cshtml")