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

<%
long classNameId = GetterUtil.getLong((String)request.getAttribute("liferay-ddm:template-selector:classNameId"));
DDMTemplate portletDisplayDDMTemplate = (DDMTemplate)request.getAttribute("liferay-ddm:template-selector:portletDisplayDDMTemplate");
ResourceBundle resourceBundle = (ResourceBundle)request.getAttribute("liferay-ddm:template-selector:resourceBundle");

long ddmTemplateGroupId = PortletDisplayTemplateUtil.getDDMTemplateGroupId(themeDisplay.getScopeGroupId());

Group ddmTemplateGroup = GroupLocalServiceUtil.getGroup(ddmTemplateGroupId);
%>

<aui:input id="displayStyleGroupId" name="preferences--displayStyleGroupId--" type="hidden" value="<%= String.valueOf(displayStyleGroupId) %>" />

<aui:select id="displayStyle" inlineField="<%= true %>" label="<%= HtmlUtil.escape(label) %>" name="preferences--displayStyle--">
	<c:if test="<%= showEmptyOption %>">
		<aui:option label="default" selected="<%= Validator.isNull(displayStyle) %>" />
	</c:if>

	<c:if test="<%= (displayStyles != null) && !displayStyles.isEmpty() %>">
		<optgroup label="<liferay-ui:message key="default" />">

			<%
			for (String curDisplayStyle : displayStyles) {
			%>

				<aui:option label="<%= HtmlUtil.escape(curDisplayStyle) %>" selected="<%= displayStyle.equals(curDisplayStyle) %>" />

			<%
			}
			%>

		</optgroup>
	</c:if>

	<%
	for (com.liferay.dynamic.data.mapping.model.DDMTemplate curDDMTemplate : DDMTemplateLocalServiceUtil.getTemplates(PortalUtil.getCurrentAndAncestorSiteGroupIds(ddmTemplateGroupId), classNameId, 0L)) {
		if (!DDMTemplatePermission.contains(permissionChecker, scopeGroupId, curDDMTemplate.getTemplateId(), PortletKeys.PORTLET_DISPLAY_TEMPLATE, ActionKeys.VIEW) || !DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY.equals(curDDMTemplate.getType())) {
			continue;
		}

		Map<String, Object> data = new HashMap<String, Object>();

		data.put("displaystylegroupid", curDDMTemplate.getGroupId());
	%>

		<aui:option data="<%= data %>" label="<%= HtmlUtil.escape(curDDMTemplate.getName(locale)) %>" selected="<%= (portletDisplayDDMTemplate != null) && (curDDMTemplate.getTemplateId() == portletDisplayDDMTemplate.getTemplateId()) %>" value="<%= PortletDisplayTemplate.DISPLAY_STYLE_PREFIX + HtmlUtil.escape(curDDMTemplate.getTemplateKey()) %>" />

	<%
	}
	%>

</aui:select>

<c:if test="<%= !ddmTemplateGroup.isLayoutPrototype() %>">
	<liferay-ui:icon
		iconCssClass="<%= HtmlUtil.escape(icon) %>"
		id="selectDDMTemplate"
		label="<%= true %>"
		message='<%= LanguageUtil.format(resourceBundle, "manage-display-templates-for-x", HtmlUtil.escape(ddmTemplateGroup.getDescriptiveName(locale)), false) %>'
		url="javascript:;"
	/>
</c:if>

<liferay-portlet:renderURL plid="<%= themeDisplay.getPlid() %>" portletName="<%= PortletProviderUtil.getPortletId(DDMTemplate.class.getName(), PortletProvider.Action.VIEW) %>" var="basePortletURL">
	<portlet:param name="showHeader" value="<%= Boolean.FALSE.toString() %>" />
</liferay-portlet:renderURL>

<aui:script sandbox="<%= true %>">
	$('#<portlet:namespace />selectDDMTemplate').on(
		'click',
		function(event) {
			Liferay.Util.openDDMPortlet(
				{
					basePortletURL: '<%= basePortletURL %>',
					classNameId: '<%= classNameId %>',
					dialog: {
						width: 1024
					},
					eventName: '<portlet:namespace />saveTemplate',
					groupId: <%= ddmTemplateGroupId %>,
					mvcPath: '/view_template.jsp',
					navigationStartsOn: '<%= DDMNavigationHelper.VIEW_TEMPLATES %>',
					refererPortletName: '<%= PortletKeys.PORTLET_DISPLAY_TEMPLATE %>',
					refreshOpenerOnClose: true,
					title: '<%= UnicodeLanguageUtil.get(request, "application-display-templates") %>'
				},
				function(event) {
					if (!event.newVal) {
						submitForm(document.<portlet:namespace />fm, '<%= HtmlUtil.escapeJS(refreshURL) %>');
					}
				}
			);
		}
	);

	var displayStyleGroupIdInput = $('#<portlet:namespace />displayStyleGroupId');

	var displayStyle = $('#<portlet:namespace />displayStyle');

	displayStyle.on(
		'change',
		function(event) {
			var displayStyleGroupId = displayStyle.find(':selected').data('displaystylegroupid');

			if (displayStyleGroupId) {
				displayStyleGroupIdInput.val(displayStyleGroupId);
			}
		}
	);
</aui:script>