@{ 
	var userContext = GreenTree.Nachtragsmanagement.Core.CommonHelper.UserContext();
}

@model GreenTree.Nachtragsmanagement.Web.Models.Site.SiteDataModel

@Html.DevExpress().TreeList(t =>
{
	t.Name = "devTreeListSiteDeviationAppendices";
	t.KeyFieldName = "TreeKey";
	t.ParentFieldName = "TreeParentKey";
	t.CallbackRouteValues = new { Controller = "Site", Action = "PartialDeviationAppendices", siteId = Model.Id };
	t.Width = Unit.Percentage(100);
	t.Settings.GridLines = GridLines.Both;
	t.SettingsBehavior.AllowDragDrop = true;
	t.SettingsEditing.NodeDragDropRouteValues = new { Controller = "Site", Action = "PartialDeviationAppendices", siteId = Model.Id };
	t.ClientSideEvents.StartDragNode = "function (s, e) { startSiteTreeNodeDrag(s, e); }";
	t.ClientSideEvents.EndDragNode = "function (s, e) { endSiteTreeNodeDrag(s, e); }";

	t.Columns.Add(column =>
	{
		column.AllowSort = DefaultBoolean.False;
		column.SetHeaderCaptionTemplateContent(c =>
		{
			ViewContext.Writer.Write(
				"<a href=\"#\" onclick=\"addAppendixToSite(" + Model.Id + ")\">Neuer NT</a>&nbsp;&nbsp;&#47;&nbsp;&nbsp;");
			ViewContext.Writer.Write(
				"<a href=\"#\" onclick=\"addDeviationToSite(" + Model.Id + ")\">Neue VA</a>");
		});
		column.SetDataCellTemplateContent(c =>
		{
			var treeKey = DataBinder.Eval(c.DataItem, "TreeKey").ToString();

			if (treeKey == "a_0")
			{

			}
			else if (treeKey.StartsWith("d"))
			{
				ViewContext.Writer.Write(
					"<a href=\"#\" onclick=\"editDeviation(" + DataBinder.Eval(c.DataItem, "Id") + ")\">Bearbeiten</a><br />");
				ViewContext.Writer.Write(
					"<a href=\"#\" onclick=\"confirmDeleteDeviation(" + DataBinder.Eval(c.DataItem, "Id") + ")\">Löschen</a><br />");
				ViewContext.Writer.Write(
					"<a href=\"#\" onclick=\"assignDeviationToAppendix(" + DataBinder.Eval(c.DataItem, "Id") + ",this)\">NT zuordnen</a>");
			}
			else if (treeKey.StartsWith("a"))
			{
				ViewContext.Writer.Write(
					"<a href=\"#\" onclick=\"editAppendix(" + DataBinder.Eval(c.DataItem, "Id") + ")\">Bearbeiten</a><br />");
				ViewContext.Writer.Write(
					"<a href=\"#\" onclick=\"confirmDeleteAppendix(" + DataBinder.Eval(c.DataItem, "Id") + ")\">Löschen</a>");
			}
		});
	});

	t.Columns.Add(column =>
	{
		column.Caption = "Nummer";
		column.SetDataCellTemplateContent(c =>
		{
			var treeKey = DataBinder.Eval(c.DataItem, "TreeKey").ToString();

			if (treeKey.StartsWith("a"))
			{
				if (treeKey == "a_0")
					ViewContext.Writer.Write("<b>" + DataBinder.Eval(c.DataItem, "CustomNumber") + "</b>");
				else
					ViewContext.Writer.Write("<b>NT - " + DataBinder.Eval(c.DataItem, "CustomNumber") + "</b>");
			}
			else if (treeKey.StartsWith("d"))
			{
				ViewContext.Writer.Write("VA - " + DataBinder.Eval(c.DataItem, "CustomNumber"));
			}
		});
	});
	t.Columns.Add(column =>
	{
		column.FieldName = "Description";
		column.Caption = "Bezeichnung";
		column.CellStyle.Wrap = DefaultBoolean.True;
	});
	t.Columns.Add(column =>
	{
		column.Caption = "Eingang";
		column.FieldName = "ReceiptDate";
		column.PropertiesEdit.DisplayFormatString = "dd.MM.yyyy";
	});
	t.Columns.Add(column =>
	{
		column.Caption = "Bewertung";
		column.FieldName = "Value";
		column.PropertiesEdit.DisplayFormatString = "c2";
	});
	t.Columns.Add("KindDescription", "VA-Art");
	t.Columns.Add("StatusDescription", "Status");
	t.Columns.Add(column =>
	{
		column.FieldName = "DistCatDescription";
		column.Caption = "Kategorien";
		column.CellStyle.Wrap = DefaultBoolean.True;
	});
	t.Columns.Add(column =>
	{
		column.Caption = "Kommentar";
		column.CellStyle.Wrap = DefaultBoolean.True;
		column.SetDataCellTemplateContent(c =>
		{
			var id = Convert.ToInt32(DataBinder.Eval(c.DataItem, "Id"));
			var treeKey = DataBinder.Eval(c.DataItem, "TreeKey").ToString();

			var comment = DataBinder.Eval(c.DataItem, "Comment");
			var text = comment == null
				? String.Empty
				: comment.ToString();

			if (text.ToString().Length > 40)
			{
				ViewContext.Writer.Write(text.Substring(0, 40) + " ...");

				if (treeKey.StartsWith("a"))
					ViewContext.Writer.Write("<a href=\"#\" onclick='showComment(\"appendix\"," + id + ",this)'><br />Anzeigen</a>");
				else
					ViewContext.Writer.Write("<a href=\"#\" onclick='showComment(\"appendix\"," + id + ",this)'><br />Anzeigen</a>");
			}
			else
				ViewContext.Writer.Write(text);
		});
	});

	t.PreRender = (s, e) =>
	{
		var tl = s as MVCxTreeList;
		tl.ExpandAll();
	};

	t.HtmlRowPrepared = (sender, e) =>
	{
		var hexColor = e.GetValue("StatusColor");
		var defaultState = ViewData["DefaultAppendixState"] as GreenTree.Nachtragsmanagement.Core.Domain.Appendix.State;
		var statusDescription = e.GetValue("StatusDescription");
		var treeKey = e.GetValue("TreeKey").ToString();

		if (hexColor == null ||
			(defaultState != null && statusDescription != null && statusDescription.ToString() == defaultState.Description))
		{
			if (treeKey.StartsWith("a"))
				e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#96C8FF");
		}
		else
			e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(hexColor.ToString());

		if (treeKey == "a_0")
		{
			e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFAB4C");
		}
	};

	t.ControlStyle.CssClass += "siteTreeList";
	t.Styles.IndentWithButton.Paddings.PaddingTop = new Unit(18, UnitType.Pixel);
	t.Styles.Cell.VerticalAlign = VerticalAlign.Top;
	t.Styles.Cell.Paddings.PaddingTop = new Unit(14, UnitType.Pixel);
	t.Styles.Cell.Paddings.PaddingBottom = new Unit(14, UnitType.Pixel);
}).Bind(Model.SiteTreeData).GetHtml()