$(document).ready(function () {

    $('.toast').toast({autohide:false});
    var cookie_life = new Date();
    cookie_life.setTime(cookie_life.getTime() + (20 * 60 * 1000));
//    $.cookie('adm_msg_timestamp', 2, { expires: cookie_life });
//    console.log($.cookie('adm_msg_timestamp'));
    check_cookies();
    setInterval(() => check_cookies(), 5000);

    function check_cookies()
    {
        var url = '/adm_monitoring'
        if ($.cookie('adm_msg_timestamp'))
            url = '/adm_monitoring?timestamp='+$.cookie('adm_msg_timestamp')
        $.ajax({
            type:'GET',
            url: url,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                for (msg_num in data['result'])
                {
                    if (data['result'][msg_num]['msg_timestamp'] > $.cookie('adm_msg_timestamp')){
                        $('#msg_toast').toast('show');
                        $('#toast_message').append('<span style="display: block; height: 50px;">'+data['result'][msg_num]['msg']+'</span>')
                    }
                }
                $.cookie('adm_msg_timestamp', data['timestamp'], { expires: cookie_life });

            },
            error:function (jqXHR, exception) {
                after_error();
            }
        });
    }

    $('.toast').on('show.bs.toast', function () {
        this.hidden = false;
        }).on('hidden.bs.toast', function () {
            this.hidden = true;
    });

    $('.close_toast').on('click', function () {
        this.hidden = false;
        $('#toast_message').empty()
    });



});
