@using GreenTree.Nachtragsmanagement.Web.Extensions

@model IEnumerable<GreenTree.Nachtragsmanagement.Web.Models.Appendix.AppendixDataModel>

<div class="deviationAssignContainer">

	<script>
		function assignDeviation() {
			var id = $("#DeviationId").val();
			var appendixId = devListBoxAssignDeviation.GetValue();
			if (!id || !appendixId) return;
			$.ajax({
				type: "POST",
				url: '@Url.Action("AssignDeviation", "Deviation")',
				data: { Id: id, AppendixId: appendixId },
				success: function (response) {
					if (response == "success") {
						devPopupControlAssignDeviation.Hide();
						devTreeListSiteDeviationAppendices.PerformCallback();
					} else {
						$("body").append(response);
					}
				},
				error: function () {
					 alert("error occured");
				}
			});
		}
	</script>

	@Html.DevExpress().PopupControl(s =>
{
	s.Name = "devPopupControlAssignDeviation";
	s.Modal = false;
	s.Width = new Unit(350, UnitType.Pixel);
	s.CloseAction = CloseAction.OuterMouseClick;
	s.PopupHorizontalAlign = PopupHorizontalAlign.OutsideRight;
	s.PopupVerticalAlign = PopupVerticalAlign.TopSides;
	s.AllowDragging = false;
	s.AllowResize = false;
	s.ShowFooter = false;
	s.ShowHeader = false;
	s.ShowOnPageLoad = false;
	s.SetContent(() =>
	{
		ViewContext.Writer.Write("<input type=\"hidden\" value=\"" + ViewData["DeviationId"] + "\" id=\"DeviationId\" name=\"DeviationId\" />");

		Html.DevExpress().ListBox(t =>
		{
			t.Name = "devListBoxAssignDeviation";
			t.Width = new Unit(100, UnitType.Percentage);
			t.Height = new Unit(150, UnitType.Pixel);
			t.Properties.ValueField = "Id";
			t.Properties.ValueType = typeof(int);
			t.SetItemTemplateContent(c =>
			{
				var id = Convert.ToInt32(DataBinder.Eval(c.DataItem, "Id"));
				var customNumber = Convert.ToInt32(DataBinder.Eval(c.DataItem, "CustomNumber"));
				var description = DataBinder.Eval(c.DataItem, "Description").ToString();

				if (id == -1)
					ViewContext.Writer.Write(description);
				else
					ViewContext.Writer.Write("NT - {0} - {1}", customNumber, description);
			});
			t.ControlStyle.Border.BorderStyle = BorderStyle.None;
		}).BindList(Model).Render();

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