@using GreenTree.Nachtragsmanagement.Web.Models.Global

@{
	Layout = "~/Views/Shared/_FunctionLayout.cshtml";
}

<script>
	var deleteId;
	var deleteReplaceId;
	var deleteClaimType;

	var claimTypeTranslation = {
		state: "NT-Status",
		category: "Kategorie"
	};

	$(document).ready(function () {
		setListBoxHeights();
		$.ajaxSetup({
			cache: false
		});
	});
	$(window).resize(setListBoxHeights);

	function setListBoxHeights() {
		var windowHeight = $(window).height();
		devListBoxStates.SetHeight(windowHeight - 40);
		devListBoxCategories.SetHeight(windowHeight - 40);
	}

	function editClaim(claimType, id) {
		if (!id) return;
		$.ajax({
			url: '@Url.Action("EditClaim", "Appendix")',
			data: { claimType: claimType, Id: id },
			success: function (response) {
				setTimeout(function () {
					$(".claimEditContainer").remove();
					$("body").append(response);
					if (claimType == "state") {
						parent.addCustomEventListener('StateDataCallbackEventReceiver', function () {
							devListBoxStates.PerformCallback();
						});
					} else if (claimType == "category") {
						parent.addCustomEventListener('CategoryDataCallbackEventReceiver', function () {
							devListBoxCategories.PerformCallback();
						});
					}
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}

	function confirmDelete(claimType, id) {
		if (!id) return;
		deleteId = id;
		deleteClaimType = claimType;
		$.ajax({
			type: "GET",
			url: '@Url.Action("GetClaim", "Appendix")',
			data: { claimType: deleteClaimType, Id: id },
			success: function (response) {
				if (response == "notFound") return;

				var claim = JSON.parse(response);

				$(".deleteValidation").hide();

				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteClaim);
				popupControl.SetHeaderText(popupControl.GetHeaderText().replace("{claim}", claim.Description));
				$(".dialogText").text($(".dialogText").text().replace("{claim}", claim.Description));
				$(".dialogText").text($(".dialogText").text().replace("{claim}", claim.Description));
				$(".dialogText").text($(".dialogText").text().replace("{claimType}", claimTypeTranslation[claimType]));

				devRadioButtonDeleteReplaceClaim
					.SetText(devRadioButtonDeleteReplaceClaim.GetText().replace("{claimType}", claimTypeTranslation[claimType]));

				devComboBoxClaimDeleteReplaceState.SetVisible(false);
				devComboBoxClaimDeleteReplaceCategory.SetVisible(false);

				var comboBox = null;

				if (deleteClaimType == "state") {
					devComboBoxClaimDeleteReplaceState.SetVisible(true);
					comboBox = MVCxClientComboBox.Cast(devComboBoxClaimDeleteReplaceState);
				} else if (deleteClaimType == "category") {
					devComboBoxClaimDeleteReplaceCategory.SetVisible(true);
					comboBox = MVCxClientComboBox.Cast(devComboBoxClaimDeleteReplaceCategory);
				}

				comboBox.PerformCallback();

				popupControl.Show();
			}
		});
	}

	function deleteClaim() {
		if (deleteClaimType == "state") {
			deleteReplaceId = devComboBoxClaimDeleteReplaceState.GetValue();
		} else if (deleteClaimType == "category") {
			deleteReplaceId = devComboBoxClaimDeleteReplaceCategory.GetValue();
		}

		if (deleteReplaceId == 0) {
			$(".deleteValidation").text("Es muss ein Ersatz ausgewählt werden.");
			$(".deleteValidation").show();
			return;
		}
		if (deleteId == deleteReplaceId) {
			$(".deleteValidation").text("Der Ersatz darf nicht gleich dem zu löschenden Element sein.");
			$(".deleteValidation").show();
			return;
		}

		$.ajax({
			type: "POST",
			url: '@Url.Action("DeleteClaim", "Appendix")',
			data: { claimType: deleteClaimType, Id: deleteId, replaceId: deleteReplaceId },
			success: function (response) {
				var popupControl = MVCxClientPopupControl.Cast(devPopupControlDeleteClaim);
				popupControl.Hide();
				setTimeout(function () {
					if (deleteClaimType == "state") {
						devListBoxStates.PerformCallback();
					} else if (deleteClaimType == "category") {
						devListBoxCategories.PerformCallback();
					}
				}, 200);
			},
			error: function () {
				 alert("error occured");
			}
		});
	}
</script>

<table style="width: 100%">
	<tbody>
		<tr>
			<td style="width: 50%; padding-right: 8px">
				<div class="listHeader">
					<span>NT-Stati</span>
					<img src='@Url.Content("~/Content/Images/add-24-contrast.png")' onclick='editClaim("state", -1)' title="Neuer NT-Status" />
				</div>
				@Html.Partial("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"])
			</td>
			<td style="width: 50%; padding-left: 8px">
				<div class="listHeader">
					<span>Kategorien</span>
					<img src='@Url.Content("~/Content/Images/add-24-contrast.png")' onclick='editClaim("category", -1)' title="Neue NT-Kategorie" />
				</div>
				@Html.Partial("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"])
			</td>
		</tr>
	</tbody>
</table>

@Html.DevExpress().PopupControl(s =>
{
	s.Name = "devPopupControlDeleteClaim";
	s.HeaderText = "\"{claim}\" löschen";
	s.Modal = false;
	s.Width = new Unit(350, UnitType.Pixel);
	s.CloseAction = CloseAction.CloseButton;
	s.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
	s.PopupVerticalAlign = PopupVerticalAlign.WindowCenter;
	s.AllowDragging = false;
	s.AllowResize = false;
	s.ShowFooter = false;
	s.SetContent(() =>
	{
		ViewContext.Writer.Write("<div class='dialogText' style='padding: 12px'>");
		ViewContext.Writer.Write("Falls Sie \"{claim}\" löschen möchten, was soll mit den Nachträgen geschehen, die bereits \"{claim}\" zugeordnet sind?");
		ViewContext.Writer.Write("</div>");

		ViewContext.Writer.Write("<div style='padding: 0 12px 12px 12px'>");
		Html.DevExpress().RadioButton(rb =>
		{
			rb.Name = "devRadioButtonDeleteReplaceClaim";
			rb.Text = "Eine neue(n) {claimType} zuweisen";
			rb.GroupName = "claimDelete";
			rb.Checked = true;
		}).Render();

		Session.Add("StatesDeleteComboBoxSettings", new Action<ComboBoxSettings>(a =>
		{
			a.Name = "devComboBoxClaimDeleteReplaceState";
			a.Width = new Unit(100, UnitType.Percentage);
			a.Properties.ValueType = typeof(int);
			a.Properties.ValueField = "Id";
			a.Properties.TextField = "Description";
			a.Properties.ClientSideEvents.BeginCallback = "function (s, e) { e.customArgs['excludedIds'] = [ deleteId ]; }";
			a.Properties.ClientSideEvents.EndCallback = "function (s, e) { s.SetSelectedIndex(0); }";
			a.SelectedIndex = 0;
			a.ClientVisible = false;
			a.CallbackRouteValues = new
			{
				Controller = "DataCallback",
				Action = "StatesComboBoxExcluded",
				SettingsKey = "StatesDeleteComboBoxSettings"
			};
		}));
		ViewData.Add("StatesComboBoxSettings", "StatesDeleteComboBoxSettings");
		Html.RenderPartial("~/Views/Shared/DataEditorTemplates/_StatesComboBox.cshtml", null, ViewData);

		Session.Add("CategoriesDeleteComboBoxSettings", new Action<ComboBoxSettings>(a =>
		{
			a.Name = "devComboBoxClaimDeleteReplaceCategory";
			a.Width = new Unit(100, UnitType.Percentage);
			a.Properties.ValueType = typeof(int);
			a.Properties.ValueField = "Id";
			a.Properties.TextField = "Description";
			a.Properties.ClientSideEvents.BeginCallback = "function (s, e) { e.customArgs['excludedIds'] = [ deleteId ]; }";
			a.Properties.ClientSideEvents.EndCallback = "function (s, e) { s.SetSelectedIndex(0); }";
			a.SelectedIndex = 0;
			a.ClientVisible = false;
			a.CallbackRouteValues = new
			{
				Controller = "DataCallback",
				Action = "CategoriesComboBoxExcluded",
				SettingsKey = "CategoriesDeleteComboBoxSettings"
			};
		}));
		ViewData.Add("CategoriesComboBoxSettings", "CategoriesDeleteComboBoxSettings");
		Html.RenderPartial("~/Views/Shared/DataEditorTemplates/_CategoriesComboBox.cshtml", null, ViewData);

		ViewContext.Writer.Write("<div class=\"deleteValidation\" style=\"display: none\"></div>");

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

		Html.RenderPartial(
			"~/Views/Shared/_PopupButtonPanelYesNo.cshtml",
			new YesNoDialogModel
			{
				PopupName = "devPopupControlDeleteClaim",
				YesFunction = "function (s, e) { deleteClaim(); }"
			}
		);
	});
	s.Styles.Content.Paddings.Padding = new Unit(0, UnitType.Pixel);
}).GetHtml()