var report = {
    appEnvironment: null,
    init: function(appEnvironment) {
        report.appEnvironment = appEnvironment;
        report.bindButtons();
        global.maskInput();
    },
    bindButtons: function() {
        jQ('.execute-report').unbind('click.execute').bind('click.execute', report.execute);
    },
    execute: function() {
        if(report.validate_fields()){
            var params_string = '';
            if(jQ(this).data('paramsids') !== '') {
                var paramsIds = jQ(this).data('paramsids').split('|');
                for(var i=0;i<paramsIds.length;i++) {
                    if((jQ('#'+paramsIds[i]).attr('type') === 'checkbox' && jQ('#'+paramsIds[i]).prop('checked')) || jQ('#'+paramsIds[i]).attr('type') !== 'checkbox') {
                        let val = jQ('#'+paramsIds[i]).val();
                        if(Array.isArray(val)) val = val.join(',');
                        if(val === null) val = '';
                        params_string += '/'+(val !== '' ? val.replace(/\//g, '-') : 'null');
                    }
                }
            }
            window.open((report.appEnvironment === 'dev' ? '/app_dev.php/' : '/')+jQ(this).data('url')+params_string, '_blank');
        }
    },
    validate_fields: function() {
        var errors = '';
        jQ('.required').each(function(){
            if(jQ(this).val() === ''){
                errors += jQ(this).data('ref-label') + ' should not be blank.' + '\n';
            }
        });
        if(errors === '') {
            return true;
        } else {
            alert(errors);
            return false;
        }
    }
};
