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

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Admin.User.UserDataModel>

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

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

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

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

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

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

	function confirmDelete(id) {
		if (!id) return;
		deleteId = id;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetUser", "Admin")',
			data: { Id: id },
			success: function (response) {
				if (response == "notFound") return;
				var user = JSON.parse(response);
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteUser);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{user}", user.Lastname + ", " + user.Forename));
				$(".dialogText").text($(".dialogText").text().replace("{user}", user.Lastname + ", " + user.Forename));
				popupControl.Show();
			}
		});
	}

	function deleteUser() {
		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteUser", "Admin")',
			data: { Id: deleteId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteUser);
				popupControl.Hide();
				setTimeout(function () {
					devGridViewUser.PerformCallback();
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

</script>

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