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

<%
long fileEntryId = BeanParamUtil.getLong(cpAttachmentFileEntry, request, "fileEntryId");
%>

<liferay-ui:error exception="<%= DuplicateCPAttachmentFileEntryException.class %>" message="that-attachment-is-already-in-use-on-this-product" />
<liferay-ui:error exception="<%= NoSuchFileEntryException.class %>" message="please-select-an-existing-file" />

<portlet:actionURL name="/cp_definitions/upload_temp_attachment" var="uploadCoverImageURL">
	<portlet:param name="cpDefinitionId" value="<%= String.valueOf(cpDefinitionId) %>" />
</portlet:actionURL>

<c:choose>
	<c:when test="<%= type == CPAttachmentFileEntryConstants.TYPE_IMAGE %>">
		<div class="lfr-attachment-cover-image-selector">
			<liferay-item-selector:image-selector
				draggableImage="vertical"
				fileEntryId="<%= fileEntryId %>"
				itemSelectorEventName="addCPAttachmentFileEntry"
				itemSelectorURL="<%= cpAttachmentFileEntriesDisplayContext.getImageItemSelectorUrl() %>"
				maxFileSize="<%= cpAttachmentFileEntriesDisplayContext.getImageMaxSize() %>"
				paramName="fileEntry"
				uploadURL="<%= uploadCoverImageURL %>"
				validExtensions="<%= StringUtil.merge(cpAttachmentFileEntriesDisplayContext.getImageExtensions(), StringPool.COMMA_AND_SPACE) %>"
			/>
		</div>
	</c:when>
	<c:when test="<%= type == CPAttachmentFileEntryConstants.TYPE_OTHER %>">
		<aui:input name="fileEntryId" type="hidden" value="<%= fileEntryId %>" />

		<div id="<portlet:namespace />fileEntryContainer">
			<h5 id="<portlet:namespace />fileEntryTitle">
				<c:if test="<%= fileEntryId > 0 %>">
					<%= cpAttachmentFileEntriesDisplayContext.getFileEntryName() %>
				</c:if>
			</h5>
		</div>

		<aui:button name="selectFile" value="select-file" />

		<aui:button cssClass='<%= (fileEntryId > 0) ? StringPool.BLANK : "hidden" %>' name="deleteFile" value="delete" />
	</c:when>
</c:choose>

<aui:script use="liferay-item-selector-dialog">
	var selectFileButton = window.document.querySelector(
		'#<portlet:namespace />selectFile'
	);

	if (selectFileButton) {
		selectFileButton.addEventListener('click', function (event) {
			event.preventDefault();

			var itemSelectorDialog = new A.LiferayItemSelectorDialog({
				eventName: 'addCPAttachmentFileEntry',
				on: {
					selectedItemChange: function (event) {
						var selectedItem = event.newVal;

						if (selectedItem) {
							var value = JSON.parse(selectedItem.value);

							window.document.querySelector(
								'#<portlet:namespace />fileEntryId'
							).value = value.fileEntryId;

							window.document.querySelector(
								'#<portlet:namespace />fileEntryTitle'
							).innerHTML = value.title;

							window.document
								.querySelector(
									'#<portlet:namespace />fileEntryContainer'
								)
								.classList.remove('hidden');

							window.document
								.querySelector('#<portlet:namespace />deleteFile')
								.classList.remove('hidden');
						}
					},
				},
				title: '<liferay-ui:message key="select-file" />',
				url:
					'<%= cpAttachmentFileEntriesDisplayContext.getAttachmentItemSelectorUrl() %>',
			});

			itemSelectorDialog.open();
		});
	}
</aui:script>

<aui:script>
	var deleteFileButton = window.document.querySelector(
		'#<portlet:namespace />deleteFile'
	);

	if (deleteFileButton) {
		deleteFileButton.addEventListener('click', function (event) {
			event.preventDefault();

			window.document.querySelector(
				'#<portlet:namespace />fileEntryId'
			).value = 0;

			window.document.querySelector(
				'#<portlet:namespace />fileEntryTitle'
			).innerHTML = '';

			window.document
				.querySelector('#<portlet:namespace />fileEntryContainer')
				.classList.add('hidden');

			deleteFileButton.classList.add('hidden');
		});
	}
</aui:script>