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

<%
AssetListEntry assetListEntry = assetPublisherDisplayContext.fetchAssetListEntry();
%>

<aui:input id="assetListEntryId" name="preferences--assetListEntryId--" type="hidden" value="<%= (assetListEntry != null) ? assetListEntry.getAssetListEntryId() : StringPool.BLANK %>" />
<aui:input id="infoListProviderKey" name="preferences--infoListProviderKey--" type="hidden" value="<%= assetPublisherDisplayContext.getInfoListProviderKey() %>" />

<div class="form-group input-text-wrapper text-default" id="<portlet:namespace />title">
	<c:choose>
		<c:when test="<%= assetListEntry != null %>">
			<%= HtmlUtil.escape(assetListEntry.getTitle()) %>
		</c:when>
		<c:when test="<%= Validator.isNotNull(assetPublisherDisplayContext.getInfoListProviderKey()) %>">
			<%= assetPublisherDisplayContext.getInfoListProviderLabel() %>
		</c:when>
		<c:otherwise>
			<span class="text-muted"><liferay-ui:message key="none" /></span>
		</c:otherwise>
	</c:choose>
</div>

<div class="button-row">
	<aui:button cssClass="mr-2" name="selectAssetListButton" value="select" />

	<aui:button name="clearAssetListButton" value="clear" />
</div>

<aui:script sandbox="<%= true %>">
	var assetListEntryId = document.getElementById(
		'<portlet:namespace />assetListEntryId'
	);
	var infoListProviderKey = document.getElementById(
		'<portlet:namespace />infoListProviderKey'
	);
	var title = document.getElementById('<portlet:namespace />title');

	var selectAssetListButton = document.getElementById(
		'<portlet:namespace />selectAssetListButton'
	);

	if (selectAssetListButton) {
		selectAssetListButton.addEventListener('click', () => {
			Liferay.Util.openSelectionModal({
				onSelect: function (selectedItem) {
					if (selectedItem) {
						if (
							selectedItem.returnType ===
							'<%= InfoListItemSelectorReturnType.class.getName() %>'
						) {
							var itemValue = JSON.parse(selectedItem.value);

							assetListEntryId.value = itemValue.classPK;

							title.innerHTML = itemValue.title;

							infoListProviderKey.value = '';
						}
						else if (
							selectedItem.returnType ===
							'<%= InfoListProviderItemSelectorReturnType.class.getName() %>'
						) {
							var itemValue = JSON.parse(selectedItem.value);

							title.innerHTML = itemValue.title;

							infoListProviderKey.value = itemValue.key;

							assetListEntryId.value = '';
						}
					}
				},
				selectEventName:
					'<%= assetPublisherDisplayContext.getSelectAssetListEventName() %>',
				title: '<liferay-ui:message key="select-collection" />',
				url:
					'<%= assetPublisherDisplayContext.getAssetListSelectorURL() %>',
			});
		});
	}

	var clearAssetListButton = document.getElementById(
		'<portlet:namespace />clearAssetListButton'
	);

	if (clearAssetListButton) {
		clearAssetListButton.addEventListener('click', (event) => {
			title.innerHTML = '<liferay-ui:message key="none" />';

			assetListEntryId.value = '';
			infoListProviderKey.value = '';
		});
	}
</aui:script>