$(document).ready(function() {

    reloadCategories();
    mapTags();

    $('#add_tag_diagnos').click(function(){
        if($(this).is(':checked'))
        {
            $('#category_block').hide();
            $('#icd_block').show();
        }
        else
        {
            $('#category_block').show();
            $('#icd_block').hide();
        }
    });

    $('#add_group').click(function(){
        var message = {
                name: $('#add_group_name').val()
            }
            $.ajax({
                type:'POST',
                url: '/add_category',
                async: false,
                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)
                    {
                        $('#add_group_name').val('');
                        $('#add_group_name').css('background-color', 'lightgreen');
                        $('#add_tag_category').empty();
                        $('#add_tag_category').append('<option value="" disabled="" selected="">Категория</option>');
                        reloadCategories();
                        mapTags();
                    }
                    else
                    {
                        $('#add_group_name').css('background-color', 'lightcora');
                    }
                }
            });
    });

     $('#check_diagnoses').click(function(){
            $.ajax({
                type:'POST',
                url: '/check_diagnoses',
                async: false,
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType
                success: (data) => {
                    if(data['success'] == true)
                    {
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                    }
                    else
                    {
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                    }
                }
            });
    });

     $('#add_tag').click(function(){
        $('#add_tag_name').css('background-color', 'white');
        var message = {
                name: $('#add_tag_name').val(),
                icd: $('#add_icd_code').val(),
                diagnos: $('#add_tag_diagnos').is(':checked'),
                category: $('#add_tag_category').val()
            }
            $.ajax({
                type:'POST',
                url: '/add_tag',
                async: false,
                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)
                    {
                         $('#add_tag_name').css('background-color', 'lightgreen');
                         $('#add_tag_name').val('');
                         $('#add_icd_code').val('');
                    }
                    else
                    {
                         $('#add_tag_name').css('background-color', 'lightcora');
                    }
                    mapTags();
                }
            });
    });


    function reloadCategories(){
        $.ajax({
            type:'GET',
            url: '/get_categories',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                for(category in data)
                {
                    $('#add_tag_category').append('<option value=' + data[category]['id'] + '> ' + data[category]['name'] + ' </option>');
                }
            },
            error:function (jqXHR, exception) {

            }
        });
    }

    //Функция отрисовки формы!
    function mapTags(){
        $('#categorized_tags_header').empty();
        $('#categorized_tags_body').empty();
        $('#diagnoses_tags').empty();
        $.ajax({
            type:'GET',
            url: '/get_categories',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                var show = 'show';
                for(category in data)
                {
                    $('#categorized_tags_header').append('<a data-toggle="collapse" href="#cat'+data[category]['id']
                    +'" aria-expanded="true" aria-controls="#cat'+data[category]['id']
                    +'" class="btn btn-link">'+data[category]['name']+'</a>')
                    $('#categorized_tags_body').append('<div id="cat'+data[category]['id']
                    +'" class="collapse '+show+'">');
                    show = ''
                    for(tag_num in data[category]['tags'])
                    {
                        var tag = data[category]['tags'][tag_num]
                        $('#cat'+data[category]['id']).append('<div class="custom-control custom-checkbox my-1 mr-sm-2 checktag"><input type="checkbox" id="tag'+tag['id']
                        +'" data-name="'+tag['name']+'" class="custom-control-input checkitem"><label for="tag'+tag['id']+'" class="custom-control-label">'+tag['name']+'</label></div>');
                    }
                }
                $('.collapse').on('show.bs.collapse', function (e) {
                    $('.collapse').collapse("hide")
                })
            },
            error:function (jqXHR, exception) {

            }
        });
        $.ajax({
            type:'GET',
            url: '/get_tags',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                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_tags').append('<div class="custom-control custom-checkbox my-1 mr-sm-2 checktag"><input type="checkbox" id="tag'+tag['id']
                        +'" data-name="'+tag['name']+'" class="custom-control-input checkitem"><label for="tag'+tag['id']+'" class="custom-control-label">'+icd+tag['name']+'</label></div>');
                    }

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

            }
        });
        $('.collapse').addClass('show');
    }
});
