@using GreenTree.Nachtragsmanagement.Web.Extensions

@model GreenTree.Nachtragsmanagement.Web.Models.Misc.HelpPageDataModel

<div class="helpPageEditContainer">

	<script>
		function onHtmlToolbarCustomCommand(e) {
			if (!e) return;
			if (e.commandName == "save") {
				saveHelpPage();
			} else if (e.commandName == "saveAs") {
				devPopupControlEditHelpPage.Show();
			}
			saveOperation = e.commandName;
		}

		function saveHelpPage() {
			devLoadingPanelSave.Show();
			var form = $("#helpPageEditForm");
			var selectedNode = devHelpPageSelectionTree.GetSelectedNode();
			$("#Content").val(devHelpPageHtmlContentEditor.GetHtml());
			if (selectedNode != null) {
				$("#ParentId").val(selectedNode.name);
			}
			$(form).submit(function (e) {
				$.ajax({
					type: "POST",
					url: '@Url.Action("EditHelpPage", "Misc")',
					data: form.serialize(),
					success: function (response) {
						setTimeout(function () {
							if (response.startsWith("success")) {
								if (saveOperation == "save") {
									devPopupControlEditHelpPage.Hide();
								} else {
									var id = response.split('-')[1];
									window.location = '@Url.Action("ViewHelpPages", "Misc")' + '?id=' + id;
								}
							} else {
								$("#helpPageContent").html(response);
								setTimeout(function () {
									devPopupControlEditHelpPage.Show();
								}, 100);
							}
							devLoadingPanelSave.Hide();
						}, 200);
					}
				});
				e.preventDefault();
			});
			form.submit();
		}

		function setDropDownText(text) {
			ParentText.SetText(text);
			ParentText.HideDropDown();
		}
	</script>

	@Html.DevExpress().LoadingPanel(t =>
	{
		t.Name = "devLoadingPanelSave";
		t.Text = "Wird gespeichert ...";
		t.Styles.LoadingDiv.Opacity = 0;
		t.Modal = true;
	}).GetHtml()

	@using (Html.BeginForm())
	{
		@Html.Partial("~/Views/Misc/_HelpPageHtmlEditPartial.cshtml", Model)
	}

	@using (Html.BeginForm("EditHelpPage", "Misc", FormMethod.Post, new { id = "helpPageEditForm" }))
	{
		@Html.HiddenFor(m => m.Content)
		@Html.HiddenFor(m => m.ParentId)

		@Html.DevExpress().PopupControl(s =>
		{
			s.Name = "devPopupControlEditHelpPage";

			if (Model.Id == -1)
				s.HeaderText = "Neue Hilfe-Seite erstellen";
			else
				s.HeaderText = "\"" + Model.Title + "\" bearbeiten";

			s.Modal = true;
			s.Width = new Unit(350, UnitType.Pixel);
			s.CloseAction = CloseAction.CloseButton;
			s.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
			s.PopupVerticalAlign = PopupVerticalAlign.TopSides;
			s.PopupVerticalOffset = 80;
			s.AllowDragging = false;
			s.AllowResize = false;
			s.ShowFooter = false;
			s.ShowOnPageLoad = false;
			s.SetContent(() =>
			{
				ViewContext.Writer.Write("<div class='editFormWrapper'>");

				ViewContext.Writer.Write("<input type=\"hidden\" value=\"" + Model.Id + "\" id=\"Id\" name=\"Id\" />");

				ViewContext.Writer.Write(Html.CustomLabelFor(m => m.Title, "Titel:"));
				ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.Title).ToHtmlString());
				Html.DevExpress().TextBoxFor(m => m.Title, t =>
				{
					t.Width = new Unit(100, UnitType.Percentage);
				}).Render();

				ViewContext.Writer.Write(Html.CustomLabelFor(m => m.ParentId, "Übergeordnete Seite:"));
				ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.ParentId).ToHtmlString());
				Html.DevExpress().DropDownEdit(t =>
				{
					t.Name = "ParentText";
					t.Width = new Unit(100, UnitType.Percentage);
					t.Text = Model.ParentText;

					t.SetDropDownWindowTemplateContent(l =>
					{
						ViewContext.Writer.Write("<div style=\"width: 100%; height: 230px; overflow: auto\">");
						{
							Html.RenderPartial("~/Views/Misc/_HelpPageTreePartial.cshtml", ViewData["AllHelpPages"]);
						}
						ViewContext.Writer.Write("</div>");
					});
				}).Render();

				ViewContext.Writer.Write(Html.CustomLabelFor(m => m.DisplayIndex, "Reihenfolge:"));
				ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.DisplayIndex).ToHtmlString());
				Html.DevExpress().SpinEditFor(m => m.DisplayIndex, t =>
				{
					t.Width = new Unit(100, UnitType.Percentage);
					t.Number = 0;
					t.Properties.NumberType = SpinEditNumberType.Integer;
				}).Render();

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

				Html.RenderPartial(
					"~/Views/Shared/_PopupButtonPanel.cshtml",
					new GreenTree.Nachtragsmanagement.Web.Models.Global.PopupModel
					{
						PopupName = "devPopupControlEditHelpPage",
						AcceptFunction = "function (s, e) { saveHelpPage(); }"
					}
				);
			});
			s.Styles.Content.Paddings.Padding = new Unit(0);
			s.Styles.ModalBackground.Opacity = 0;
		}).GetHtml()
	}

</div>