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

<%
DLPortletInstanceSettingsHelper dlPortletInstanceSettingsHelper = new DLPortletInstanceSettingsHelper(igRequestHelper);
%>

<liferay-portlet:actionURL portletConfiguration="<%= true %>" var="configurationActionURL" />

<liferay-portlet:renderURL portletConfiguration="<%= true %>" var="configurationRenderURL" />

<liferay-frontend:edit-form
	action="<%= configurationActionURL %>"
	method="post"
	name="fm"
	onSubmit='<%= "event.preventDefault(); " + liferayPortletResponse.getNamespace() + "saveConfiguration();" %>'
>
	<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
	<aui:input name="redirect" type="hidden" value="<%= configurationRenderURL %>" />
	<aui:input name="preferences--mimeTypes--" type="hidden" />
	<aui:input name="preferences--rootFolderId--" type="hidden" value="<%= rootFolderId %>" />

	<liferay-frontend:edit-form-body>
		<liferay-ui:error key="rootFolderIdInvalid" message="please-enter-a-valid-root-folder" />

		<liferay-frontend:fieldset-group>
			<liferay-frontend:fieldset
				collapsible="<%= true %>"
				id="imageGalleryDisplayDisplay"
				label="display-settings"
			>
				<aui:input label="show-actions" name="preferences--showActions--" type="checkbox" value="<%= dlPortletInstanceSettings.isShowActions() %>" />

				<aui:field-wrapper label="show-media-type">
					<liferay-ui:input-move-boxes
						leftBoxName="currentMimeTypes"
						leftList="<%= dlPortletInstanceSettingsHelper.getCurrentMimeTypes() %>"
						leftReorder="<%= Boolean.TRUE.toString() %>"
						leftTitle="current"
						rightBoxName="availableMimeTypes"
						rightList="<%= dlPortletInstanceSettingsHelper.getAvailableMimeTypes() %>"
						rightTitle="available"
					/>
				</aui:field-wrapper>

				<div class="display-template">
					<liferay-ddm:template-selector
						className="<%= FileEntry.class.getName() %>"
						displayStyle="<%= displayStyle %>"
						displayStyleGroupId="<%= displayStyleGroupId %>"
						refreshURL="<%= configurationRenderURL %>"
						showEmptyOption="<%= true %>"
					/>
				</div>
			</liferay-frontend:fieldset>

			<liferay-frontend:fieldset
				collapsible="<%= true %>"
				id="imageGalleryDisplayFoldersListingPanel"
				label="folders-listing"
			>
				<aui:field-wrapper>
					<div class="form-group">
						<aui:input label="root-folder" name="rootFolderName" type="resource" value="<%= rootFolderName %>" />

						<div class="alert alert-warning <%= rootFolderInTrash ? StringPool.BLANK : "hide" %>" id="<portlet:namespace />rootFolderInTrash">
							<liferay-ui:message key="the-selected-root-folder-is-in-the-recycle-bin-please-remove-it-or-select-another-one" />
						</div>

						<div class="alert alert-warning <%= rootFolderNotFound ? StringPool.BLANK : "hide" %>" id="<portlet:namespace />rootFolderNotFound">
							<liferay-ui:message key="the-selected-root-folder-cannot-be-found-please-select-another-one" />
						</div>

						<aui:button name="openFolderSelectorButton" value="select" />

						<%
						String taglibRemoveFolder = "Liferay.Util.removeEntitySelection('rootFolderId', 'rootFolderName', this, '" + liferayPortletResponse.getNamespace() + "');";
						%>

						<aui:button disabled="<%= rootFolderId <= 0 %>" name="removeFolderButton" onClick="<%= taglibRemoveFolder %>" value="remove" />
					</div>
				</aui:field-wrapper>
			</liferay-frontend:fieldset>
		</liferay-frontend:fieldset-group>
	</liferay-frontend:edit-form-body>

	<liferay-frontend:edit-form-footer>
		<aui:button type="submit" />

		<aui:button type="cancel" />
	</liferay-frontend:edit-form-footer>
</liferay-frontend:edit-form>

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

	if (openFolderSelectorButton) {
		openFolderSelectorButton.addEventListener('click', function (event) {
			Liferay.Util.getOpener().Liferay.Util.openSelectionModal({
				id:
					'_<%= HtmlUtil.escapeJS(igRequestHelper.getPortletResource()) %>_selectFolder',
				onSelect: function (selectedItem) {
					var folderData = {
						idString: 'rootFolderId',
						idValue: selectedItem.folderid,
						nameString: 'rootFolderName',
						nameValue: selectedItem.foldername,
					};

					Liferay.Util.selectFolder(folderData, '<portlet:namespace />');

					var rootFolderInTrashWarning = document.querySelector(
						'#<portlet:namespace />rootFolderInTrash'
					);

					rootFolderInTrashWarning.classList.add('hide');

					var rootFolderNotFoundWarning = document.querySelector(
						'#<portlet:namespace />rootFolderNotFound'
					);

					rootFolderNotFoundWarning.classList.add('hide');
				},
				selectEventName:
					'_<%= HtmlUtil.escapeJS(igRequestHelper.getPortletResource()) %>_selectFolder',
				title: '<liferay-ui:message arguments="folder" key="select-x" />',

				<liferay-portlet:renderURL portletName="<%= igRequestHelper.getPortletResource() %>" var="selectFolderURL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
					<portlet:param name="mvcRenderCommandName" value="/document_library/select_folder" />
					<portlet:param name="folderId" value="<%= (rootFolderInTrash || rootFolderNotFound) ? String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID): String.valueOf(rootFolderId) %>" />
					<portlet:param name="ignoreRootFolder" value="<%= Boolean.TRUE.toString() %>" />
					<portlet:param name="selectedFolderId" value="<%= String.valueOf(rootFolderId) %>" />
				</liferay-portlet:renderURL>

				url: '<%= HtmlUtil.escapeJS(selectFolderURL.toString()) %>',
			});
		});
	}

	function <portlet:namespace />saveConfiguration() {
		var form = document.<portlet:namespace />fm;

		Liferay.Util.postForm(form, {
			data: {
				mimeTypes: Liferay.Util.listSelect(
					Liferay.Util.getFormElement(form, 'currentMimeTypes')
				),
			},
		});
	}
</script>