$('.search > input').on('keyup', function() {
  var rex = new RegExp($(this).val(), 'i');
    $('.people .person').hide();
    $('.people .person').filter(function() {
        return rex.test($(this).text());
    }).show();
});

$('.user-list-box .person').on('click', function(event) {
    if ($(this).hasClass('.active')) {
        return false;
    } else {
		
        var findChat = $(this).attr('data-chat');
		
		var split_id = findChat.split("_");
		var postid = split_id[1];
		
        var personName = $(this).find('.user-name').text();
        var personImage = $(this).find('img').attr('src');
        var hideTheNonSelectedContent = $(this).parents('.chat-system').find('.chat-box .chat-not-selected').hide();
        var showChatInnerContent = $(this).parents('.chat-system').find('.chat-box .chat-box-inner').show();

        if (window.innerWidth <= 767) {
          $('.chat-box .current-chat-user-name .name').html(personName.split(' ')[0]);
        } else if (window.innerWidth > 767) {
          $('.chat-box .current-chat-user-name .name').html(personName);
        }
        $('.chat-box .current-chat-user-name img').attr('src', personImage);
        $('.chat').removeClass('active-chat');
        $('.user-list-box .person').removeClass('active');
        $('.chat-box .chat-box-inner').css('height', '100%');
        $(this).addClass('active');
		$('#chatmsgs').html('');
        $('.chat').addClass('active-chat'); //chat code here
		$("#msgowner").val(postid);
		
		//working chat here
			
		
		setInterval(function(){
			var chkinp = $("#msgowner").val();
			//$('#chatmsgs').html(chkinp);
			update_chat_history_data(chkinp);
		}, 5000);
		
		function update_chat_history_data(towho)
		{
			$.ajax({
			url:"../account/actions/chatpost.php",
			method:"POST",
			data:{'updt_mess': 1,'msgowner':towho},
			success:function(data){
				$('#chatmsgs').append(data);
				if(data != '')
				{
				const getScrollContainer = document.querySelector('.chat-conversation-box');
				getScrollContainer.scrollTop = getScrollContainer.scrollHeight;
				}
				return;
			}
			})
		}
		
    }
    if ($(this).parents('.user-list-box').hasClass('user-list-box-show')) {
      $(this).parents('.user-list-box').removeClass('user-list-box-show');
    }
    $('.chat-meta-user').addClass('chat-active');
    $('.chat-box').css('height', 'calc(100vh - 266px)');
    $('.chat-footer').addClass('chat-active');

  const ps = new PerfectScrollbar('.chat-conversation-box', {
    suppressScrollX : true
  });

	
  
});

const ps = new PerfectScrollbar('.people', {	suppressScrollX : true	});



$('.msgbox').on('keydown', function(event) {

    if(event.key === 'Enter') {
        var chatInput = $(this);
        var chatMessageValue = chatInput.val();
        if (chatMessageValue === '') { return; }
		
		// submit message here
		
		
		var data = $("#msgboxform").serialize();
		$.ajax({
            url: '../account/actions/chatpost.php',
            type: 'POST',
            data: data,
			beforeSend: function()
			{	
				$("#button_combtn").html('posting <span class="spinner-grow-sm text-white ml-2 align-self-center loader-sm"></span>');
				$("#button_combtn").attr('disabled', 'disabled');
			},
			dataType:"json",
            success: function(response)
			{
				if(response.prpt == "grant")
				{
					$messageHtml = '<div class="bubble me">' + chatMessageValue + '</div>';
					var appendMessage = $('.msgbox').parents('.chat-system').find('.active-chat').append($messageHtml);
					const getScrollContainer = document.querySelector('.chat-conversation-box');
					getScrollContainer.scrollTop = getScrollContainer.scrollHeight;
					var clearChatInput = chatInput.val('');
					
					return;
				}
				else
				{
					return;
				}
				
            }
        });
		
		
		
        
    }
})

$('.hamburger, .chat-system .chat-box .chat-not-selected p').on('click', function(event) {
  $(this).parents('.chat-system').find('.user-list-box').toggleClass('user-list-box-show')
})