$(document).ready(function() {
    let searchTimer;

    $('#permitSearch').on('keyup', function() {
        clearTimeout(searchTimer);
        let query = $(this).val();

        $('#permitBody').css('opacity', '0.5');

        searchTimer = setTimeout(function() {
            $.ajax({
                url: window.location.pathname,
                type: "GET",
                data: { search: query },
                success: function(response) {
                    let html = $(response);

                    $('#permitBody').html(html.find('#permitBody').html());
                    
                    $('#permit-pagination').html(html.find('#permit-pagination').html());
                    $('#permitBody').css('opacity', '1');
                },
                error: function() {
                    console.error("Live search failed");
                    $('#permitBody').css('opacity', '1');
                }
            });
        }, 500); // 500ms delay 
    });
});