<%--
/**
 * 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 %>">
					<%= HtmlUtil.escape(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 sandbox="<%= true %>">
	const selectFileButton = document.getElementById(
		'<portlet:namespace />selectFile'
	);

	if (selectFileButton) {
		const openerWindow = Liferay.Util.getOpener();

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

			openerWindow.Liferay.Util.openSelectionModal({
				onSelect: (selectedItem) => {
					if (!selectedItem || !selectedItem.value) {
						return;
					}

					const value = JSON.parse(selectedItem.value);

					const fileEntryIdInput = document.getElementById(
						'<portlet:namespace />fileEntryId'
					);

					if (fileEntryIdInput) {
						fileEntryIdInput.value = value.fileEntryId;
					}

					const fileEntryTitle = document.getElementById(
						'<portlet:namespace />fileEntryTitle'
					);

					if (fileEntryTitle) {
						fileEntryTitle.innerHTML = value.title;
					}

					const fileEntryContainer = document.getElementById(
						'<portlet:namespace />fileEntryContainer'
					);

					if (fileEntryContainer) {
						fileEntryContainer.classList.remove('hidden');
					}

					const deleteFileButton = document.getElementById(
						'<portlet:namespace />deleteFile'
					);

					if (deleteFileButton) {
						deleteFileButton.classList.remove('hidden');
					}
				},
				selectEventName: 'addCPAttachmentFileEntry',
				title: '<liferay-ui:message key="select-file" />',
				url:
					'<%= cpAttachmentFileEntriesDisplayContext.getAttachmentItemSelectorUrl() %>',
			});
		});
	}

	const deleteFileButton = document.getElementById(
		'<portlet:namespace />deleteFile'
	);

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

			const fileEntryIdInput = document.getElementById(
				'<portlet:namespace />fileEntryId'
			);

			if (fileEntryIdInput) {
				fileEntryIdInput.value = 0;
			}

			const fileEntryTitle = document.getElementById(
				'<portlet:namespace />fileEntryTitle'
			);

			if (fileEntryTitle) {
				fileEntryTitle.innerHTML = '';
			}

			const fileEntryContainer = document.getElementById(
				'<portlet:namespace />fileEntryContainer'
			);

			if (fileEntryContainer) {
				fileEntryContainer.classList.add('hidden');
			}

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