$(document).ready(function(){
    
    $('#semana').hide();
    $('#semanaeco').hide();

    $('#div_5_1').hide();
    $('#div_5_2').hide();
    $('#div_5_3').hide();
    $('#div_5_4').hide();
    $('#div_5_5').hide();
    $('#div_5_6').hide();
    $('#div_5_7').hide();
    $('#div_5_8').hide();
    $('#div_5_9').hide();
    $('#div_5_10').hide();
    
    var s = "";
    s = document.getElementById("txt_5_1").value;
    if(s!==""){
        $('#div_5_1').show();
    }

    s = document.getElementById("txt_5_2").value;
    if(s!==""){
        $('#div_5_2').show();
    }

    s = document.getElementById("txt_5_3").value;
    if(s!==""){
        $('#div_5_3').show();
    }

    s = document.getElementById("txt_5_4").value;
    if(s!==""){
        $('#div_5_4').show();
    }

    s = document.getElementById("txt_5_5").value;
    if(s!==""){
        $('#div_5_5').show();
    }

    s = document.getElementById("txt_5_6").value;
    if(s!==""){
        $('#div_5_6').show();
    }

    s = document.getElementById("txt_5_7").value;
    if(s!==""){
        $('#div_5_7').show();
    }

    s = document.getElementById("txt_5_8").value;
    if(s!==""){
        $('#div_5_8').show();
    }

    s = document.getElementById("txt_5_9").value;
    if(s!==""){
        $('#div_5_9').show();
    }

    s = document.getElementById("txt_5_10").value;
    if(s!==""){
        $('#div_5_10').show();
    }

    $('#div_7_1').hide();
    $('#div_7_2').hide();
    $('#div_7_3').hide();
    $('#div_7_4').hide();
    $('#div_7_5').hide();
    $('#div_7_6').hide();

    s = document.getElementById("txt_7_1").value;
    if(s!==""){
        $('#div_7_1').show();
    }

    s = document.getElementById("txt_7_2").value;
    if(s!==""){
        $('#div_7_2').show();
    }

    s = document.getElementById("txt_7_3").value;
    if(s!==""){
        $('#div_7_3').show();
    }

    s = document.getElementById("txt_7_4").value;
    if(s!==""){
        $('#div_7_4').show();
    }

    s = document.getElementById("txt_7_5").value;
    if(s!==""){
        $('#div_7_5').show();
    }

    s = document.getElementById("txt_7_6").value;
    if(s!==""){
        $('#div_7_6').show();
    }
    
    //llamada a funciones luego de cargar los datos:
    CambioFechaEdit();
    CambioFechaEco();
    CalculaIMC();

   $('#txt_peso').change(function(){
       CalculaIMC();
   });
   
   $('#txt_medico').change(function(){
       var x=document.getElementById("txt_medico").value;
       document.getElementById("txt_idmedico").value = x;
   });
   $('#txt_seguro').change(function(){
       var x=document.getElementById("txt_seguro").value;
       document.getElementById("txt_idseguro").value = x;
   });
   
   $('#txt_talla').change(function(){
       CalculaIMC();
   });

   $('#txt_nrosemanaeco').change(function(){
       CambioFechaEco();
   });

   $('#7_1_C').change(function(){
       $('#div_7_1').show();
    });
   $('#7_1_S').change(function(){
       $('#div_7_1').hide();
   });
   
   $('#7_2_C').change(function(){
       $('#div_7_2').show();
   });
   $('#7_2_S').change(function(){
       $('#div_7_2').hide();
   });

   $('#7_3_C').change(function(){
       $('#div_7_3').show();
   });
   $('#7_3_S').change(function(){
       $('#div_7_3').hide();
   });

   $('#7_4_C').change(function(){
       $('#div_7_4').show();
   });
   $('#7_4_S').change(function(){
       $('#div_7_4').hide();
   });
    
   $('#7_5_C').change(function(){
       $('#div_7_5').show();
   });
   $('#7_5_S').change(function(){
       $('#div_7_5').hide();
   });

   $('#7_6_C').change(function(){
       $('#div_7_6').show();
   });
   $('#7_6_S').change(function(){
       $('#div_7_6').hide();
   });
    

   $('#5_1_C').change(function(){
       $('#div_5_1').show();
    });
   $('#5_1_S').change(function(){
       $('#div_5_1').hide();
   });
   
   $('#5_2_C').change(function(){
       $('#div_5_2').show();
   });
   $('#5_2_S').change(function(){
       $('#div_5_2').hide();
   });

   $('#5_3_C').change(function(){
       $('#div_5_3').show();
   });
   $('#5_3_S').change(function(){
       $('#div_5_3').hide();
   });

   $('#5_4_C').change(function(){
       $('#div_5_4').show();
   });
   $('#5_4_S').change(function(){
       $('#div_5_4').hide();
   });
    
   $('#5_5_C').change(function(){
       $('#div_5_5').show();
   });
   $('#5_5_S').change(function(){
       $('#div_5_5').hide();
   });

   $('#5_6_C').change(function(){
       $('#div_5_6').show();
   });
   $('#5_6_S').change(function(){
       $('#div_5_6').hide();
   });

   $('#5_7_C').change(function(){
       $('#div_5_7').show();
    });
   $('#5_7_S').change(function(){
       $('#div_5_7').hide();
   });
   
   $('#5_8_C').change(function(){
       $('#div_5_8').show();
   });
   $('#5_8_S').change(function(){
       $('#div_5_8').hide();
   });

   $('#5_9_C').change(function(){
       $('#div_5_9').show();
   });
   $('#5_9_S').change(function(){
       $('#div_5_9').hide();
   });

   $('#5_10_C').change(function(){
       $('#div_5_10').show();
   });
   $('#5_10_S').change(function(){
       $('#div_5_10').hide();
   });

   $('#txt_idMedicamento').change(function(){
       try{
           var data = $('#txt_idMedicamento').select2('data');
           var x=data[0].text;
           document.getElementById("txt_descMedicamento").value = x;
       }
       catch(e){
           document.getElementById("txt_descMedicamento").value = "";
       }
   });
   
   $('#diaspermiso').change(function(){
       var dias = document.getElementById("diaspermiso").value;
       var consulta = document.getElementById("txt_id").value;
       if(dias>0){
            $('#certificado').html('<a target="_blank" type="button" class="form-control" title="Emisi&oacute;n de Certificado" href="https://grupohqs.com/CERTIFICADOMEDICO.php?CodConsulta='+consulta+'&dias='+dias+'" class="btn btn-warning btn_reporte"><i class="fas fa-print"></i></a>');
       }else{
           $('#certificado').html('');
       }

   });

    
});

function LlenarDescripcion(){
}

function CambioFechaEdit(){
    var x = document.getElementById("fechperiodo").value;
    if(x!=""){
        var fecha = new Date(document.getElementById("fechperiodo").value);
        document.getElementById("fechaultperiodo").value = x;
        
        var dias = 280; 
        var fechaNow = new Date();
        var diff = fechaNow - fecha;
        
        fecha.setDate(fecha.getDate() + dias);
        var FPP = fecha.toISOString().slice(0,10);
        document.getElementById("fechFPP").value = FPP;
        document.getElementById("FPP").value = FPP;
        
        var nroSemana = 0;
        var nroDias = Math.round(diff/(1000*60*60*24));
        nroSemana = Math.floor(nroDias / 7);
        nroDias = nroDias % 7;
        document.getElementById("txt_nrosemana").value = nroSemana +'.' + nroDias;

        if(nroSemana>=3){
            $('#semana').html('<a href="https://blogdelbebe.com/embarazo/semana-' + nroSemana + '/" id="linkSemanas" target="_blank"><button id="btnSemana" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button></a>');
        }else{
            $('#semana').html('<button id="btnSemana" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button>');
        }
        $('#semana').show();
    }
}


function CambioFecha(e){
    var x = (e.target.value);
    if(x!=""){
        var fecha = new Date(e.target.value);
        document.getElementById("fechaultperiodo").value = x;
        
        var dias = 280; 
        var fechaNow = new Date();
        var diff = fechaNow - fecha;
        
        fecha.setDate(fecha.getDate() + dias);
        var FPP = fecha.toISOString().slice(0,10);
        document.getElementById("fechFPP").value = FPP;
        document.getElementById("FPP").value = FPP;
        
    
        var nroSemana = 0;
        var nroDias = Math.round(diff/(1000*60*60*24));
        nroSemana = Math.floor(nroDias / 7);
        nroDias = nroDias % 7;
        document.getElementById("txt_nrosemana").value = nroSemana +'.' + nroDias;
        
        if(nroSemana>=3){
            $('#semana').html('<a href="https://blogdelbebe.com/embarazo/semana-' + nroSemana + '/" id="linkSemanas" target="_blank"><button id="btnSemana" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button></a>');
        }else{
            $('#semana').html('<button id="btnSemana" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button>');
        }
        $('#semana').show();
    }
}

function CambioFechaEco(){
    //var x = (e.target.value);
    var x = document.getElementById("fecheco").value;
    var nroSemanaEco = document.getElementById("txt_nrosemanaeco").value;
    if(x!=="" && nroSemanaEco!==""){
        document.getElementById("fechaulteco").value = x;
        //calcular la cantidad de dias transcurridos
        var diasT = nroSemanaEco * 7;
        //calcular la fecha inicio del proceso
        var fecha = new Date(x);
        fecha.setDate(fecha.getDate() - diasT);

        var dias = 280; 
        var fechaNow = new Date();
        var diff = fechaNow - fecha;
        
        fecha.setDate(fecha.getDate() + dias);
        var FPP = fecha.toISOString().slice(0,10);
        document.getElementById("fechFPPeco").value = FPP;
        document.getElementById("FPPeco").value = FPP;
        
        var nroSemana = 0;
        var nroDias = Math.round(diff/(1000*60*60*24));
        nroSemana = Math.floor(nroDias / 7);
        nroDias = nroDias % 7;

        if(nroSemana>=3){
            $('#semanaeco').html('<a href="https://blogdelbebe.com/embarazo/semana-' + nroSemana + '/" id="linkSemanaseco" target="_blank"><button id="btnSemanaeco" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button></a>');
        }else{
            $('#semanaeco').html('<button id="btnSemanaeco" type="button" class="btn btn-primary">Semana <span class="badge badge-light">'+ nroSemana +'.' + nroDias + '</span><span class="sr-only">semanas de embarazo</span></button>');
        }
        $('#semanaeco').show();
    }
}


function CalculaIMC(){
    if($('#txt_peso').val() !== '' && $('#txt_talla').val() !== ''){
        peso = $('#txt_peso').val();
        talla = $('#txt_talla').val();
        talla = (talla/100);
        talla = (talla*talla);
        imc = Math.round(peso/talla);
        document.getElementById('txt_IMC').value = imc;
    }
}

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



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


    $('#txt_idMedicamento').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Lista de Medicamentos',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consulta/medicamento',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                s = document.getElementById("txt_idseguro").value;
                var query = {
                  search: params.term,
                  seguro: s
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });


    $('#txt_8_1').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Diagnostico [1]',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consulta/diagnostico',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });

    $('#txt_8_2').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Diagnostico [2]',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consulta/diagnostico',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });

    $('#txt_8_3').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Diagnostico [3]',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consulta/diagnostico',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });

    $('#txt_8_4').select2({
        theme: "classic",
        allowClear: true,
        placeholder: 'Diagnostico [4]',
        minimumInputLength: 3,
        ajax: {
            delay: 1000,
            url: '/consulta/diagnostico',
            dataType: 'json',
            contentType: "application/json",
            data: function (params) {
                var query = {
                  search: params.term,
                }
                return query;
            },
            processResults: function (data) {
                return {
                  results: data
                };
            },
        }
    });

