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

<%
String csvExport = "enabled-with-warning";

if (Validator.isNotNull(ddmFormAdminDisplayContext.getCSVExport())) {
	csvExport = ddmFormAdminDisplayContext.getCSVExport();
}

boolean csvExportDisabled = StringUtil.equals("disabled", csvExport);
%>

<div class="hide" id="<portlet:namespace />exportFormInstance">
	<clay:alert
		message="timezone-warning-message"
	/>

	<clay:alert
		message="the-export-includes-data-from-all-fields-and-form-versions"
	/>

	<div class="hide" id="<portlet:namespace />csvWarning">
		<clay:alert
			displayType="warning"
			message="csv-warning-message"
		/>
	</div>

	<div class="hide" id="<portlet:namespace />xlsWarning">
		<clay:alert
			displayType="warning"
			message="the-total-number-of-characters-that-a-cell-can-contain-is-32767-characters"
		/>

		<clay:alert
			displayType="warning"
			message="the-total-number-of-columns-that-a-worksheet-can-contain-is-256-columns"
		/>
	</div>

	<aui:select label="file-extension" name="fileExtension">

		<%
		Map<String, String> availableExportExtensions = ddmFormAdminDisplayContext.getAvailableExportExtensions();

		for (Map.Entry<String, String> entry : availableExportExtensions.entrySet()) {
			if (Objects.equals(entry.getKey(), "csv") && csvExportDisabled) {
				continue;
			}
		%>

			<aui:option value="<%= entry.getKey() %>"><%= entry.getValue() %></aui:option>

		<%
		}
		%>

	</aui:select>
</div>

<aui:script>
	Liferay.provide(
		window,
		'<portlet:namespace />exportFormInstance',
		function (url) {
			var A = AUI();

			var form = A.Node.create('<form />');

			form.attr('method', 'POST');

			var content = A.one('#<portlet:namespace />exportFormInstance');

			var fileExtensionSelect = A.one('#<portlet:namespace />fileExtension');

			var showCSVWarning = function () {
				var csvWarning = A.one('#<portlet:namespace />csvWarning');

				if (
					fileExtensionSelect.val() === 'csv' &&
					<%= StringUtil.equals("enabled-with-warning", csvExport) %>
				) {
					csvWarning.show();
				}
				else {
					csvWarning.hide();
				}
			};

			var showXLSWarning = function () {
				var xlsWarning = A.one('#<portlet:namespace />xlsWarning');

				if (fileExtensionSelect.val() === 'xls') {
					xlsWarning.show();
				}
				else {
					xlsWarning.hide();
				}
			};

			var showWarnings = function () {
				showCSVWarning();

				showXLSWarning();
			};

			if (content) {
				if (fileExtensionSelect) {
					showWarnings();

					fileExtensionSelect.on('change', showWarnings);
				}

				form.append(content);

				content.show();
			}

			var dialog = Liferay.Util.Window.getWindow({
				dialog: {
					bodyContent: form,
					cssClass: 'ddm-form-export-modal',
					resizable: false,
					toolbars: {
						footer: [
							{
								cssClass: 'btn-secondary',
								label: '<liferay-ui:message key="cancel" />',
								on: {
									click: function () {
										dialog.hide();
									},
								},
							},
							{
								cssClass: 'btn-primary',
								label: '<liferay-ui:message key="ok" />',
								on: {
									click: function () {
										submitForm(form, url, false);

										dialog.hide();
									},
								},
							},
						],
						header: [
							{
								cssClass: 'close',
								discardDefaultButtonCssClasses: true,
								labelHTML: Liferay.Util.getLexiconIconTpl('times'),
								on: {
									click: function () {
										dialog.hide();
									},
								},
							},
						],
					},
					width: 600,
				},
				title: '<%= UnicodeLanguageUtil.get(request, "export") %>',
			});
		},
		['aui-alert', 'liferay-util-window']
	);
</aui:script>