$(document).ready(function () {

    $('#save_patient_props').on('click', function(){save_patient_props(false)});

    if ($('#nic_visit').length > 0){
        $('#patient_props_modal').modal('show');
    }

    $("#fio_select2").select2({
        tags: true,
        width: '100%'
    });

    $("#fio_select2").on("change", function() {
        id = this.value;
        text = $( "#fio_select2 option:selected" ).text();
        if (id == text)
        {
            console.log('New Patient');
        }
        else{
            $.ajax({
                type: "GET",
                url: '/get_patient_clinic/' + id,
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType
                success: (data) => {
                    $("#med_card").val(data['card_number']);
                    $("#telephone").val(data['telephone']);
                    $("#dob").val(data['dob']);
                    $("#sex").val(data['sex']);
                    $("#email").val(data['email']);
                }
            })
        }
    })

    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
        return false;
    };

    function save_patient_props(redirect){

        fio = $('#fio').val();
        dob = $('#dob').val();
        med_card = $('#med_card').val();
        sex = $('#sex').val();
        telephone = $('#telephone').val();
        email = $('#email').val();
        false_form = false
        lang = getUrlParameter('lang');
        if(false_form==false)
        {
            var message = {
                fio: fio,
                dob:dob,
                med_card:med_card,
                sex:sex,
                telephone:telephone,
                email:email
            }
            $.ajax({
                type:'POST',
                url: '/update_patient/' + $('#patient_id').val(),
                async: !1,
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType
                data: JSON.stringify(message),
                success: (data) => {
                    if(data['success'] == true)
                    {
                        if (lang == 'en')
                            $('#success').html('Parameters updated')
                        else
                            $('#success').html('Информация обновлена')
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                        $('#patient_full_name').html(fio);
                        if ($('#nic_visit').length > 0){
                            $('#fio_info').html(fio);
                            $('#dob_info').html(dob);
                            $('#med_card_info').html(med_card);
                            $('#sex_info').html(sex);
                            $('#patient_props_modal').modal('hide');
                        }
                    }
                    else
                    {
                        if (lang == 'en')
                            $('#exception').html('Failed to update parameters')
                        else
                            $('#exception').html('Не удалось обновить')
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                    }
                }
            });
        }
    }

});
