jQuery(document).ready(function($) {
    const overlay = $('#scrapers-overlay');
    function showOverlay() {
        if (overlay.length) {
            overlay.css('display', 'flex');
        }
    }
    function hideOverlay() {
        if (overlay.length) {
            overlay.hide();
        }
    }

    function deleteCookie(name) {
        document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }
    var wrg = 'Warning';
    var nte = 'Note';
    var ero = 'Error';
    var delay = (function() {
        var timer = 0;
        return function(callback, ms) {
            clearTimeout(timer);
            timer = setTimeout(callback, ms);
        };
    })();
    
    function importCookie(name, value, days) {
        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (3600 * 1000 * 24 * 365));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "") + expires + "; path=/";
    }

    function show_history() {
        $('#scraper_response_history').show();
        $("#scrapes-logs-box ul").append('<li class="jump"><span class="type green">Filter</span> ' + scraper.completed + '</li>');
    }

    function forbidden() {
        $('#scraper_response_history').show();
        $("#scrapes-logs-box ul").append('<li class="jump">' + scraper.forbidden + '</li>');
    }

    function search_history() {
        $('#scraper_response_history').show();
        $("#scrapes-logs-box ul").append('<li class="jump"><span class="type green">Search</span> ' + scraper.completed + '</li>');
    }

    function cimport(id, nonce, permalink) {
        $('#c' + id).hide();
        $('#' + id).removeClass('scimport');
        $('#' + id).addClass('fadeInDown');
        $('#' + id).addClass('preimport');
        $("#bulk-importer-click").hide();
        $("#import_all").hide();
        $.ajax({
            url: scraper.url,
            type: 'post',
            data: {
                id: id,
                nonce: nonce,
                permalink: permalink,
                action: 'scraper_app_post'
            },
            error: function(response) {
                //console.log(response);
            },
            success: function(response) {
                $("#scrapes-logs-box ul").append(response);
                $('#' + id).removeClass('preimport');
                $('#' + id).addClass('scimported');
                $('#' + id).addClass('jump');
            }
        });
    }

    $(document).on('submit', '#scraper-filter', function() {
        var that = $(this);
        var ajaxurl = scraper.url;
        showOverlay(); // --- BARU: Tampilkan overlay saat filter dimulai ---
        $('#scraper_response').addClass('scclick');
        $('#scraperfilter').prop('disabled', true);
        $('#scraperfilter').val(scraper.filtering);
        $.ajax({
            url: ajaxurl,
            type: 'post',
            data: that.serialize(),
            error: function(response) {
                console.log(response);
            },
            success: function(response) {
                show_history();
                $('#scraper_response').removeClass('scclick');
                $('#scraperfilter').prop('disabled', false);
                $('#scraperfilter').val(scraper.filter);
                $('#scraper_response').html(response);
                $("#bulk-importer-click").show();
                $("#import_all").show();
                $('.pagex').click(function() {
                    var page = $(this).data('page');
                    $('#page').val(page);
                    importCookie('importpage', page);
                    $('#scraperfilter').trigger('click');
                    $("#import_all").hide();
                });
                $('.cimport').click(function() {
                    var id = $(this).data('id');
                    var permalink = $(this).data('permalink');
                    var nonce = $(this).data('nonce');
                    cimport(id, nonce, permalink);
                    return false;
                });
            },
            complete: function() {
                hideOverlay();
            }
        });
        return false;
    });


    $('#import_all').click(function() {
        $("#bulk-importer-click").hide();
        $("#import_all").hide();
        $(".cimport").trigger('click');
        return false;
    });
    $('#reset_all').click(function(e) {
        e.preventDefault();
        $("#scrapes-logs-box ul").empty();
        $("#scrapes-logs-box ul").append('<li class="jump"><span class="type">importer</span>Log cleaned</li>');
        return false;
    });

    $("#scrapes-logs-box ul").append('<li class="jump"><span class="type">importer</span>' + scraper.welcomeText + '</li>');

});