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

<liferay-frontend:side-panel-content
	title="<%= title %>"
>
	<portlet:actionURL name="/cp_definitions/edit_cp_attachment_file_entry" var="editCPAttachmentFileEntryActionURL" />

	<aui:form action="<%= editCPAttachmentFileEntryActionURL %>" method="post" name="fm" onSubmit='<%= "event.preventDefault(); " + liferayPortletResponse.getNamespace() + "saveAttachmentFileEntry();" %>'>
		<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= (cpAttachmentFileEntry == null) ? Constants.ADD : Constants.UPDATE %>" />
		<aui:input name="redirect" type="hidden" value="<%= currentURL %>" />
		<aui:input name="cpDefinitionId" type="hidden" value="<%= cpDefinitionId %>" />
		<aui:input name="cpAttachmentFileEntryId" type="hidden" value="<%= cpAttachmentFileEntryId %>" />
		<aui:input name="type" type="hidden" value="<%= type %>" />
		<aui:input name="workflowAction" type="hidden" value="<%= String.valueOf(WorkflowConstants.ACTION_SAVE_DRAFT) %>" />

		<commerce-ui:panel>
			<%@ include file="/attachment_file_entry/image_selector.jspf" %>
		</commerce-ui:panel>

		<commerce-ui:panel
			title='<%= LanguageUtil.get(request, "details") %>'
		>
			<%@ include file="/attachment_file_entry/details.jspf" %>
		</commerce-ui:panel>

		<commerce-ui:panel
			title='<%= LanguageUtil.get(request, "options") %>'
		>
			<%@ include file="/attachment_file_entry/options.jspf" %>
		</commerce-ui:panel>

		<commerce-ui:panel
			title='<%= LanguageUtil.get(request, "schedule") %>'
		>
			<%@ include file="/attachment_file_entry/schedule.jspf" %>
		</commerce-ui:panel>

		<c:if test="<%= cpAttachmentFileEntriesDisplayContext.hasCustomAttributes() %>">
			<commerce-ui:panel
				title='<%= LanguageUtil.get(request, "custom-attribute") %>'
			>
				<%@ include file="/attachment_file_entry/custom_fields.jspf" %>
			</commerce-ui:panel>
		</c:if>

		<%
		boolean pending = false;

		if (cpAttachmentFileEntry != null) {
			pending = cpAttachmentFileEntry.isPending();
		}
		%>

		<c:if test="<%= pending %>">
			<div class="alert alert-info">
				<liferay-ui:message key="there-is-a-publication-workflow-in-process" />
			</div>
		</c:if>

		<aui:button-row cssClass="product-definition-button-row">

			<%
			String saveButtonLabel = "save";

			if ((cpAttachmentFileEntry == null) || cpAttachmentFileEntry.isDraft() || cpAttachmentFileEntry.isApproved() || cpAttachmentFileEntry.isExpired() || cpAttachmentFileEntry.isScheduled()) {
				saveButtonLabel = "save-as-draft";
			}

			String publishButtonLabel = "publish";

			if (WorkflowDefinitionLinkLocalServiceUtil.hasWorkflowDefinitionLink(themeDisplay.getCompanyId(), scopeGroupId, CPAttachmentFileEntry.class.getName())) {
				publishButtonLabel = "submit-for-workflow";
			}
			%>

			<aui:button cssClass="btn-lg" disabled="<%= pending %>" name="publishButton" type="submit" value="<%= publishButtonLabel %>" />

			<aui:button cssClass="btn-lg" name="saveButton" primary="<%= false %>" type="submit" value="<%= saveButtonLabel %>" />

			<aui:button cssClass="btn-lg" type="cancel" />
		</aui:button-row>
	</aui:form>
</liferay-frontend:side-panel-content>

<%
String fieldValues = "[]";

if (cpAttachmentFileEntry != null) {
	fieldValues = cpAttachmentFileEntry.getJson();
}
%>

<aui:script require="commerce-frontend-js/utilities/forms/formsHelper as FormsHelper">
	var fieldValues = [];

	try {
		fieldValues = JSON.parse('<%= fieldValues %>');
	}
	catch (_ignore) {}

	Liferay.componentReady('ProductOptions<%= cpDefinitionId %>').then(
		(ddmForm) => {
			if (!fieldValues.length) {
				var ddmFormJSONShape = ddmForm.reactComponentRef.current.toJSON();
				fieldValues = FormsHelper.getDefaultFieldsShape(ddmFormJSONShape);
			}
			ddmForm.unstable_onEvent((event) => {
				var eventName = event.type;
				if (eventName === 'field_change') {
					fieldValues = FormsHelper.updateFields(
						fieldValues,
						event.payload
					);
					var form = document['<portlet:namespace />fm'];
					form[
						'<portlet:namespace />ddmFormValues'
					].value = JSON.stringify(fieldValues);
				}
			});
		}
	);

	Liferay.provide(window, '<portlet:namespace />saveAttachmentFileEntry', () => {
		var form = window.document['<portlet:namespace />fm'];

		var ddmForm = Liferay.component('ProductOptions<%= cpDefinitionId %>');

		if (ddmForm) {
			form['<portlet:namespace />ddmFormValues'].value = JSON.stringify(
				fieldValues
			);
		}

		submitForm(form);
	});
</aui:script>

<aui:script use="aui-base,event-input">
	var publishButton = A.one('#<portlet:namespace />publishButton');

	publishButton.on('click', () => {
		var workflowActionInput = A.one('#<portlet:namespace />workflowAction');

		if (workflowActionInput) {
			workflowActionInput.val('<%= WorkflowConstants.ACTION_PUBLISH %>');
		}
	});
</aui:script>