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

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

DDMFormInstance formInstance = ddmFormAdminDisplayContext.getDDMFormInstance();

long formInstanceId = BeanParamUtil.getLong(formInstance, request, "formInstanceId");
long groupId = BeanParamUtil.getLong(formInstance, request, "groupId", scopeGroupId);
long ddmStructureId = BeanParamUtil.getLong(formInstance, request, "structureId");

String defaultLanguageId = ddmFormAdminDisplayContext.getDefaultLanguageId();

Locale[] availableLocales = ddmFormAdminDisplayContext.getAvailableLocales();

portletDisplay.setShowBackIcon(true);
portletDisplay.setURLBack(redirect);

renderResponse.setTitle((formInstance == null) ? LanguageUtil.get(request, "new-form") : LanguageUtil.get(request, "edit-form"));
%>

<div class="loading-animation" id="<portlet:namespace />loader"></div>

<portlet:actionURL name="saveFormInstance" var="saveFormInstanceURL">
	<portlet:param name="mvcRenderCommandName" value="/admin/edit_form_instance" />
</portlet:actionURL>

<div class="hide portlet-forms" id="<portlet:namespace />formContainer">
	<clay:navigation-bar
		componentId="formsNavigationBar"
		inverted="<%= true %>"
		navigationItems="<%= ddmFormAdminDisplayContext.getFormBuilderNavigationItems() %>"
	/>

	<nav class="management-bar management-bar-light navbar navbar-expand-md toolbar-group-field">
		<div class="autosave-bar container toolbar">
			<div class="navbar-form navbar-form-autofit navbar-overlay toolbar-group-content">
				<span class="autosave-feedback management-bar-text" id="<portlet:namespace />autosaveMessage"></span>
			</div>

			<ul class="navbar-nav toolbar-group-field">
				<li class="nav-item">
					<button class="btn btn-secondary nav-btn nav-btn-monospaced publish-icon <%= (formInstance == null) ? "hide" : "" %>" data-original-title="<liferay-ui:message key="copy-url" />" id="<portlet:namespace />publishIcon" title="<liferay-ui:message key="copy-url" />" type="button">
						<svg class="lexicon-icon">
							<use xlink:href="<%= ddmFormAdminDisplayContext.getLexiconIconsPath() %>link" />
						</svg>
					</button>
				</li>
				<li class="nav-item">
					<button class="btn btn-primary lfr-ddm-add-field lfr-ddm-plus-button nav-btn nav-btn-monospaced">
						<svg class="lexicon-icon">
							<use xlink:href="<%= ddmFormAdminDisplayContext.getLexiconIconsPath() %>plus" />
						</svg>
					</button>
				</li>
			</ul>
		</div>
	</nav>

	<div class="container-fluid-1280">
		<aui:translation-manager availableLocales="<%= availableLocales %>" changeableDefaultLanguage="<%= false %>" defaultLanguageId="<%= defaultLanguageId %>" id="translationManager" />
	</div>

	<aui:form action="<%= saveFormInstanceURL %>" cssClass="ddm-form-builder-form" enctype="multipart/form-data" method="post" name="editForm">
		<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
		<aui:input name="formInstanceId" type="hidden" value="<%= formInstanceId %>" />
		<aui:input name="groupId" type="hidden" value="<%= groupId %>" />
		<aui:input name="ddmStructureId" type="hidden" value="<%= ddmStructureId %>" />
		<aui:input name="serializedSettingsContext" type="hidden" value="" />

		<%@ include file="/admin/exceptions.jspf" %>

		<div class="ddm-form-basic-info">
			<div class="container-fluid-1280">
				<h1>
					<liferay-ui:input-editor
						autoCreate="<%= false %>"
						contents="<%= HtmlUtil.escapeAttribute(ddmFormAdminDisplayContext.getFormName()) %>"
						cssClass="ddm-form-name"
						editorName="alloyeditor"
						name="nameEditor"
						placeholder="untitled-form"
						showSource="<%= false %>"
					/>
				</h1>

				<aui:input name="name" type="hidden" />

				<h5>
					<liferay-ui:input-editor
						autoCreate="<%= false %>"
						contents="<%= HtmlUtil.escapeAttribute(ddmFormAdminDisplayContext.getFormDescription()) %>"
						cssClass="ddm-form-description h5"
						editorName="alloyeditor"
						name="descriptionEditor"
						placeholder="add-a-short-description-for-this-form"
						showSource="<%= false %>"
					/>
				</h5>

				<aui:input name="description" type="hidden" />
			</div>
		</div>

		<div class="container-fluid-1280 ddm-form-builder-app">
			<aui:input name="serializedFormBuilderContext" type="hidden" />

			<div id="<portlet:namespace />formBuilder"></div>
			<div id="<portlet:namespace />ruleBuilder"></div>
		</div>

		<div class="container-fluid-1280">
			<aui:button-row cssClass="ddm-form-builder-buttons">
				<aui:button cssClass="btn-primary ddm-button" id="publish" value='<%= ddmFormAdminDisplayContext.isFormPublished() ? "unpublish-form": "publish-form" %>' />

				<aui:button cssClass="ddm-button" id="save" value="save-form" />

				<aui:button cssClass="btn-link" id="preview" value="preview-form" />
			</aui:button-row>
		</div>

		<portlet:actionURL name="publishFormInstance" var="publishFormInstanceURL">
			<portlet:param name="mvcRenderCommandName" value="/admin/edit_form_instance" />
		</portlet:actionURL>

		<liferay-portlet:resourceURL copyCurrentRenderParameters="<%= false %>" id="saveFormInstance" var="autoSaveFormInstanceURL" />

		<liferay-form:ddm-form-builder
			ddmStructureVersionId="<%= ddmFormAdminDisplayContext.getLatestDDMStructureVersionId() %>"
			defaultLanguageId="<%= ddmFormAdminDisplayContext.getDefaultLanguageId() %>"
			editingLanguageId="<%= ddmFormAdminDisplayContext.getDefaultLanguageId() %>"
			fieldSetClassNameId="<%= PortalUtil.getClassNameId(DDMFormInstance.class) %>"
			refererPortletNamespace="<%= liferayPortletResponse.getNamespace() %>"
		/>

		<aui:script use="aui-promise,liferay-ddm-form-portlet">
			Liferay.namespace('DDM').FormSettings = {
				autosaveInterval: '<%= ddmFormAdminDisplayContext.getAutosaveInterval() %>',
				autosaveURL: '<%= autoSaveFormInstanceURL.toString() %>',
				portletNamespace: '<portlet:namespace />',
				publishFormInstanceURL: '<%= publishFormInstanceURL.toString() %>',
				restrictedFormURL: '<%= ddmFormAdminDisplayContext.getRestrictedFormURL() %>',
				sharedFormURL: '<%= ddmFormAdminDisplayContext.getSharedFormURL() %>',
				showPagination: true
			};

			A.Promise.all(
				[
					new A.Promise(
						function(resolve) {
							var formRegisteredHandler = Liferay.after(
								'form:registered',
								function(event) {
									if (event.formName === '<portlet:namespace />editForm') {
										formRegisteredHandler.detach();

										resolve(event.form);
									}
								}
							);
						}
					),
					new A.Promise(
						function(resolve) {
							var formBuilderRegisteredHandler = Liferay.after(
								'<portlet:namespace />formBuilder:registered',
								function(event) {
									formBuilderRegisteredHandler.detach();

									resolve();
								}
							);
						}
					),
					new A.Promise(
						function(resolve) {
							var ruleBuilderRegisteredHandler = Liferay.after(
								'<portlet:namespace />ruleBuilder:registered',
								function(event) {
									ruleBuilderRegisteredHandler.detach();

									resolve();
								}
							);
						}
					),
					new A.Promise(
						function(resolve) {
							if (Liferay.DMMFieldTypesReady) {
								resolve();
							}
							else {
								var fieldTypesHandler = Liferay.onceAfter(
									'DMMFieldTypesReady',
									function() {
										fieldTypesHandler.detach();

										resolve();
									}
								);
							}
						}
					)
				]
			).then(
				function(result) {
					var form = result[0];

					Liferay.component(
						'formPortlet',
						new Liferay.DDM.FormPortlet(
							{
								defaultLanguageId: '<%= ddmFormAdminDisplayContext.getDefaultLanguageId() %>',
								editForm: form,
								editingLanguageId: '<%= ddmFormAdminDisplayContext.getDefaultLanguageId() %>',
								formBuilder: Liferay.component('<portlet:namespace />formBuilder'),
								formInstanceId: <%= formInstanceId %>,
								localizedDescription: <%= ddmFormAdminDisplayContext.getFormLocalizedDescription() %>,
								localizedName: <%= ddmFormAdminDisplayContext.getFormLocalizedName() %>,
								namespace: '<portlet:namespace />',
								published: !!<%= ddmFormAdminDisplayContext.isFormPublished() %>,
								publishFormInstanceURL: '<%= publishFormInstanceURL.toString() %>',
								ruleBuilder: Liferay.component('<portlet:namespace />ruleBuilder'),
								showPublishAlert: <%= ddmFormAdminDisplayContext.isShowPublishAlert() %>,
								translationManager: Liferay.component('<portlet:namespace />translationManager')
							}
						)
					);
				}
			).catch(
				function(error) {
					throw error;
				}
			);

			Liferay.namespace('FormPortlet').destroySettings = function() {
				var settingsNode = A.one('#<portlet:namespace />settingsModal');

				if (settingsNode) {
					Liferay.Util.getWindow('<portlet:namespace />settingsModal').destroy();
				}
			};

			Liferay.namespace('DDM').openSettings = function() {
				Liferay.Util.openWindow(
					{
						dialog: {
							cssClass: 'ddm-form-settings-modal',
							height: 700,
							resizable: false,
							'toolbars.footer': [
								{
									cssClass: 'btn-link',
									label: '<liferay-ui:message key="cancel" />',
									on: {
										click: function() {
											Liferay.Util.getWindow('<portlet:namespace />settingsModal').hide();
										}
									}
								},
								{
									cssClass: 'btn-primary',
									label: '<liferay-ui:message key="done" />',
									on: {
										click: function() {
											var ddmForm = Liferay.component('settingsDDMForm');

											ddmForm.validate(
												function(hasErrors) {
													if (!hasErrors) {
														Liferay.Util.getWindow('<portlet:namespace />settingsModal').hide();
													}
												}
											);
										}
									}
								}
							],
							width: 720
						},
						id: '<portlet:namespace />settingsModal',
						title: '<liferay-ui:message key="form-settings" />'
					},
					function(dialogWindow) {
						var bodyNode = dialogWindow.bodyNode;

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

						settingsNode.show();

						bodyNode.append(settingsNode);
					}
				);
			};

			var clearPortletHandlers = function(event) {
				if (event.portletId === '<%= portletDisplay.getRootPortletId() %>') {
					Liferay.detach('destroyPortlet', clearPortletHandlers);
					Liferay.namespace('FormPortlet').destroySettings();

					Liferay.destroyComponents(
						function(component) {
							var destroy = false;

							if (
								component === Liferay.component('<portlet:namespace />formBuilder') ||
								component === Liferay.component('<portlet:namespace />ruleBuilder') ||
								component === Liferay.component('<portlet:namespace />translationManager') ||
								component === Liferay.component('formPortlet') ||
								component === Liferay.component('settingsDDMForm')
							) {
								destroy = true;
							}

							return destroy;
						}
					);
				}
			};

			Liferay.on('destroyPortlet', clearPortletHandlers);
		</aui:script>
	</aui:form>

	<div class="container-fluid-1280 ddm-form-instance-settings hide" id="<portlet:namespace />settings">
		<%= request.getAttribute(DDMWebKeys.DYNAMIC_DATA_MAPPING_FORM_HTML) %>
	</div>

	<%@ include file="/admin/copy_form_publish_url.jspf" %>
</div>