@using GreenTree.Nachtragsmanagement.Web.Extensions

@model GreenTree.Nachtragsmanagement.Web.Models.Config.ConfigItemDataModel

<div class="configItemEditContainer">

	<script>
		function saveConfigItem() {
			var form = $("#configItemEditForm");
			$(form).submit(function (e) {
				$.ajax({
					type: "POST",
					url: '@Url.Action("EditConfigItem", "Misc")',
					data: form.serialize(),
					success: function (response) {
						setTimeout(function () {
							$(".configItemEditContainer").remove();
							if (response == "success") {
								parent.callCustomEventListener('ConfigItemDataCallbackEventReceiver');
							} else {
								$("body").append(response);
							}
						}, 200);
					}
				});
				e.preventDefault();
			});
			form.submit();
		}

		function getValueTypePartialEdit() {
			$.ajax({
				type: "GET",
				url: '@Url.Action("GetValueTypePartialEdit", "Misc")',
				data: { typeFullName: TypeFullName.GetValue() },
				success: function (response) {
					$(".configItemValueEdit").replaceWith(response);
				}
			});
		}
	</script>

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

	if (Model.Id == -1)
		s.HeaderText = "Neue Einstellung erstellen";
	else
	{
		if (Model.Name.Length > 40)
			s.HeaderText = "\"" + Model.Name.Substring(0, 15) + " ... " + Model.Name.Substring(Model.Name.Length - 15, 15) + "\" bearbeiten";
	}

	s.Modal = true;
	s.Width = new Unit(500, UnitType.Pixel);
	s.CloseAction = CloseAction.CloseButton;
	s.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
	s.PopupVerticalAlign = PopupVerticalAlign.TopSides;
	s.PopupVerticalOffset = 20;
	s.AllowDragging = false;
	s.AllowResize = false;
	s.ShowFooter = false;
	s.ShowOnPageLoad = true;
	s.SetContent(() =>
	{
		using (Html.BeginForm("EditConfigItem", "Deviation", FormMethod.Post, new { id = "configItemEditForm" }))
		{
			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.Name, "Name:"));
			ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.Name).ToHtmlString());
			Html.DevExpress().TextBoxFor(m => m.Name, t =>
			{
				t.Width = new Unit(100, UnitType.Percentage);
			}).Render();

			ViewContext.Writer.Write(Html.CustomLabelFor(m => m.TypeFullName, "Datentyp:"));
			ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.TypeFullName).ToHtmlString());
			Html.DevExpress().ComboBoxFor(m => m.TypeFullName, t =>
			{
				t.Width = new Unit(60, UnitType.Percentage);
				t.Properties.Items.AddRange(
					GreenTree.Nachtragsmanagement.Web.Models.Config.ConfigItemDataModel.FullTypeTranslations
						.Select(f => new ListEditItem(f.Value, f.Key))
						.ToList());
				t.Properties.ClientSideEvents.SelectedIndexChanged = "function (s, e) { getValueTypePartialEdit(); }";
			}).Render();

			ViewContext.Writer.Write("<div class=\"configItemEditContainerPlaceholder\">");
			{
				ViewContext.Writer.Write(Html.CustomLabelFor(m => m.Value, "Wert:"));
				ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.Value).ToHtmlString());
				Html.RenderPartial("~/Views/Config/_ConfigItemValueEditPartial.cshtml", Model);
			}
			ViewContext.Writer.Write("</div>");

			ViewContext.Writer.Write(Html.CustomLabelFor(m => m.Description, "Beschreibung:"));
			ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.Description).ToHtmlString());
			Html.DevExpress().MemoFor(m => m.Description, t =>
			{
				t.Width = new Unit(100, UnitType.Percentage);
				t.Height = new Unit(80, UnitType.Pixel);
			}).Render();

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

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