$(document).ready(function () {

    function update_pics(){
        $.ajax({
            type: "GET",
            url: '/nikio/pics_list',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            async: !1,
            success: (data) => {
                $('#report_table').empty();
                $('#total_pics').text(data['desc']['total_pics']);
                $('#nn_true').text(data['desc']['nn_true']);
                $('#nn_err').text(data['desc']['nn_err']);
                $('#accuracy').text(Math.round(100/(data['desc']['nn_true']+data['desc']['nn_err'])*data['desc']['nn_true']));
                for(num in data['data'])
                {
                    let btn = ''
                    if (data['data'][num]['included'] == true){
                        btn = '<div class="btn btn-danger x-icn exclude" data-pic_id="'+data['data'][num]['pic_id']+'"></div>'
                    }
                    else
                    {
                        btn = '<div class="btn btn-success check-icn include" data-pic_id="'+data['data'][num]['pic_id']+'"></div>'
                    }
                    let html_text = '<tr style="background-color:'+data['data'][num]['color']+'">'//
                    html_text += '<td>'+data['data'][num]['init_user']+'</td><td><img src=/img/'+data['data'][num]['pic_src']+' style="max-width:256px;"></img></td>'+
                    '<td>'+data['data'][num]['doctor_dianosis']+'</td>'+'<td>'+data['data'][num]['expert_dianosis']+'</td>'+
                    '<td class="recalc" data-pic_id="'+data['data'][num]['pic_id']+'">'+data['data'][num]['nn_diagnosis']+'</td>'+
                    '<td>'+btn+'</td>'
                    html_text += '</tr>'
                    $('#report_table').append(html_text);
                }
                exclude_pic();
                include_pic();
            }
        });

    }

    update_pics();

    $('#recalculate').click(function(){
        $.each($('.recalc'), function(e) {
            console.log($(this).data('pic_id'));
            var message = {
                pic_id: $(this).data('pic_id')
            }
            $.ajax({
                type:'POST',
                url: '/nikio/recalculate',
                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);
                    $(this).text(data['data']['nn_diagnosis']);
                },
                error:function (jqXHR, exception) {
                }
            });
        });
    })

    function exclude_pic(){
        $('.exclude').click(function(){
            console.log($(this).data('pic_id'));
            var message = {
                pic_id: $(this).data('pic_id')
            }
            $.ajax({
                type:'POST',
                url: '/nikio/exclude',
                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) => {
                    update_pics()
                },
                error:function (jqXHR, exception) {
                }
            });
        });
    }
    function include_pic(){
        $('.include').click(function(){
            console.log($(this).data('pic_id'));
            var message = {
                pic_id: $(this).data('pic_id')
            }
            $.ajax({
                type:'POST',
                url: '/nikio/include',
                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('OK')
                    update_pics()
                },
                error:function (jqXHR, exception) {
                }
            });
        });
    }

});