<%--
/**
 * 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.
 *
 *
 *
 */
--%>

<div class="alert alert-info">
	<liferay-ui:message key="use-the-form-below-to-move-a-page-and-all-of-its-history-to-be-the-child-of-a-new-parent-page" />
</div>

<%
String parentText = StringPool.BLANK;

WikiPage parentPage = wikiPage.getViewableParentPage();

if (parentPage == null) {
	parentText = StringPool.OPEN_PARENTHESIS + LanguageUtil.get(request, "none") + StringPool.CLOSE_PARENTHESIS;
}
else {
	parentText = parentPage.getTitle();

	parentPage = parentPage.getViewableParentPage();

	while (parentPage != null) {
		parentText = parentPage.getTitle() + " &raquo; " + parentText;

		parentPage = parentPage.getViewableParentPage();
	}
}

List<WikiPage> childPages = WikiPageLocalServiceUtil.getChildren(node.getNodeId(), true, StringPool.BLANK);

childPages = ListUtil.sort(childPages);

childPages.remove(wikiPage);
%>

<aui:form action="<%= movePageURL %>" method="post" name="changeParentFm" onSubmit='<%= liferayPortletResponse.getNamespace() + "changeParent();" %>'>
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	<aui:input name="nodeId" type="hidden" value="<%= node.getNodeId() %>" />
	<aui:input name="title" type="hidden" value="<%= title %>" />
	<aui:input name="workflowAction" type="hidden" value="<%= WorkflowConstants.ACTION_PUBLISH %>" />

	<aui:input name="redirect" type="hidden" value="<%= redirect %>" />

	<aui:input name="currentParent" type="resource" value="<%= parentText %>" />

	<%
	boolean newParentAvailable = true;
	%>

	<c:choose>
		<c:when test="<%= childPages.isEmpty() %>">

			<%
			newParentAvailable = false;
			%>

			<aui:select disabled="<%= true %>" label="new-parent" name="newParentTitle">
				<aui:option label="not-available" value="" />
			</aui:select>
		</c:when>
		<c:otherwise>
			<aui:select label="new-parent" name="newParentTitle">
				<aui:option label="none" selected="<%= Validator.isNull(wikiPage.getParentTitle()) %>" value="" />

				<%
				for (WikiPage childPage : childPages) {
				%>

					<c:if test="<%= Validator.isNull(childPage.getRedirectTitle()) %>">

						<%
						request.setAttribute(WikiWebKeys.WIKI_TREE_WALKER_DEPTH, 1);
						request.setAttribute(WikiWebKeys.WIKI_TREE_WALKER_PAGE, wikiPage);
						request.setAttribute(WikiWebKeys.WIKI_TREE_WALKER_PARENT, childPage);
						%>

						<liferay-util:include page="/wiki/page_tree.jsp" servletContext="<%= application %>" />
					</c:if>

				<%
				}
				%>

			</aui:select>
		</c:otherwise>
	</c:choose>

	<c:if test="<%= pending %>">
		<div class="alert alert-info">
			<liferay-ui:message key="there-is-a-publication-workflow-in-process" />
		</div>
	</c:if>

	<div class="sheet-footer">
		<c:choose>
			<c:when test="<%= hasWorkflowDefinitionLink %>">
				<aui:button disabled="<%= pending %>" name="publishButton" onClick='<%= liferayPortletResponse.getNamespace() + "publishPage();" %>' value="submit-for-publication" />
			</c:when>
			<c:otherwise>
				<aui:button disabled="<%= !newParentAvailable %>" type="submit" value="change-parent" />
			</c:otherwise>
		</c:choose>

		<aui:button href="<%= redirect %>" type="cancel" />
	</div>
</aui:form>

<aui:script>
	function <portlet:namespace />changeParent() {
		document.<portlet:namespace />changeParentFm.<portlet:namespace /><%= Constants.CMD %>.value =
			'<%= Constants.CHANGE_PARENT %>';

		submitForm(document.<portlet:namespace />changeParentFm);
	}

	function <portlet:namespace />publishPage() {
		document.<portlet:namespace />changeParentFm.<portlet:namespace />workflowAction.value =
			'<%= WorkflowConstants.ACTION_PUBLISH %>';

		<portlet:namespace />changeParent();
	}
</aui:script>