function get_directory_results(search_value) {
	$.ajax({
		type: "POST",
		url: '/_resources/directory/directory-ajax.php',
		dataType: 'json',
		data: {search: search_value},
		success: function(response) {

			// Check if empty etc
			if (!$.trim(response)) {
				$('#directory_results').append('<div class="well" style="padding: 2rem;">No results. Try another search.</div>');
				$('#pcc-loading-animation').hide();
			} else {
				$.each(response, function(i, item) {
					var first_name = item['FIRST_NAME'];
					var last_name = item['LAST_NAME'];
					var middle_name = item['MIDDLE_NAME'];
					var pidm = item['PIDM'];
					var udc_id = item['UDC_ID'];
					var phone = item['PHONE_EXTENSION'];
					var email = item['EMAIL'];
					var org = item['ORG_Num'];
					var location = item['BANNER_WORK_LOCATION'];
					var classification = item['CLASSIFICATION'];
					var classification_desc = item['CLASSIFICATION_DESC'];
					var office_location = item['OFFICE_LOCATION'];
					var org_num = item['ORG_Num'];
					var org_title = item['ORG_TITLE'];
					var title = item['JOBTITLE'];

					$('#directory_results').append('<div class="well" style="padding: 2rem;"><ul><li><strong>First Name: ' + first_name + '</strong></li><li><strong>Last Name: ' + last_name + '</strong></li><li>Middle Name: ' + middle_name + '</li><li>Job Title: ' + title + '</li><li>Email: ' + email + '</li><li>Phone: ' + phone + '</li><li>Banner Work Location: ' + location + '</li><li>Classification: ' + classification + '</li><li>Classification Description: ' + classification_desc + '</li><li>Office Location: ' + office_location + '</li><li>Organization Number: ' + org_num + '</li><li>Organization Title: ' + org_title + '</li><li>PIDM: ' + pidm + '</li><li>UDC ID: ' + udc_id + '</li></ul></div>');
				});
			}
			$('#pcc-loading-animation').hide();
		}
	});
}
// If Query Parameter Exists
$(document).ready(function() {
	var url_params = new URLSearchParams(window.location.search);
	var search = url_params.get('search');
	
	if(search) {
		var search_value = search;
		$('#pcc-loading-animation').show();
		get_directory_results(search_value);
	}

});

// Form Submission Get Results
$(document).ready(function() {
	$('#pcc-loading-animation').hide();
	
	$('#directoryform').submit(function(e) {
		e.preventDefault();
		$('#directory_results').html('');
		$('#pcc-loading-animation').show();
		// The value in the input
		var search_value = $('#directoryform #directory_search').val();
		get_directory_results(search_value);
	});
});

// On page load show loading icon, then hide once loaded
if($('#directory_real_time_form').length) {
	$('#pcc-loading-animation').show();
	$('.pcc-directory-wrapper').addClass('pcc-directory-ready');
	$('#directory_real_time_form input').val('');
}
if($('.pcc-directory').length) {
	$('#pcc-loading-animation').hide();
	$('#directory_real_time_form input').prop('disabled', false);
	$('#directory_real_time_form input').attr('placeholder','Start typing to search...');
}

// Real-Time Search Function
// Searching the data-meta attribute
function text_filter_search(input, target) {
	var value = $(input).val().toLowerCase();
	
	$(target).filter(function() {
		//$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
		// Using the data-meta attribute
		$(this).toggle($(this).attr('data-meta').toLowerCase().indexOf(value) > -1)
	});
}

$(document).ready(function() {
	// Search
	$('#directory_real_time_search').on('keyup', function() {
		var input = '#directory_real_time_search';
		var target = '.directory-main .card';
		text_filter_search(input, target);
		// Show the results after entering 2 characters
		if(this.value.length >= '2') {
			$('.pcc-directory.directory-main').addClass('pcc-directory-visible');
		} else {
			$('.pcc-directory.directory-main').removeClass('pcc-directory-visible');
		}
	});
});

// Copy to Clipboard
$('.directory-copy-button').click(function() {
	var copy_text = $(this).attr('data-copy');
	navigator.clipboard.writeText(copy_text).then(
		function() {
		  /* clipboard successfully set */
		  window.alert('Success! The text was copied to your clipboard') 
		}, 
		function() {
		  /* clipboard write failed */
		  window.alert('Opps! Your browser does not support the Clipboard API')
		}
	  )
});

// Collapse for Staff Listing
$(document).ready(function() {
	$('.pcc-directory .pcc-directory-heading').click(function() {
		$(this).closest('.pcc-directory-card').toggleClass('directory-item-active');
	});
});