@using GreenTree.Nachtragsmanagement.Web.Extensions

@model GreenTree.Nachtragsmanagement.Web.Models.Appendix.InvoiceDataModel

<div class="invoiceEditContainer">

	<script>
		function saveInvoice() {
			var form = $("#invoiceEditForm");
			$(form).submit(function (e) {
				$.ajax({
					type: "POST",
					url: '@Url.Action("EditInvoice", "Appendix")',
					data: form.serialize(),
					success: function (response) {
						setTimeout(function () {
							$(".invoiceEditContainer").remove();
							if (response == "success") {
								parent.callCustomEventListener('InvoiceDataCallbackEventReceiver');
							} else {
								$("body").append(response);
							}
						}, 200);
					}
				});
				e.preventDefault();
			});
			form.submit();
		}
	</script>

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

	if (Model.Id == -1)
		s.HeaderText = "Neue Rechnung erstellen";
	else
		s.HeaderText = "\"" + Model.CustomNumber + "\" bearbeiten";

	s.Modal = true;
	s.Width = new Unit(300, UnitType.Pixel);
	s.CloseAction = CloseAction.CloseButton;
	s.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
	s.PopupVerticalAlign = PopupVerticalAlign.WindowCenter;
	s.AllowDragging = false;
	s.AllowResize = false;
	s.ShowFooter = false;
	s.ShowOnPageLoad = true;
	s.SetContent(() =>
	{
		using (Html.BeginForm("EditInvoice", "Appendix", FormMethod.Post, new { id = "invoiceEditForm" }))
		{
			ViewContext.Writer.Write("<div class='editFormWrapper'>");

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

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

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

			ViewContext.Writer.Write(Html.CustomLabelFor(m => m.Value, "Rechnungswert:"));
			ViewContext.Writer.Write(Html.ValidationMessageFor(m => m.Value).ToHtmlString());
			Html.DevExpress().SpinEditFor(m => m.Value, t =>
			{
				t.Width = new Unit(100, UnitType.Percentage);
				t.Properties.DecimalPlaces = 2;
				t.Properties.NumberType = SpinEditNumberType.Float;
				t.Properties.DisplayFormatString = "c2";
			}).Render();

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

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