$(document).ready(function () {

    lastVisits();

    reload_visit_info();

    function reload_visit_info(){
        if($('#visit_id').val()){
            lang = getUrlParameter('lang');
            $.ajax({
                type:'GET',
                url: '/reload_visit_info/' + $('#visit_id').val() + '?task=off'+'&lang='+lang,
                processData: false,
                contentType: false,
                success: (data) => {
                    //load__endo_history(false);
                }
            });
        }
    }

    $('#visits_link').click(function(){
        lastVisits();
    })

    $('#recover_visit').click(function(){

        visit_id = $('#visit_id').val()
        var message = {
            visit_id: visit_id,
        }
        $.ajax({
            type:'POST',
            url: '/ca_recover_visit',
            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)
                {
                    $('#recover_visit').hide();
                    $('#delete_visit').show();
                }
                else
                {
                    console.log('Saving error')
                    $('#exception').html('Ошибка, не удалось обновить информацию');
                    $('#exception').slideDown(500);
                    $('#exception').delay(2000).slideUp();
                }
            }
        });

    });

    $('#delete_visit').click(function(){
        visit_id = $('#visit_id').val()
        var message = {
            visit_id: visit_id,
        }
        $.ajax({
            type:'POST',
            url: '/ca_delete_visit',
            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)
                {
                    $('#recover_visit').show();
                    $('#delete_visit').hide();
                }
                else
                {
                    console.log('Saving error')
                    if(notify == true)
                    {
                        $('#exception').html('Ошибка, не удалось обновить информацию');
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                    }
                }
            }
        });
    });

    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 lastVisits(){
        $('#last_visits').empty();
        var lang = getUrlParameter('lang');
        $.ajax({
            type:'GET',
            url: '/last_visits/'+$('#patient_id').val()+'?lang=' + lang,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
//                console.log(data)
                for (visit_num in data['visits'])
                {
                    visit = data['visits'][visit_num]
                    if (data['full_name'])
                        var full_name = data['full_name'];
                    else
                        var full_name = 'N/D';

                    if($('#language').val() == 'ru')
                        $('#last_visits').append('<tr><td><a href="/medical_report/'+visit['id']+'?nn_diagnos=true">'+full_name+'</a></td><td>'+visit['date']+'</td><td>'+visit['clinic']+'</td><td>'+visit['doctor']+'</td><td>'+visit['profarea']+'</td></tr>');
                    else
                        $('#last_visits').append('<tr><td><a href="/medical_report/'+visit['id']+'?lang=en&nn_diagnos=true">'+full_name+'</a></td><td>'+visit['date']+'</td><td>'+visit['clinic']+'</td><td>'+visit['doctor']+'</td><td>'+visit['profarea']+'</td></tr>');

                }
            },
            error:function (jqXHR, exception) {

            }
        });
    }


});
