@using GreenTree.Nachtragsmanagement.Web.Extensions

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

<div class="configItemValueEdit">

	<script type="text/javascript">

		var textSeparator = ", ";

		function onListBoxSelectionChanged(s, e) {
			updateText();
		}

		function updateText() {
			var selectedItems = Values.GetSelectedItems();
			devDropDownListConfigItemValues.SetText(getSelectedItemsText(selectedItems));
		}

		function getSelectedItemsText(items) {
			var texts = [];
			for (var i = 0; i < items.length; i++)
				if (items[i].index != 0)
					texts.push(items[i].text);
			return texts.join(textSeparator);
		}

	</script>

	@if (Model.IsValueCollection)
	{
		Html.DevExpress().DropDownEdit(t =>
		{
			t.Name = "devDropDownListConfigItemValues";
			t.Width = new Unit(100, UnitType.Percentage);

			t.SetDropDownWindowTemplateContent(l =>
			{
				Html.DevExpress().ListBox(lb =>
				{
					lb.Name = "Values";
					lb.Width = new Unit(100, UnitType.Percentage);
					lb.Properties.TextField = "Text";
					lb.Properties.ValueField = "Value";
					lb.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
					lb.ControlStyle.Border.BorderStyle = BorderStyle.None;
					lb.PreRender = (sender, e) =>
					{
						var listBox = sender as MVCxListBox;

						foreach (ListEditItem listItem in listBox.Items)
						{
							if (Model.Values == null || !Model.Values.Any(m => m == (string)listItem.Value)) continue;

							listItem.Selected = true;
						}
					};
					lb.Properties.ClientSideEvents.SelectedIndexChanged = "function (s, e) { onListBoxSelectionChanged(s, e); }";
				}).BindList(ViewData["ConfigReferenceValues"]).Render();
			});

			t.Properties.ClientSideEvents.Init = "function (s, e) { s.SetText(getSelectedItemsText(Values.GetSelectedItems())); }";
		}).Bind(ViewData["ConfigReferenceText"]).Render();
	}
	else
	{
		Html.DevExpress().ComboBoxFor(m => m.Value, t =>
		{
			t.Width = new Unit(100, UnitType.Percentage);
			t.Properties.TextField = "Text";
			t.Properties.ValueField = "Value";
		}).BindList(ViewData["ConfigReferenceValues"]).GetHtml();
	}

</div>