<%--
/**
 * 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="/item/selector/init.jsp" %>

<%
WikiPageItemSelectorViewDisplayContext wikiPageItemSelectorViewDisplayContext = (WikiPageItemSelectorViewDisplayContext)request.getAttribute(WikiItemSelectorWebKeys.WIKI_PAGE_ITEM_SELECTOR_VIEW_DISPLAY_CONTEXT);

WikiNode node = wikiPageItemSelectorViewDisplayContext.getNode();

String keywords = ParamUtil.getString(request, "keywords");

SearchContainer<WikiPage> wikiPagesSearchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, wikiPageItemSelectorViewDisplayContext.getPortletURL(request, liferayPortletResponse), null, wikiPageItemSelectorViewDisplayContext.isSearch() ? LanguageUtil.format(locale, "no-pages-were-found-that-matched-the-keywords-x", "<strong>" + HtmlUtil.escape(keywords) + "</strong>", false) : "there-are-no-pages");

if (wikiPageItemSelectorViewDisplayContext.isSearch()) {
	Indexer<WikiPage> indexer = IndexerRegistryUtil.getIndexer(WikiPage.class);

	SearchContext searchContext = SearchContextFactory.getInstance(request);

	searchContext.setEnd(wikiPagesSearchContainer.getEnd());
	searchContext.setIncludeAttachments(false);
	searchContext.setIncludeDiscussions(false);
	searchContext.setNodeIds(new long[] {node.getNodeId()});
	searchContext.setStart(wikiPagesSearchContainer.getStart());

	Hits hits = indexer.search(searchContext);

	wikiPagesSearchContainer.setTotal(hits.getLength());

	List<SearchResult> searchResults = SearchResultUtil.getSearchResults(hits, themeDisplay.getLocale());

	List<WikiPage> results = new ArrayList<>();

	for (SearchResult searchResult : searchResults) {
		WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(searchResult.getClassPK());

		results.add(wikiPage);
	}

	wikiPagesSearchContainer.setResults(results);
}
else {
	wikiPagesSearchContainer.setTotal(WikiPageLocalServiceUtil.getPagesCount(node.getNodeId(), true, wikiPageItemSelectorViewDisplayContext.getStatus()));
	wikiPagesSearchContainer.setResults(WikiPageLocalServiceUtil.getPages(node.getNodeId(), true, wikiPageItemSelectorViewDisplayContext.getStatus(), wikiPagesSearchContainer.getStart(), wikiPagesSearchContainer.getEnd()));
}
%>

<style type="text/css">
	.portlet-item-selector .wiki-page-item {
		cursor: pointer;
	}
</style>

<%
PortletURL searchBaseURL = PortletURLUtil.clone(currentURLObj, liferayPortletResponse);

searchBaseURL.setParameter("resetCur", Boolean.TRUE.toString());

String searchURL = HttpUtil.removeParameter(searchBaseURL.toString(), liferayPortletResponse.getNamespace() + "keywords");
%>

<clay:management-toolbar
	clearResultsURL="<%= searchURL %>"
	itemsTotal="<%= wikiPagesSearchContainer.getTotal() %>"
	searchActionURL="<%= searchURL %>"
	selectable="<%= false %>"
	showCreationMenu="<%= false %>"
/>

<clay:container-fluid
	cssClass="lfr-item-viewer"
	id='<%= liferayPortletResponse.getNamespace() + "wikiPagesSelectorContainer" %>'
>
	<liferay-ui:search-container
		id="wikiPagesSearchContainer"
		searchContainer="<%= wikiPagesSearchContainer %>"
		total="<%= wikiPagesSearchContainer.getTotal() %>"
	>
		<liferay-ui:search-container-results
			results="<%= wikiPagesSearchContainer.getResults() %>"
		/>

		<liferay-ui:search-container-row
			className="com.liferay.wiki.model.WikiPage"
			cssClass="wiki-page-item"
			keyProperty="pageId"
			modelVar="curPage"
		>
			<liferay-ui:search-container-column-icon
				icon="wiki-page"
			/>

			<liferay-ui:search-container-column-text
				colspan="<%= 2 %>"
			>

				<%
				Date modifiedDate = curPage.getModifiedDate();

				String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
				%>

				<h5 class="text-default">
					<c:choose>
						<c:when test="<%= Validator.isNotNull(curPage.getUserName()) %>">
							<liferay-ui:message arguments="<%= new String[] {HtmlUtil.escape(curPage.getUserName()), modifiedDateDescription} %>" key="x-modified-x-ago" />
						</c:when>
						<c:otherwise>
							<liferay-ui:message arguments="<%= modifiedDateDescription %>" key="modified-x-ago" />
						</c:otherwise>
					</c:choose>
				</h5>

				<%
				WikiPageItemSelectorReturnTypeResolver wikiPageItemSelectorReturnTypeResolver = wikiPageItemSelectorViewDisplayContext.getWikiPageItemSelectorReturnTypeResolver();
				%>

				<h4>
					<a class="wiki-page" data-title="<%= wikiPageItemSelectorReturnTypeResolver.getTitle(curPage, themeDisplay) %>" data-value="<%= wikiPageItemSelectorReturnTypeResolver.getValue(curPage, themeDisplay) %>" href="javascript:;">
						<%= curPage.getTitle() %>
					</a>
				</h4>

				<h5 class="text-default">
					<aui:workflow-status markupView="lexicon" showIcon="<%= false %>" showLabel="<%= false %>" status="<%= curPage.getStatus() %>" />
				</h5>
			</liferay-ui:search-container-column-text>
		</liferay-ui:search-container-row>

		<liferay-ui:search-iterator
			displayStyle="descriptive"
			markupView="lexicon"
			searchContainer="<%= wikiPagesSearchContainer %>"
		/>
	</liferay-ui:search-container>
</clay:container-fluid>

<aui:script use="liferay-search-container">
	var Util = Liferay.Util;

	var searchContainer = Liferay.SearchContainer.get(
		'<portlet:namespace />wikiPagesSearchContainer'
	);

	var searchContainerContentBox = searchContainer.get('contentBox');

	searchContainerContentBox.delegate(
		'click',
		function (event) {
			var selectedItem = event.currentTarget;

			var linkItem = selectedItem.one('.wiki-page');

			Util.getOpener().Liferay.fire(
				'<%= wikiPageItemSelectorViewDisplayContext.getItemSelectedEventName() %>',
				{
					data: {
						title: linkItem.attr('data-title'),
						value: linkItem.attr('data-value'),
					},
				}
			);

			selectedItem.siblings().removeClass('active');
			selectedItem.addClass('active');
		},
		'.list-group-item'
	);
</aui:script>