$(document).ready(function () {
    function get_file_info(image_id, reload){

        $.ajax({
            type: "GET",
            url: '/2_0/get_file_info/'+image_id,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            async: true,
            success: (data) => {
                console.log(data)
                tag_id = null;
                if (reload == true)
                    $('#diagnoses_select').data('img', data['data']['hash_name']);
                if (data['data']['user'] == 'Neural Network')
                    $('#nn_diagnos').text(data['data']['tag']);
                else
                {
                    if(data['data']['tag_id'])
                    {
                        if (reload == true){
                            $('#diagnoses_select').val(data['data']['tag_id']);
                            $('#diagnoses_select').trigger('change');
                        }
                    }
                    if (data['data']['nn'])
                        $('#nn_diagnos').text(data['data']['nn']['tag']);
                }

            }
        });
    }

    function save_select(element){
        if(element.value == 0)
            return;
        var message = {
            diagnos_id: element.value,
            pic_name: $(element).data('img'),
        }
        $.ajax({
            type:'POST',
            url: '/2.0/save_pic_diagnos',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            data: JSON.stringify(message),
            success: (data) => {
                console.log(data);
                show_success('Данные сохранены', false);
            },
            error:function (jqXHR, exception) {
                show_error('Не удалось сохранить данные', false);
            }
        });

    }

    function select_fill(){
        lang = getUrlParameter('lang');
        $('#diagnoses_select').empty();
        $.ajax({
            type:'GET',
            url: '/get_diagnos_tags/'+$('#profarea').val()+'?lang='+lang,
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                    $('#diagnoses_select').append('<option value="0" disabled selected>Диагноз</option>');
                    for(tag_num in data)
                    {
                        var tag = data[tag_num]
                        if(tag['category_id'] == null && tag['diagnos'] == true)
                        {
                            var icd = ''
                            if (tag['icd'])
                            {
                                icd = tag['icd'] + ' '
                            }
                            $('#diagnoses_select').append('<option value="'+tag['id']+'">'+icd+tag['standart_name']+'</option>');
                        }
                    }
            },
            error:function (jqXHR, exception) {

            }
        });
    }

    function get_pics(){
        $.ajax({
            type:'GET',
            url: '/get_new_files',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                let last_group = data['result']['groups'][0]
                let last_file = data['result'][last_group][data['result'][last_group].length-1]
                console.log(data['result'][last_group])
                let path = last_file['hash_name']
                if (last_file['id'] != $('#last_file').val()){
                    $("#exam_time").text(' от '+last_file['index_date'])
                    $('#nn_diagnos').text('');
                    $('#diagnoses_select').val(0);
                    $('#diagnoses_select').trigger('change');
                    $('#last_file').val(last_file['id']);
                    $('#video_play_file').hide();
                    $('#video_controls').hide();
                    $('#current_img').show();
                    $('#current_img').attr('src', '/img/'+path);
                    get_file_info(last_file['id'], true);
                    select_fill();
                     $('#diagnoses_select').on('change', function() {
                        save_select(this);
                    });
                }
                else
                {
                    if ($('#nn_diagnos').text() == '')
                    {
                        get_file_info(last_file['id'], false)
                    }
                    console.log('Nothing new')
                }
            },
            error:function (jqXHR, exception) {

            }
        });
    }

    $('#diagnoses_select').select2();
    get_pics();
    setInterval(() => get_pics(), 5000);

});