$(document).ready(function () {

    function update_table(patient_id){
        $('#patient_routings').empty();
        $.ajax({
            type: "GET",
            url: '/routing/get_patient_routings/' + patient_id,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                for(i in data['result'])
                {
                    $('#patient_routings').append('<div class="col-4"><p>' +data['result'][i][1]+ '</p></div><div class="col-8"><p>' +data['result'][i][0]+ '</p></div>')
                }
            }
        })
    }

     $('#complete').click(function(){
        $.ajax({
            type: "GET",
            url: '/route/' + $('#visit_id').val(),
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                console.log(data);
                if(data['success'] == true)
                {
                    $.each($('.direction-modal'), function(e) {
                        if($(this).data('visit_id') == $('#visit_id').val())
                        {
                            $(this).children('.complete_status').empty();
                            $(this).children('.complete_status').append('<span style="color: #530FAD;" class="v-icn"></span>');
                            $(this).data('completed', true);
                        }
                    });
                    update_table($('#patient_id').val());
                    $('#completed').show();
                    $('#complete').hide();
                }
                else
                {
                    show_error('Не удалось завершить визит',0);
                }
            },
            error: function (jqXHR, exception) {
                show_error('Не удалось завершить визит',0);
            }
        })
     });

    $('.direction-modal').click(function(){
        $('#patient_routings').empty();
        $('#direction_info').modal('toggle');
        let visit_id = $(this).data('visit_id');
        let patient_id = $(this).data('patient_id');
        let completed = $(this).data('completed');
        $('#add_reception').data('visit_id', visit_id);
        $('#add_reception').data('patient_id', patient_id);
        lang = getUrlParameter('lang');
        if(visit_id)
        {
            $('#user_report').show();
            $('#user_report').attr('href', '/medical_report/'+visit_id+'?nn_diagnos=true&lang='+lang);
            $('#visit_id').val(visit_id);
        }
        else
        {
            $('#user_report').hide();
            $('#visit_id').val('');
        }
        console.log('completed', completed)
        if(completed != undefined)
        {
            $('#completed').show();
            $('#complete').hide();
        }
        else
        {
            $('#complete').show();
            $('#completed').hide();
        }
        if(patient_id)
        {
            $('#patient_info').show();
            $('#patient_info').attr('href', '/patient/'+patient_id+'?lang='+lang);
            $('#patient_id').val(patient_id);
        }
        else
        {
            $('#patient_info').hide();
            $('#patient_id').val('');
        }
        $('#patient_full_name').text('');
        $('#patient_dob').text('');
        $('#patient_telephone').text('');
        $('#patient_email').text('');
        $('#patient_visit_date').text('');
        $.ajax({
            type: "GET",
            url: '/get_patient_info/' + patient_id,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                $('#patient_full_name').text(data['full_name']);
                $('#patient_dob').text(data['dob']);
                $('#patient_telephone').text(data['telephone']);
                $('#patient_email').text(data['email']);
            }
        })
        update_table(patient_id);
    })

    $('#add_reception').click(function(){
            patient_id = $(this).data('patient_id');
            clinic_rt_id = $('#reception_pat').val();
            var message = {
                patient_id: patient_id,
                clinic_rt_id: clinic_rt_id,
            }
            $.ajax({
                type: "POST",
                url: '/internal_route_patient',
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType\
                data: JSON.stringify(message),
                success: (data) => {
                    update_table(patient_id);
                    $('#reception_pat').prop('selectedIndex',0);
                }
            })
        }

    )

});
