<link href="/js/plugins/autocomplete/jquery-ui.css" rel="stylesheet">
<script src="/js/plugins/autocomplete/jquery-ui.min.js"></script>
<?php
    $format = config('date.formats.datepicker_date_format');
?>
<script>
    $(".search-asset-input").each(function() {
        var currentInput = $(this);
        currentInput.autocomplete({
            source: function(request, response) {
                var type = currentInput.closest('form').find('.general_type').val();
                $.ajax({
                    url: '/get-search-fields',
                    dataType: "json",
                    data: {
                        search: request.term,
                        type: type,
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                label: item.label,
                                value: item.value,
                                asset_type_tab: item.asset_type_tab
                            };
                        }));
                    }
                });
            },
            minLength: 1,
            select: function(event, ui) {
                event.preventDefault();
                var form = currentInput.closest('form'); // Find the parent form
                form.find(".search-asset-input").val(ui.item.label);
                form.find(".asset_tab_selected").val(ui.item.asset_type_tab);
                form.find('.search-asset-form-submit').trigger("click");
            },
            open: function() {
                $(".ui-autocomplete").css("z-index", 900);
            }
        });
    });


    $(document).on("click", ".search-asset-form-submit", function(e) {
        e.preventDefault();
        var form = $(this).closest('form'); // Find the parent form

        if (form.find(".search-asset-input").val()) {
            var searchText = form.find(".search-asset-input").val();
            var generalType = form.find(".general_type").val();
            var assetTabSelected = form.find(".asset_tab_selected").val();

            var url = '';
            if (assetTabSelected == 'mobile_assets') {
                url = '/mobile-assets/search';
            } else {
                url = '/it-assets';
            }

            var queryParameters = {
                searchText: searchText,
                general_type: generalType,
                asset_tab_selected: assetTabSelected
            };

            window.location.href = url + '?' + $.param(queryParameters);
        }
    });

    // var currentInput; // Declare currentInput variable outside
    // $(".search-asset-input").autocomplete({
    //     source: function (request, response) {
    //         var currentInput = $(this); // Get the current input element
    //         // var type = $('.general_type').val();
    //         var type = currentInput.closest('form').find('.general_type').val();
    //         $.ajax({
    //             url: '/get-search-fields',
    //             dataType: "json",
    //             data: {
    //                 search: request.term,
    //                 type: type,
    //             },
    //             success: function (data) {
    //                 response($.map(data, function (item) {
    //                     return {
    //                         label: item.label,
    //                         value: item.value,
    //                         asset_type_tab:item.asset_type_tab
    //                     }
    //                 }));
    //             }
    //         });
    //     },
    //     minLength: 1,
    //     select: function (event, ui) {
    //         event.preventDefault();
    //         // $(".search-asset-input").val(ui.item.label);
    //         // $(".asset_tab_selected").val(ui.item.asset_type_tab);
    //         // $('.search-asset-form-submit').trigger("click");
    //         console.log(currentInput);
    //         var form = currentInput.closest('div.input-group').closest('form'); // Find the parent form
    //         form.find(".search-asset-input").val(ui.item.label);
    //         form.find(".asset_tab_selected").val(ui.item.asset_type_tab);
    //         form.find('.search-asset-form-submit').trigger("click");
    //     }

    // });


    // $(document).on("click",".search-asset-form-submit",function(e){
    //     e.preventDefault();
    //     if($(".search-asset-input").val()){
    //         if($(".asset_tab_selected").val()=='mobile_assets'){
    //             window.location.href ='/mobile-assets?searchText='+$(".search-asset-input").val()+'&general_type='+$(".general_type").val()+'&asset_tab_selected='+$(".asset_tab_selected").val()
    //         }else{
    //             window.location.href ='/it-assets?searchText='+$(".search-asset-input").val()+'&general_type='+$(".general_type").val()+'&asset_tab_selected='+$(".asset_tab_selected").val()
    //         }
    //     }

    // })

    var format = <?php echo json_encode($format); ?>;

    $('#data_1 .input-group.date').datepicker({
        todayBtn: "linked",
        format: format,
        clearBtn:true,
    });
</script><?php /**PATH /var/www/html/nebius/resources/views/partials/global-search-asset-scripts.blade.php ENDPATH**/ ?>