﻿$(document).ready(function () {

    // Categorias
    $('#sltCategorias').change(function () {
        var codCategoria = +$('#sltCategorias').val();
        if (codCategoria > 0) {
            //chamada ajax da função
            UI_Index_Cliente.GetSubCategorias(codCategoria, function (res) {
                if (!res.error) {
                    RenderFields.select.render('sltSubCategoria', res.value, 'CodCategoria', 'Nome', true, null);
                } else {
                    alert(res.error.message);
                }
            });
        }
    });

    // SubCategorias
    $('#sltSubCategoria').change(function () {
        var codSubCategorias = +$('#sltSubCategoria').val();
        if (codSubCategorias > 0) {

            //chamada ajax da função
            var codCategoria = +$('#sltCategorias').val() > 0 ? +$('#sltCategorias').val() : null;
            codSubCategorias = +$('#sltSubCategoria').val() > 0 ? +$('#sltSubCategoria').val() : null;

            UI_Index_Cliente.GetEstados(codCategoria, codSubCategorias, function (res) {
                if (!res.error) {
                    RenderFields.select.render('sltEstado', res.value, 'CodEstado', 'Nome', true, null);
                    RenderFields.select.clear('sltCidade', true);
                } else {
                    alert(res.error.message);
                }
            });
        }
    });

    // Estados
    $('#sltEstado').change(function () {
        var codEstado = +$('#sltEstado').val();
        if (codEstado > 0) {
            //chamada ajax da função

            var codCategoria = +$('#sltCategorias').val() > 0 ? +$('#sltCategorias').val() : null;
            var codSubCategorias = +$('#sltSubCategoria').val() > 0 ? +$('#sltSubCategoria').val() : null;


            UI_Index_Cliente.GetCidades(codEstado, codCategoria, codSubCategorias, function (res) {
                if (!res.error) {
                    RenderFields.select.render('sltCidade', res.value, 'CodCidade', 'Nome', true, null);
                } else {
                    alert(res.error.message);
                }
            });
        }
    });

    // Botão Buscar Event
    $('#btnBuscarCredenciados').click(function () {
        
        var count = 0;
        if(+$('#sltCategorias').val() > 0)
            count++;
        if(+$('#sltSubCategoria').val() > 0)
            count++;
        if(+$('#sltEstado').val() > 0)
            count++;
        if(+$('#sltCidade').val() > 0)
            count++;


        if (count > 1) {

            var codCategoria = +$('#sltCategorias').val();
            var codSubCategoria = +$('#sltSubCategoria').val();

            var codEstado = +$('#sltEstado').val();
            var codCidade = +$('#sltCidade').val();

            var url = 'Busca.aspx?';
            url += ((codCategoria != null && codCategoria > 0) ? '&codCategoria=' + codCategoria : '');
            url += ((codSubCategoria != null && codSubCategoria > 0) ? '&codSubCategoria=' + codSubCategoria : '');

            url += ((codEstado != null && codEstado > 0) ? '&codEstado=' + codEstado : '');
            url += ((codCidade != null && codCidade > 0) ? '&codCidade=' + codCidade : '');

            window.location = url;
        } else {
            alert('Selecione as mais filtros para efetuar a busca');
        }

    });

});
