HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/root/proc/self/cwd/nueva/modules/btecommercecopilot/views/js/common-tools.js
/**
 * Ecommerce copilot
 *
 * @author    businesstech.fr <modules@businesstech.fr> - https://www.businesstech.fr/
 * @copyright https://www.businesstech.fr/
 * @license   see file: LICENSE.txt
 *
 *           ____    _______
 *          |  _ \  |__   __|
 *          | |_) |    | |
 *          |  _ <     | |
 *          | |_) |    | |
 *          |____/     |_|
 */


document.addEventListener('DOMContentLoaded', function () {
    $("#product-search_inprogress").addClass('d-none');
    $("#cms-search_inprogress").addClass('d-none');
    $("#category-search_inprogress").addClass('d-none');
    $("#category-search-block").addClass('d-none');
    $("#cms-search-block").addClass('d-none');

    $("#id_product_search").typeWatch({
        captureLength: 2,
        highlight: true,
        wait: 200,
        callback: function () {
            searchProducts();
            $("#product-search_inprogress").removeClass('d-none');
        },
    });

    //handle the category search with typewatch
    $("#id_category_search").typeWatch({
        captureLength: 2,
        highlight: true,
        wait: 200,
        callback: function () {
            searchCategory();
            $("#category-search_inprogress").removeClass('d-none');
        },
    });

    //handle the category search with typewatch
    $("#id_cms_search").typeWatch({
        captureLength: 2,
        highlight: true,
        wait: 200,
        callback: function () {
            searchCms();
            $("#cms-search_inprogress").removeClass('d-none');
        },
    });

    // Function to search for products
    function searchProducts() {
        // AJAX request to search for products
        $.ajax({
            type: "POST",
            url: btEcop.ajaxUrl,
            dataType: "json",
            data: {
                ajax: "1",
                token: $("input[name=token]").val(),
                action: "searchProducts",
                product_search: $("#id_product_search").val(),
            },
            beforeSend: function () {
                // Clear and hide product list before search
                $(".product-list").addClass("hidden").empty();
            },
            success: function (res) {
                // Hide loader
                $(".product-search_inprogress").addClass("hide");

                if (res.found) {
                    // Build select element with search results
                    var $select = $('<select>', {
                        name: "id_product[]",
                        id: "id_product",
                        class: "w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none focus:border-blue-500"
                    }).append($('<option>', {
                        value: "0",
                        text: "--",
                        class: "text-gray-500"
                    }));

                    $.each(res.products, function () {
                        $select.append($('<option>', {
                            value: this.id_product,
                            text: this.name,
                            class: "text-gray-800"
                        }));
                    });

                    // Display product list and append select element
                    $(".product-list").removeClass("hidden").html($select);

                    // Handle product selection
                    $("#id_product").on("change", function () {
                        var selectedProduct = $(this).val();
                        if (selectedProduct !== "0") {
                            $("#elementId").val(selectedProduct);
                        }
                    });
                }
            },
        });
    }

    // Function to search for categories
    function searchCategory() {
        // AJAX request to search for categories
        $.ajax({
            type: "POST",
            url: btEcop.ajaxUrl,
            dataType: "json",
            data: {
                ajax: "1",
                token: $("input[name=token]").val(),
                action: "searchCategory",
                category_search: $("#id_category_search").val(),
            },
            beforeSend: function () {
                // Clear and hide category list before search
                $(".category-list").addClass("hidden").empty();
            },
            success: function (res) {
                // Hide loader
                $(".category-search_inprogress").addClass("hide");

                if (res.found) {
                    // Create select element with search results
                    var $select = $('<select>', {
                        name: "id_category[]",
                        id: "id_category",
                        class: "w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none focus:border-blue-500"
                    }).append($('<option>', {
                        value: "0",
                        text: "--",
                        class: "text-gray-500"
                    }));

                    $.each(res.category, function (_, category) {
                        $select.append($('<option>', {
                            value: category.id_category,
                            text: category.name,
                            class: "text-gray-800"
                        }));
                    });

                    // Display category list and append select element
                    $(".category-list").removeClass("hidden").html($select);

                    // Handle category selection
                    $("#id_category").on("change", function () {
                        var selectedCategory = $(this).val();
                        if (selectedCategory !== "0") {
                            $("#elementId").val(selectedCategory);
                        }
                    });
                }
            },
        });
    }

    // Function to search for CMS pages
    function searchCms() {
        $.ajax({
            type: "POST",
            url: btEcop.ajaxUrl,
            dataType: "json",
            data: {
                ajax: "1",
                token: $("input[name=token]").val(),
                action: "searchCms",
                cms_search: $("#id_cms_search").val(),
            },
            beforeSend: function () {
                // Clear and hide CMS list before search
                $(".cms-list").addClass("hidden").empty();
            },
            success: function (res) {
                // Hide loader
                $(".cms-search_inprogress").addClass("hide");

                if (res.found) {
                    // Create select element with search results
                    var $select = $('<select>', {
                        name: "id_product[]",
                        id: "id_product",
                        class: "w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none focus:border-blue-500"
                    }).append($('<option>', {
                        value: "0",
                        text: "--",
                        class: "text-gray-500"
                    }));

                    $.each(res.pages, function (_, page) {
                        $select.append($('<option>', {
                            value: page.id_cms,
                            text: page.meta_title,
                            class: "text-gray-800"
                        }));
                    });

                    // Display CMS list and append select element
                    $(".cms-list").removeClass("hidden").html($select);

                    // Handle CMS page selection
                    $("#id_product").on("change", function () {
                        var selectedCms = $(this).val();
                        if (selectedCms !== "0") {
                            $("#elementId").val(selectedCms);
                        }
                    });
                }
            },
        });
    }
});