@using GreenTree.Nachtragsmanagement.Web.Models.Global;

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

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

<script>
	var deleteId;
	var deleteReplaceId = -1;
	var gridScrollHeight;
	var gridScrollOffset = 130;
	var resizeFinished;

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

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

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

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

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

	function confirmDelete(id) {
		if (!id) return;
		deleteId = id;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetRole", "Admin")',
			data: { id: deleteId },
			success: function (response) {
				if (response == "notFound") return;

				var role = JSON.parse(response);

				$(".deleteValidation").hide();

				var comboBox = MVCxClientComboBox.Cast(devComboBoxRoleDeleteReplaceRole);
				comboBox.PerformCallback();

				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteRole);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{role}", role.Description));
				$(".dialogText").text($(".dialogText").text().replace("{role}", role.Description));
				popupControl.Show();
			}
		});
	}

	function deleteRole() {
		if (devRadioButtonDeleteReplaceRole.GetChecked()) {
			deleteReplaceId = devComboBoxRoleDeleteReplaceRole.GetValue();
		} else {
			deleteReplaceId = -1;
		}

		if (deleteReplaceId == 0) {
			$(".deleteValidation").text("Es muss ein Ersatz ausgewählt werden.");
			$(".deleteValidation").show();
			return;
		}
		if (deleteId == deleteReplaceId) {
			$(".deleteValidation").text("Der Ersatz darf nicht gleich dem zu löschenden Element sein.");
			$(".deleteValidation").show();
			return;
		}

		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteRole", "Admin")',
			data: { id: deleteId, replaceId: deleteReplaceId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteRole);
				popupControl.Hide();
				setTimeout(function () {
					devGridViewRole.PerformCallback();
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}
</script>

@Html.DevExpress().PopupControl(s =>
{
	s.Name = "devPopupControlDeleteRole";
	s.HeaderText = "\"{role}\" löschen";
	s.Modal = false;
	s.Width = new Unit(350, UnitType.Pixel);
	s.CloseAction = CloseAction.CloseButton;
	s.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
	s.PopupVerticalAlign = PopupVerticalAlign.WindowCenter;
	s.AllowDragging = false;
	s.AllowResize = false;
	s.ShowFooter = false;
	s.SetContent(() =>
	{
		ViewContext.Writer.Write("<div class='dialogText' style='padding: 12px'>");
		ViewContext.Writer.Write("Falls Sie \"{role}\" löschen möchten, was soll mit den Benutzern geschehen, die sich bereits in dieser Rolle befinden?");
		ViewContext.Writer.Write("</div>");

		ViewContext.Writer.Write("<div style='padding: 0 12px 12px 12px'>");
		Html.DevExpress().RadioButton(rb =>
		{
			rb.Name = "devRadioButtonDeleteReplaceRole";
			rb.Text = "Eine neue Rolle zuweisen";
			rb.GroupName = "roleDelete";
			rb.Properties.ClientSideEvents.CheckedChanged =
				"function (s, e) { " +
					"devComboBoxRoleDeleteReplaceRole.SetEnabled(s.GetChecked()); " +
				"}";
		}).Render();

		Session.Add("RolesDeleteComboBoxSettings", new Action<ComboBoxSettings>(a =>
		{
			a.Name = "devComboBoxRoleDeleteReplaceRole";
			a.Width = new Unit(100, UnitType.Percentage);
			a.Properties.ValueType = typeof(int);
			a.Properties.ValueField = "Id";
			a.Properties.TextField = "Description";
			a.Properties.ClientSideEvents.BeginCallback = "function (s, e) { e.customArgs['excludedIds'] = [ deleteId ]; }";
			a.Properties.ClientSideEvents.EndCallback = "function (s, e) { s.SetSelectedIndex(0); }";
			a.SelectedIndex = 0;
			a.ClientEnabled = false;
			a.CallbackRouteValues = new
			{
				Controller = "DataCallback",
				Action = "RolesComboBoxExcluded",
				SettingsKey = "RolesDeleteComboBoxSettings"
			};
		}));
		ViewData.Add("RolesComboBoxSettings", "RolesDeleteComboBoxSettings");
		Html.RenderPartial("~/Views/Shared/DataEditorTemplates/_RolesComboBox.cshtml", EmptyIRequireDataModel.Instance, ViewData);

		Html.DevExpress().RadioButton(rb =>
		{
			rb.Name = "devRadioButtonDeleteRemoveRole";
			rb.Text = "Benutzer aus der Rolle entfernen";
			rb.GroupName = "roleDelete";
			rb.Checked = true;
			rb.Properties.ClientSideEvents.CheckedChanged =
				"function (s, e) { " +
					"devComboBoxRoleDeleteReplaceRole.SetEnabled(!s.GetChecked()); " +
				"}";
		}).Render();

		ViewContext.Writer.Write("<div class=\"deleteValidation\" style=\"display: none\"></div>");

		ViewContext.Writer.Write("</div>");

		Html.RenderPartial(
			"~/Views/Shared/_PopupButtonPanelYesNo.cshtml",
			new YesNoDialogModel
			{
				PopupName = "devPopupControlDeleteRole",
				YesFunction = "function (s, e) { deleteRole(); }"
			}
		);
	});
	s.Styles.Content.Paddings.Padding = new Unit(0, UnitType.Pixel);
}).GetHtml()

@Html.Partial("~/Views/Admin/Roles/_RoleGridPartial.cshtml", Model)