$( document ).ready(function($) {
    let activeIncompleteLeads = true;

    const changeActiveIncompleteLeads = () => {

        if(activeIncompleteLeads == true) {
            activeIncompleteLeads = false;

            $('input[name=include_draft]').attr('value', 'false');

            $('.af2_post_table_body .af2_post_table_row').each((i, el) => {
                let id = $(el).attr('id');
                if(id != undefined) {
                    let post_status = $('.af2_post_table_body .af2_post_table_row#' + id +' .af2_post_table_content[data-searchfilter="post_status"]').data('searchvalue');
                    if(post_status == 'draft') $('.af2_post_table_body .af2_post_table_row#' + id).addClass('af2_hide');
                    if(post_status == 'draft') $('.af2_post_table_body .af2_post_table_row#' + id).addClass('af2_table_highlight');
                }
            });
        }
        else {
            activeIncompleteLeads = true;

            $('input[name=include_draft]').attr('value', 'true');

            $('.af2_post_table_row.af2_hide').each((i, el) => {
                $('#'+$(el).attr('id')).removeClass('af2_hide');
            });
        } 
    }

    $(document).on('click', '#af2_show_uncomplete', changeActiveIncompleteLeads );

    $('.af2_choose_table_object').on('click', function() {
        let ids = [];
        $('.af2_choose_table_object:checked').each((i, el) => {
            ids.push($(el).attr('id'));
        });
        if(ids.length) {
            $('input[name=form_ids]').attr('value', ids.join(','));
        } else {
            $('input[name=form_ids]').attr('value', 'all');
        }
    });

    changeActiveIncompleteLeads();
});
