<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<%@ include file="/display/init.jsp" %>

<%
KBArticle kbArticle = (KBArticle)request.getAttribute(KBWebKeys.KNOWLEDGE_BASE_KB_ARTICLE);

KBNavigationDisplayContext kbNavigationDisplayContext = (KBNavigationDisplayContext)request.getAttribute(KBWebKeys.KNOWLEDGE_BASE_KB_NAVIGATION_DISPLAY_CONTEXT);

String currentKBFolderURLTitle = kbNavigationDisplayContext.getCurrentKBFolderURLTitle();

long rootResourcePrimKey = kbDisplayPortletInstanceConfiguration.resourcePrimKey();

long rootKBFolderId = KBFolderConstants.DEFAULT_PARENT_FOLDER_ID;
String rootKBFolderName = LanguageUtil.get(resourceBundle, "home");
String rootKBFolderURLTitle = StringPool.BLANK;

if (rootResourcePrimKey != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
	KBFolder rootKBFolder = KBFolderServiceUtil.getKBFolder(rootResourcePrimKey);

	rootKBFolderId = rootKBFolder.getKbFolderId();
	rootKBFolderName = rootKBFolder.getName();
	rootKBFolderURLTitle = rootKBFolder.getUrlTitle();
}

List<KBFolder> kbFolders = KBUtil.getAlternateRootKBFolders(scopeGroupId, kbDisplayPortletInstanceConfiguration.resourcePrimKey());
%>

<liferay-portlet:actionURL name="/knowledge_base/update_root_kb_folder_id" var="updateRootKBFolderIdURL">
	<c:if test="<%= kbArticle != null %>">
		<portlet:param name="urlTitle" value="<%= kbArticle.getUrlTitle() %>" />
	</c:if>
</liferay-portlet:actionURL>

<div class="kbarticle-root-selector">
	<aui:form action="<%= updateRootKBFolderIdURL %>" name="updateRootKBFolderIdFm">
		<aui:select disabled="<%= kbFolders.isEmpty() %>" label="" name="rootKBFolderId">
			<c:if test="<%= KBArticleServiceUtil.getKBArticlesCount(scopeGroupId, rootKBFolderId, WorkflowConstants.STATUS_APPROVED) > 0 %>">
				<aui:option selected="<%= currentKBFolderURLTitle.equals(rootKBFolderURLTitle) %>" value="<%= rootKBFolderId %>">
					<%= HtmlUtil.escape(kbDisplayPortletInstanceConfiguration.contentRootPrefix() + " " + rootKBFolderName) %>
				</aui:option>
			</c:if>

			<%
			for (KBFolder kbFolder : kbFolders) {
			%>

				<aui:option selected="<%= currentKBFolderURLTitle.equals(kbFolder.getUrlTitle()) %>" value="<%= kbFolder.getKbFolderId() %>">
					<%= HtmlUtil.escape(kbDisplayPortletInstanceConfiguration.contentRootPrefix() + " " + kbFolder.getName()) %>
				</aui:option>

			<%
			}
			%>

		</aui:select>
	</aui:form>
</div>

<script>
	var <portlet:namespace />form = document.getElementById(
		'<portlet:namespace />updateRootKBFolderIdFm'
	);

	if (<portlet:namespace />form) {
		document
			.getElementById('<portlet:namespace />rootKBFolderId')
			.addEventListener('change', () => {
				<portlet:namespace />form.submit();
			});
	}
</script>