Error calling form with JS

Asked

Viewed 27 times

1

Explaining the code below.

I have a meta(value), if the user registers a goal below that indicated in the system, automatically the system opens a form ('#add-modal-action-plan-form')

Error: If the user registers a value at the date of the current month (ex: 05/09/2018) the modal opens automatically, but if he has to register a value of one month that has already passed (ex:10/01/2018) the modal does not open.

var url = window.location.href;
var arr = url.split("/");
var host = arr[0] + "//" + arr[2];

// Checa se existe um plano de acao
function checkPlan(name, city, year, callback) {

    $.getJSON(host + '/apps/indicadores/checar/indicador/plano-de-acao?name=' + name + '&city=' + city + '&year=' + year, function (err, result) {
        if (err) {
            return callback(err);
        }
        return callback(null, result);
    });

}

//Checa se o indicador está fora da meta ou dentro da meta
function checkIndicadorMeta(name, city, month, year, role, callback) {
    
    $.getJSON(host + '/apps/indicadores/checar/indicador/meta?name=' + name + '&city=' + city + '&month=' + month + '&year=' + year, function (result) {

        var verify = false;
        if(result.length > 0){
            var indicador = result[0];
       
            switch (role) {

                case 1:
                    if(indicador.ind_meta > indicador.din_valor){
                      verify = true;  
                    }
                    
                    break;
                case 2:
                    if (indicador.ind_meta < indicador.din_valor) {
                        verify = true;
                    }
                    break;
                case 3:
                    if ((indicador.ind_meta > indicador.din_valor)||(indicador.ind_meta_final < indicador.din_valor)) {
                        verify = true;
                    }
                    break;
                default:
                    verify = false;
                    break;
            }
        }
    
        callback(verify);
    });


}

function convertIdToName(id){
    var separator = id.split("#");
    var arr = separator[1].split("-"); 
    var name = "";
    for (var i = 0; i < arr.length - 1; i++){
        name = name + arr[i] + "_";
    }
    name = name + arr[arr.length - 1];

    return name;
}

function convertNameToId(name) {
    var separator = name.split("_");
    //var arr = separator[1].split("-");
    var id = "";
    for (var i = 0; i < separator.length - 1; i++) {
        id = id + separator[i] + "-";
    }
    id = id + separator[separator.length - 1];

    return id;
}

function abrirPlano(id, month, year) {
    window.location.href = '/apps/indicadores/visualizar/indicador/plano-de-acao?id=' + id + '&month=' + month + '&year=' + year;
}

function showModals(indicadorID, name, city, month, year, role){
    checkPlan(name, city, year, function (result) {
        //Exemplo de operacao
        //mes a definir em indicadorForaDaMeta
        checkIndicadorMeta(name, city, month, year, role, function (indicadorForaDaMeta) {
            var mesAtual = new Date().getMonth();
            if (result.length == 0) {
                var mesDoUltimoPlano = mesAtual - 1;
            } else {
                var mesDoUltimoPlano = new Date(result[result.length - 1].pla_data).getMonth();
            }
            console.log(result);
            if (indicadorForaDaMeta && !(mesAtual == mesDoUltimoPlano)) {
                $('#add-modal-plano-de-acao-form').find('[name="id"]').val(indicadorID);
                $('#add-modal-plano-de-acao').modal('show');
            } else {
                for (let i = 0; i < result.length; i++) {
                    if (result[i].pla_status) {
                        var month = new Date(result[i].pla_data).getMonth() + 1;
                        var year = new Date(result[i].pla_data).getFullYear();
                        $('#info-modal-plano-de-acao-form').find('[name="id"]').val(result[i].pla_id);
                        $('#info-modal-plano-de-acao-form').find('[name="month"]').val(month);
                        $('#info-modal-plano-de-acao-form').find('[name="year"]').val(year);
                        $('#info-modal-plano-de-acao').modal('show');
                        
                        console.log("#" + convertNameToId(name) + "-botao-plano");
                        
                        $('#' + convertNameToId(name) + '-botao-plano').removeAttr('href');
                        $('#' + convertNameToId(name) + '-botao-plano').attr('href', '/apps/indicadores/visualizar/indicador/plano-de-acao?id=' + result[i].pla_id + '&month=' + month + '&year=' + year);
                      
                        i = result.length; 

                    }
                }
            }
        });

    });
}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.