/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function () {

    init();
    validarSelect();

});

function init() {
    listarDepartamento();
    listarProvincia();
    listarDistrito();
}

function listarDepartamento() {
    $.ajax({
        type: 'POST',
        url: '../UbigeoServlet?accion=listarDepartamento',
        beforeSend: function (xhr) {

        },
        success: function (data, textStatus, jqXHR) {
            var status = data.status;
            var departamentos = data.data.departamentos;
            if (status) {
                var select_departamento = '<option value="0">[ SELECCIONE ]</option>';
                for (var i in departamentos) {
                    select_departamento += '<option value="' + departamentos[i].codigoDepartamento + '">' + departamentos[i].nombreDepartamento + '</option>';
                }
                $('#select_departamento').html(select_departamento);
                $('#select_departamento').selectpicker('refresh');
            } else {
                console.log('no hay data prro');
            }
        },
        complete: function (jqXHR, textStatus) {

        }
    });
}

function listarProvincia() {
    $('#select_departamento').on('change', function () {

        var json = {
            codigoDepartamento: $('#select_departamento').val()
        };

        $.ajax({
            type: 'POST',
            url: '../UbigeoServlet?accion=listarProvincia',
            dataType: 'json',
            data: {json: JSON.stringify(json)},
            beforeSend: function (xhr) {

            },
            success: function (data, textStatus, jqXHR) {
                var status = data.status;
                var provincias = data.data.provincias;
                if (status) {
                    var select_provincia = '<option value="0">[ SELECCIONE ]</option>';
                    for (var i in provincias) {
                        select_provincia += '<option value="' + provincias[i].codigoProvincia + '">' + provincias[i].nombreProvincia + '</option>';
                    }
                    $('#select_provincia').html(select_provincia);
                    $('#select_provincia').selectpicker('refresh');
                } else {
                    console.log('no hay data prro');
                }
            },
            complete: function (jqXHR, textStatus) {

            }
        });
    });
}

function listarDistrito() {
    $('#select_provincia').on('change', function () {
        var json = {
            codigoDepartamento: $('#select_departamento').val(),
            codigoProvincia: $('#select_provincia').val()
        };
        $.ajax({
            type: 'POST',
            url: '../UbigeoServlet?accion=listarDistrito',
            dataType: 'json',
            data: {json: JSON.stringify(json)},
            beforeSend: function (xhr) {

            },
            success: function (data, textStatus, jqXHR) {
                var status = data.status;
                var distritos = data.data.distritos;
                if (status) {
                    var select_distrito = '<option value="0">[ SELECCIONE ]</option>';
                    for (var i in distritos) {
                        select_distrito += '<option value="' + distritos[i].codigoDistrito + '">' + distritos[i].nombreDistrito + '</option>';
                    }
                    $('#select_distrito').html(select_distrito);
                    $('#select_distrito').selectpicker('refresh');
                } else {
                    console.log('no hay data prro');
                }
            },
            complete: function (jqXHR, textStatus) {

            }
        });
    });
}

function validarSelect() {

    $('#select_departamento').on('change', function () {
//        debugger;
        var select_departamento = $('#select_departamento').val();
        if (select_departamento === '0') {
            $('#select_provincia').html('<option value="0">[ SELECCIONE ]</option>');
            $('#select_distrito').html('<option value="0">[ SELECCIONE ]</option>');

            $('#select_provincia').selectpicker('refresh');
            $('#select_distrito').selectpicker('refresh');
        } else {
            $('#select_provincia').val(0);
            $('#select_distrito').val(0);
            $('#select_distrito').val('<option value="0">[ SELECCIONE ]</option>');

            $('#select_provincia').selectpicker('refresh');
            $('#select_distrito').selectpicker('refresh');
        }
    });

}