$(document).ready(function() {

	$('.text-truncate__multiline').each(function(){

		var thisElementId = $(this).attr('id');
		var thisElement = document.getElementById(thisElementId);
		var contentOverflowsContainer = thisElement.scrollHeight > thisElement.clientHeight || thisElement.scrollWidth > thisElement.clientWidth;

		if (contentOverflowsContainer) {
			var visibilityToggleId = $(this).attr('data-visibility-toggle-id');
			$('#' + visibilityToggleId).removeClass('d-none');
		}

	});

	$('.charity-register-charity-details .visibility-toggle').click(function() {

		var contentId = $(this).attr('data-content-id');
		var content = $('#' + contentId);

		if (content.hasClass('text-truncate__multiline')) {

			$(this).text('Less');
			$(this).attr("aria-expanded", "true");

		} else {

			$(this).text('More');
			$(this).attr("aria-expanded", "false");

		}

		content.toggleClass('text-truncate__multiline');

	});

	$('.ccew__details-expand').on('click', function(event){

		var target = $(event.target);

		if (target.hasClass('opened')) {
			target.text('More');
		} else {
			target.text('Less');
		}

		target.toggleClass('opened');

		let table = target.closest('table');

		$(table).find('.govuk-table__row:nth-child(n+11)').toggleClass('hide')
	});

});

function addUserFeedbackEventListener(portletId, resourceCommandName, nameSpace){
	$('#portlet_' + portletId + ' .ccew-user-feedback-button').on('click', function(){
		saveUserFeedback(portletId, resourceCommandName, this.getAttribute('ccew-data-feedback'), nameSpace)
	});
}

function saveUserFeedback(portletId, resourceCommandName, feedbackValue, nameSpace){

		
	AUI().use('aui-io-request','liferay-portlet-url', function(A){
		let url = Liferay.PortletURL.createResourceURL();
		let currentURL = Liferay.currentURL;
		url.setParameter("feedbackValue", feedbackValue);
		url.setParameter("currentURL", currentURL);
	
		url.setPortletId(portletId);
		url.setResourceId(resourceCommandName);
	
		A.io.request(url.toString(), {
			method: 'post',
			on: {
					success: function() {
						showMessage();
				}
			}
		});
	});
}

function showMessage(){
	$('.ccew-user-feedback-icons-wrapper').hide();

	$('.ccew-user-feedback-article').removeClass('hide');
}
