jQuery(document).ready(function ($) {
   
    $('.tablenav.top #post-query-submit , .tablenav.bottom #post-query-submit').click(function (e) {
        if ($('.metaseo-filter').val() == 'bulk-copy-metatitle') {
            e.preventDefault();
            $('#bulk-copy-metatitle').show();
        }
    });

    $('.btn_do_copy').click(function (e) {
        var $this = $(this);
        var sl_bulk = $('.mbulk_copy').val();
        if (sl_bulk == 0)
            return;

        var mpost_selected = [];
        if (sl_bulk != 'all') {
            $(".metaseo_post").each(function (index) {
                if ($(this).is(':checked')) {
                    mpost_selected.push($(this).val());
                }
            });
        }

        if ($this.hasClass('post_do_copy')) {
            var maction = 'wpms_bulk_post_copy';
            var mtype = 'post_title';
        } else {
            var maction = 'wpms_bulk_image_copy';
            if($this.hasClass('image_do_copy_alt')){
                var mtype = 'image_alt';
            }else{
                var mtype = 'image_title';
            }
        }

        $this.closest('.tablenav').find('.spinner').show().css('visibility', 'visible');
        $.ajax({
            url: ajaxurl,
            method: 'POST',
            dataType: 'json',
            data: {
                action: maction,
                ids: mpost_selected,
                sl_bulk: sl_bulk,
                mtype:mtype
            },
            success: function (res) {
                if (res == true) {
                    window.location.assign(document.URL);
                } else {
                    alert(wpmseobulkL10n.metaseo_message_false_copy);
                }
                $this.closest('.tablenav').find('.spinner').hide();
            }

        });
    });

});