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

<%
long organizationId = ParamUtil.getLong(request, "organizationId");

Organization organization = OrganizationServiceUtil.fetchOrganization(organizationId);

String[] organizationsTypes = OrganizationLocalServiceUtil.getTypes();

String type = BeanParamUtil.getString(organization, request, "type", organizationsTypes[0]);

long regionId = BeanParamUtil.getLong(organization, request, "regionId");
long countryId = BeanParamUtil.getLong(organization, request, "countryId");

long groupId = 0;

if (organization != null) {
	groupId = organization.getGroupId();
}
%>

<aui:model-context bean="<%= organization %>" model="<%= Organization.class %>" />

<clay:row>
	<clay:col
		md="7"
	>
		<liferay-ui:error exception="<%= DuplicateOrganizationException.class %>" message="the-organization-name-is-already-taken" />

		<liferay-ui:error exception="<%= OrganizationNameException.class %>">
			<liferay-ui:message arguments="<%= new String[] {OrganizationConstants.NAME_LABEL, OrganizationConstants.NAME_GENERAL_RESTRICTIONS, OrganizationConstants.NAME_RESERVED_WORDS} %>" key="the-x-cannot-be-x-or-a-reserved-word-such-as-x" />
		</liferay-ui:error>

		<aui:input autoFocus="<%= windowState.equals(WindowState.MAXIMIZED) %>" name="name" />

		<c:choose>
			<c:when test="<%= PropsValues.FIELD_ENABLE_COM_LIFERAY_PORTAL_KERNEL_MODEL_ORGANIZATION_STATUS %>">
				<liferay-ui:error key="<%= NoSuchListTypeException.class.getName() + Organization.class.getName() + ListTypeConstants.ORGANIZATION_STATUS %>" message="please-select-a-type" />

				<aui:select label="status" listType="<%= ListTypeConstants.ORGANIZATION_STATUS %>" listTypeFieldName="statusId" name="statusId" showEmptyOption="<%= true %>" />
			</c:when>
			<c:otherwise>
				<aui:input name="statusId" type="hidden" value="<%= (organization != null) ? organization.getStatusId() : ListTypeConstants.ORGANIZATION_STATUS_DEFAULT %>" />
			</c:otherwise>
		</c:choose>

		<c:choose>
			<c:when test="<%= (organization == null) && (organizationsTypes.length > 1) %>">
				<aui:select name="type">

					<%
					for (String curType : organizationsTypes) {
					%>

						<aui:option label="<%= curType %>" selected="<%= type.equals(curType) %>" />

					<%
					}
					%>

				</aui:select>
			</c:when>
			<c:when test="<%= organization == null %>">
				<aui:input name="type" type="hidden" value="<%= organizationsTypes[0] %>" />
			</c:when>
			<c:otherwise>
				<aui:input name="typeLabel" type="resource" value="<%= LanguageUtil.get(request, organization.getType()) %>" />

				<aui:input name="type" type="hidden" value="<%= organization.getType() %>" />
			</c:otherwise>
		</c:choose>

		<liferay-ui:error exception="<%= NoSuchCountryException.class %>" message="please-select-a-country" />

		<div class="<%= OrganizationLocalServiceUtil.isCountryEnabled(type) ? StringPool.BLANK : "hide" %>" id="<portlet:namespace />countryDiv">
			<aui:select label="country" name="countryId" />

			<aui:select label="region" name="regionId" />
		</div>
	</clay:col>

	<clay:col
		md="5"
	>
		<div align="middle">
			<c:choose>
				<c:when test="<%= organization != null %>">
					<label class="control-label"></label>

					<liferay-ui:logo-selector
						currentLogoURL="<%= organization.getLogoURL() %>"
						defaultLogo="<%= organization.getLogoId() == 0 %>"
						defaultLogoURL='<%= themeDisplay.getPathImage() + "/organization_logo?img_id=0" %>'
						logoDisplaySelector=".organization-logo"
						tempImageFileName="<%= String.valueOf(groupId) %>"
					/>
				</c:when>
				<c:otherwise>
					<liferay-ui:logo-selector
						currentLogoURL='<%= themeDisplay.getPathImage() + "/organization_logo?img_id=0" %>'
						defaultLogo="<%= true %>"
						defaultLogoURL='<%= themeDisplay.getPathImage() + "/organization_logo?img_id=0" %>'
						logoDisplaySelector=".organization-logo"
						tempImageFileName="0"
					/>
				</c:otherwise>
			</c:choose>
		</div>
	</clay:col>
</clay:row>

<liferay-frontend:component
	componentId="CountryRegionDynamicSelect"
	context='<%=
		HashMapBuilder.<String, Object>put(
			"countrySelect", portletDisplay.getNamespace() + "countryId"
		).put(
			"countrySelectVal", countryId
		).put(
			"regionSelect", portletDisplay.getNamespace() + "regionId"
		).put(
			"regionSelectVal", regionId
		).build()
	%>'
	module="js/CountryRegionDynamicSelect"
/>

<c:if test="<%= organization == null %>">
	<aui:script sandbox="<%= true %>">
		var typeSelect = document.getElementById('<portlet:namespace />type');

		if (typeSelect) {
			typeSelect.addEventListener('change', (event) => {
				var countryDiv = document.getElementById(
					'<portlet:namespace />countryDiv'
				);

				if (countryDiv) {

					<%
					for (String curType : organizationsTypes) {
					%>

						if (event.currentTarget.value === '<%= curType %>') {
							if (
								!<%= OrganizationLocalServiceUtil.isCountryEnabled(curType) %>
							) {
								countryDiv.classList.add('hide');
							}
							else {
								countryDiv.classList.remove('hide');
							}
						}

					<%
					}
					%>

				}
			});
		}
	</aui:script>
</c:if>