<script type="text/javascript">
    $.ajaxSetup({
        headers: {
            'Accept': 'application/json',
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

            
    $(document).ajaxError(function(e, xhr, settings) {
        
        if(!(xhr && xhr.readyState === 4)) {
            return;
        }

        var response = false;
        var message = '@lang("Došlo je do greške u komunikaciji sa serverom!")';
        
        if(!xhr.responseText) {
            @if(env('APP_DEBUG'))
                let errId = Math.floor(Math.random() * Math.floor(1000000));
                console.log(errId + ": Greška. 'xhr' objekat je neispravno formiran.\
                                    'responseText' nije setovan. Error trace:");
                console.log(e);
                console.log(errId + ": 'xhr' objekat: ");
                console.log(xhr);
                console.log(errId + ': End error trace.');
            @endif
        }
        else {
            try {
                response = JSON.parse(xhr.responseText);
            } catch(err) {
                @if(env('APP_DEBUG'))
                    let errId = Math.floor(Math.random() * Math.floor(1000000));
                    console.log(errId + ': Greška prilikom poziva JS funkcije \
                                        JSON.parse(). Error trace:');
                    console.log(err);
                    console.log(errId + ":'xhr' objekat: ");
                    console.log(xhr);
                    console.log(errId + ': End error trace.')
                @endif
            }
        }

        console.log(response);
        
        if (response && response['message']) {
            message = response['message'];
        }

        @if(env('APP_DEBUG'))
            showSystemMessage(message, 'error');
        @else 
            if(xhr && xhr.status !== 422)  {
                showSystemMessage(message, 'error');
            }
        @endif
    });
    
    $(document).ajaxSuccess(function(e, xhr, settings) {
//        DEBUG
//        if(!(xhr && xhr.readyState === 4)) {
//            return;
//        }

        var response = false;
        var message = '';
        
        if(xhr.responseText) {
            try {
                response = JSON.parse(xhr.responseText);
            } catch(err) {
                let errId = Math.floor(Math.random() * Math.floor(1000000));
                console.log(errId + ': Greška prilikom poziva JS funkcije \
                                     JSON.parse(). Error trace:');
                console.log(err);
                console.log(errId + ":'xhr' objekat: ");
                console.log(xhr);
                console.log(errId + ': End error trace.')
            }
        }
        
        if (response && response['message']) {
            message = response['message'];
        }
        
        if(message){
            showSystemMessage(message, 'success');
        }
    });
</script>