<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<script src="<?php echo base_url(); ?>assets/js/script.js?v=<?php echo date('ihs') ?>"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $(".search_load_more").hide();
        $("#search_page_no").val(1);
        search_result();
    });
    var isSubmitting = false;
    var debounceTimer;
	function call_ajax(url,data,datatype,sync,type){
        sync = typeof sync !== 'undefined' ? sync : false;
        if(isSubmitting && !sync){ return; }
        isSubmitting = true;
        var myajax = $.ajax({
            url: url,
            type: type,
            dataType: datatype,
            data:  data,
            async: sync,
            beforeSend: function() {
                //$(".loader").css("display", "block");
            },
            complete:function()
            {
                //$(".loader").css("display", "none");
                isSubmitting = false;
                //equalheight('.project_location');
            }
        });
        return myajax;
    }

    $('#filesSearchModal').on('shown.bs.modal', function () {
        $('#seachText').focus();
    });

    $(document).on("keyup", "#seachText", function(event){
        //if(event.keyCode === 13){
            $("#search_page_no").val(1);
            debounceSearch();
        //}
    });

    $(document).on("click", ".search_load_more", function(){
        debounceSearch();
    });

    function debounceSearch(){
        clearTimeout(debounceTimer); // Clear any previous debounce timer
        debounceTimer = setTimeout(function(){
            search_result(); // Call the search function after debounce time
        }, 300); // Adjust the debounce time (in milliseconds) as needed
    }

    function search_result(){
        var base_url = '<?php echo base_url(); ?>';
        var keywords = $("#seachText").val();
        var url = base_url+"products/search_load_data";

        var search_page_no = $("#search_page_no").val();
                    
        var contentLoad = call_ajax(url, {keywords: keywords, page_no: search_page_no}, 'JSON', true, 'POST');
        contentLoad.done(function(result){

            if(result["status"] === false){
                console.log(result['message']);
            }else{
                if(result['page_no'] > 2){
                    $('#search_result').append(result['search_result']);
                }else{
                    $('#search_result').html(result['search_result']);
                }
                if(result['show_more'] == 0){
                    $(".search_load_more").hide();
                }else{
                    $(".search_load_more").show();
                }

                $("#search_page_no").val(result['page_no']);
            }

        });
    }
</script>