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

<%
String redirect = ParamUtil.getString(request, "redirect");
%>

<liferay-portlet:resourceURL copyCurrentRenderParameters="<%= false %>" id="/export_import/export_import" var="importPortletURL">
	<portlet:param name="p_p_isolated" value="<%= Boolean.TRUE.toString() %>" />
	<portlet:param name="redirect" value="<%= redirect %>" />
	<portlet:param name="portletResource" value="<%= portletResource %>" />
	<portlet:param name="groupId" value="<%= String.valueOf(scopeGroupId) %>" />
	<portlet:param name="validate" value="<%= String.valueOf(Boolean.FALSE) %>" />
</liferay-portlet:resourceURL>

<aui:form action="<%= importPortletURL %>" cssClass="lfr-export-dialog" method="post" name="fm1">

	<%
	FileEntry fileEntry = ExportImportHelperUtil.getTempFileEntry(scopeGroupId, themeDisplay.getUserId(), ExportImportHelper.TEMP_FOLDER_NAME + selPortlet.getPortletId());
	%>

	<div class="lfr-dynamic-uploader <%= (fileEntry == null) ? "hide-dialog-footer" : StringPool.BLANK %>">
		<clay:container-fluid>
			<div class="lfr-upload-container" id="<portlet:namespace />fileUpload"></div>
		</clay:container-fluid>
	</div>

	<aui:button-row>
		<aui:button name="continueButton" type="submit" value="continue" />
	</aui:button-row>

	<%
	Date expirationDate = new Date(System.currentTimeMillis() + (PropsValues.SESSION_TIMEOUT * Time.MINUTE));

	Ticket ticket = TicketLocalServiceUtil.addTicket(user.getCompanyId(), User.class.getName(), user.getUserId(), TicketConstants.TYPE_IMPERSONATE, null, expirationDate, new ServiceContext());
	%>

	<aui:script use="liferay-upload">
		var liferayUpload = new Liferay.Upload({
			'boundingBox': '#<portlet:namespace />fileUpload',

			<%
			DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(locale);
			%>

			'decimalSeparator': '<%= decimalFormatSymbols.getDecimalSeparator() %>',

			'deleteFile':
				'<liferay-portlet:actionURL doAsUserId="<%= user.getUserId() %>" name="/export_import/export_import"><portlet:param name="mvcRenderCommandName" value="/export_import/export_import" /><portlet:param name="<%= Constants.CMD %>" value="<%= Constants.DELETE_TEMP %>" /><portlet:param name="redirect" value="<%= redirect %>" /><portlet:param name="portletResource" value="<%= portletResource %>" /></liferay-portlet:actionURL>&ticketKey=<%= ticket.getKey() %><liferay-ui:input-permissions-params modelName="<%= Group.class.getName() %>" />',

			<%
			DLConfiguration dlConfiguration = ConfigurationProviderUtil.getSystemConfiguration(DLConfiguration.class);
			%>

			'fileDescription':
				'<%= StringUtil.merge(dlConfiguration.fileExtensions()) %>',

			'maxFileSize':
				'<%= UploadServletRequestConfigurationProviderUtil.getMaxSize() %> B',
			'metadataContainer': '#<portlet:namespace />commonFileMetadataContainer',
			'metadataExplanationContainer':
				'#<portlet:namespace />metadataExplanationContainer',
			'multipleFiles': false,
			'namespace': '<portlet:namespace />',
			'strings.dropFileText':
				'<liferay-ui:message key="drop-a-lar-file-here-to-import" />',
			'strings.fileCannotBeSavedText':
				'<liferay-ui:message key="the-file-x-cannot-be-imported" />',
			'strings.pendingFileText':
				'<liferay-ui:message key="this-file-was-previously-uploaded-but-not-actually-imported" />',
			'strings.uploadsCompleteText':
				'<liferay-ui:message key="the-file-is-ready-to-be-imported" />',
			'tempFileURL': {
				method: Liferay.Service.bind('/layout/get-temp-file-names'),
				params: {
					folderName:
						'<%= HtmlUtil.escapeJS(ExportImportHelper.TEMP_FOLDER_NAME + selPortlet.getPortletId()) %>',
					groupId: <%= scopeGroupId %>,
				},
			},
			'uploadFile':
				'<liferay-portlet:actionURL doAsUserId="<%= user.getUserId() %>" name="/export_import/export_import"><portlet:param name="mvcRenderCommandName" value="/export_import/export_import" /><portlet:param name="<%= Constants.CMD %>" value="<%= Constants.ADD_TEMP %>" /><portlet:param name="redirect" value="<%= redirect %>" /><portlet:param name="plid" value="<%= String.valueOf(plid) %>" /> <portlet:param name="groupId" value="<%= String.valueOf(themeDisplay.getScopeGroupId()) %>" /><portlet:param name="portletResource" value="<%= portletResource %>" /></liferay-portlet:actionURL>&ticketKey=<%= ticket.getKey() %><liferay-ui:input-permissions-params modelName="<%= Group.class.getName() %>" />',
		});

		liferayUpload._uploader.on('alluploadscomplete', (event) => {
			toggleContinueButton();
		});

		Liferay.on('tempFileRemoved', (event) => {
			toggleContinueButton();
		});

		function toggleContinueButton() {
			var lfrDynamicUploader = liferayUpload
				.get('boundingBox')
				.ancestor('.lfr-dynamic-uploader');
			var uploadedFiles = liferayUpload._fileListContent.all(
				'.upload-file.upload-complete'
			);

			if (uploadedFiles.size() == 1) {
				lfrDynamicUploader.removeClass('hide-dialog-footer');
			}
			else {
				lfrDynamicUploader.addClass('hide-dialog-footer');
			}
		}
	</aui:script>
</aui:form>

<aui:script require="frontend-js-web/index as frontendJsWeb">
	var {runScriptsInElement} = frontendJsWeb;

	var continueButton = document.getElementById(
		'<portlet:namespace />continueButton'
	);
	var exportImportOptions = document.getElementById(
		'<portlet:namespace />exportImportOptions'
	);

	if (continueButton && exportImportOptions) {
		var form = document.<portlet:namespace />fm1;

		continueButton.addEventListener('click', (event) => {
			event.preventDefault();

			Liferay.Util.fetch(form.action)
				.then((response) => {
					return response.text();
				})
				.then((response) => {
					exportImportOptions.innerHTML = response;

					runScriptsInElement(exportImportOptions);
				});
		});
	}
</aui:script>