(function($){
	$.fn.getSupportTickets = function() {
		var element = $(this);
		$.ajax({
			type: "post",
			url: "contents/plugins/messaging/ajax.php",
			data: {"action": "get_support_tickets"}
		}).done(function(data) {
			try {
				data = JSON.parse(data);
				$(element).html(data);
				$("#support-tickets table").DataTable({
			        lengthMenu: [[-1, 10, 50, 75, 100], ["All", 10, 50, 75, 100]],
			        iDisplayLength: 10
			    });
			}
			catch(err) {
				console.log(err);
				console.log(data);
			}
			finally{}
		}).fail(function(err) {

		})
	}
})(jQuery);

jQuery(document).ready(function($){

	$(document).on("change blur keyup", "#selector", function(){
		
		var selector = $(this).val();
		if(selector.length > 5) {
			$("#display-name-verification").html("Searching for User")
				.addClass("alert-default pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
				.removeClass("alert-success");
			$.ajax({
				type: "post",
				url: "contents/plugins/messaging/ajax.php",
				data: {"action": "new_ticket_get_user", "selector": selector}
			}).done(function(data){
				try {
					data = JSON.parse(data);
					if(data) {
						$("#display-name-verification").html("User Found: "+data.surname+" "+data.other_names)
							.addClass("alert-success pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
							.removeClass("alert-warning");
						$("#user_id").val(data.id);
						
					} else {
						$("#display-name-verification").html("User Not Found")
							.addClass("alert-warning pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
							.removeClass("alert-success");
						
						$("#user_id").val("");
					}
				}
				catch(err) {
					console.log(err);
					$("#display-name-verification").html("User Not Found")
						.addClass("alert-warning pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
						.removeClass("alert-success");
					
					$("#user_id").val("");
				}
				finally{

				}
			}).fail(function(){
				$("#display-name-verification").html("User Not Found")
					.addClass("alert-warning pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
					.removeClass("alert-success");
				
				$("#user_id").val("");
			})
		} else {
			$("#display-name-verification").html("")
				.removeClass("alert-success alert-warning pt-2 pb-2 pl-2 pr-2 mt-2 mb-1")
				
		}
		
	});


	$("#all-support-tickets").getSupportTickets();
})