$(document).ready(function() {
    $(".brand-minimizer").click(function() {
        if ($("body").hasClass("sidebar-minimized")) {
            console.log('sidebar-minimized')
            $('.navbar-brand').show();
        }
        else {
            $('.navbar-brand').hide();
        }
    });
    
    $(".nav-link.subitem.nav-dropdown-toggle").click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        let parent_element = $(this).parent('.nav-item.nav-dropdown');
        parent_element.toggleClass( "open" );
    });
});

$('body').tooltip({selector: '[data-toggle="tooltip"]'});
//$('[data-toggle="tooltip"]').tooltip();

var now;

var exceptionUrls = [
    base_url+'sistema/session/get_last_activity',
    base_url+'auth/consulta_captcha'
];


paceOptions = {
    startOnPageLoad: false
}

if($('.sidebar').length == 0){
    $('.main,.app-footer').css('margin-left', '0px');
    $('.navbar-toggler').hide();
} 

$(document).ajaxSend(function( event, jqxhr, settings ) 
{
    if ( jQuery.inArray( settings.url, exceptionUrls ) === -1 )
    {
        Pace.restart();
    }
});

$(document).ready(function(){
	
	var loader = $('.box').find('.overlay');
    
    if(loader.length){
      loader.remove();
    }
	
	var date_expire = null;

	$.fn.atualiza_clock_logout = function(date_expire)
	{	
		// Request
		var data = {
			post: 'ok'
		};
		
		var deferredDateExpire = $.Deferred();
		
		if(date_expire == undefined)
		{
			$.ajax({
				url: base_url + 'sistema/session/get_last_activity',
				dataType: 'json',
				type: 'POST',
				data: data,
				success: function(result)
				{				
					if (result.date_expire)
					{
						date_expire = result.date_expire;
						deferredDateExpire.resolve();
						now = result.now;
					}
					else
					{
						$(this).atualiza_clock_logout();
					}
				}
			});
		}
		else
		{
			deferredDateExpire.resolve();
		}
		
		deferredDateExpire.done(function(){
			$('#clock').countdown(date_expire)
			.on('update.countdown', function(event) {
				if(event.offset.hours == 0)
				{
					var format = '%M:%S';
				}
				else
				{
					var format = '%H:%M:%S';
				}
				$(this).html(event.strftime(format));
			})
			.on('finish.countdown', function(event) {
				$(this).html('encerrado');
				var $Cthis = $(this);
				setInterval(function(){$Cthis.append('.')},1000);		
				setTimeout(function(){
					   location.reload();
				},2000);
				
			});
		})
	}

	$(this).atualiza_clock_logout();
	
});