$(document).ready(function () {

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#txt_paciente').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Nombre del paciente',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consultivo/paciente',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });


    $('#txt_morbilidad').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Concepto de morbilidad',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consultivo/morbilidad',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });


    //function consultaSelection(state) {
        //console.log(window.location.origin)
        //'{{ route("showAnnouncement", ":id") }}'
        
        //$('#consulta-select').attr('href', window.location.origin + '/consulta/editar/' + state.id)
    //}
    function changeHrefConsulta(id) {
        if ($.isNumeric(id)) {
            makeVisible()
            $('#consulta-select').attr('href', window.location.origin + '/consulta/editar/' + id)
        } else {
            makeInvisible()
        }
    }
    function makeVisible() {
        $('#consulta-select').removeClass("invisible").addClass("visible")
    }
    function makeInvisible() {
        $('#consulta-select').removeClass("visible").addClass("invisible")
    }
    $("#txt_consulta").select2({
        theme: "classic",
        placeholder: "Consultas del paciente...",
        allowClear : true,
        data: []
    });
    $('#txt_consulta').on('select2:select', function (e) {
        changeHrefConsulta(e.params.data.id)
    });
    function getConsultaByPaciente(id) {
        $.ajax({
            //type: 'POST',
            url: '/consultivo/consulta',
            data: {idpaciente:id},
            success: function (data) {
                var elarray = JSON.parse(data);
                cadena = [];
                if (elarray.length == 0) {
                    makeInvisible()
                }
                for (i = 0; i < elarray.length; i++) {
                    if (i == 0) {
                        changeHrefConsulta(elarray[i].id)
                    }
                    cadena.push({id: elarray[i].id, text: '#' + elarray[i].id + ' - ' + elarray[i].diagnostico + ' - ' + elarray[i].fechaconsulta  });
                }
                $("#txt_consulta").empty()
                $("#txt_consulta").select2({
                     data:cadena,
                });
            },
            error: function (error) {
                console.log(error)
            }
        });
    }
    $('#txt_paciente').on('select2:select', function (e) {
        let id =  $('#txt_paciente').val()
        getConsultaByPaciente(id)
    });

    $('#txt_morbilidad').on('select2:select', function (e) {
        var texto =  $('#txt_morbilidad').find('option:selected').text();
        let id = texto.substring(texto.length - 1, texto.length);
        $('#txt_patologia').val(id);
        console.log(id);
        console.log('registrando...');
    });
});
