$('.cat-select-two').each(function (i, obj) {

    let selectDiv = $(this);
    selectDiv.select2({
        ajax: {
            url: mpPanelBaseUrl + "/category-request",
            type: "post",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    search: $.trim(params.term)
                };
            },
            processResults: function (data) {
                return {
                    results: data
                };
            },
            cache: true
        },
        language: {
            inputTooShort: function () {
                return "جستجو در دسته‌بندی‌ها";
            },
            noResults: function () {
                return "جستجو نتیجه‌ای نداشت";
            }
        },
        dir: "rtl",
        minimumInputLength: 0,
        templateResult: formatCat,
        placeholder: "انتخاب دسته بندی",
        templateSelection: formatCatSelection
    });

    let clearBtn = selectDiv.parent().find('.clear-s2');

    selectDiv.on("change", function (e) {
        let selectedValue = selectDiv.val();
        console.log('Selected value :', selectedValue);
        if (selectedValue != 0) {
            clearBtn.show();
        } else {
            clearBtn.hide();
        }
    });

    clearBtn.click(function () {
        selectDiv.val(0);
        selectDiv.trigger('change');
    });

});


function formatCat(cat) {
    if (cat.loading) {
        return cat.text;
    }
    let $container = $(
        "<div class='select2-result clearfix'>" +
        "<div class='select2-result-id'></div>" +
        "<div class='select2-result-image'><img style='opacity:0.3' src='" + mpPanelBaseUrl + "/core/_dist/img/default_image2.jpg" + "'/></div>" +
        "<div class='select2-result-meta'>" +
        "<div style='display:block'>" +
        "<div class='s2crbox'><div class='select2-result-parent'></div><i class='select2-result-parent-arrow ph ph-caret-left'></i><div class='select2-result-text'></div></div>" +
        "<div class='select2-result-summary'></div>" +
        "<div ><span class='select2-result-cat-root'></span>" +
        "<span class='select2-result-cat-dependency'></span></div>" +
        "</div>" +
        "</div>"
    );

    $container.find(".select2-result-id").text(cat.id);
    $container.find(".select2-result-text").text(cat.text);
    if (cat.parent != "") {
        $container.find(".select2-result-parent").text(cat.parent);
    } else {
        $container.find(".select2-result-parent").hide();
        $container.find(".select2-result-parent-arrow").hide();
    }

    $container.find(".select2-result-summary").text(cat.summary);
    if (cat.image != "") {
        $container.find(".select2-result-image img").attr("src", cat.image);
        $container.find(".select2-result-image img").css("opacity", "1");
    }

    if (cat.root != "") {
        $container.find(".select2-result-cat-root").text("ریشه");
        $container.find(".select2-result-cat-root").addClass("root");
    }

    if (cat.dependency != "") {
        $container.find(".select2-result-cat-dependency").text("مستقل");
        $container.find(".select2-result-cat-dependency").addClass("independent");
    }

    return $container;
}

function formatCatSelection(cat) {
    return cat.text;
}



$(".cat-filter-select-two").select2({
    ajax: {
        url: mpPanelBaseUrl + "/category-request",
        type: "post",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: $.trim(params.term)
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    language: {
        inputTooShort: function () {
            return "جستجو در دسته‌بندی‌ها";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    dir: "rtl",
    minimumInputLength: 0,
    templateResult: formatCatFilter,
    templateSelection: formatCatFilterSelection
});

function formatCatFilter(cat) {
    if (cat.loading) {
        return cat.text;
    }
    let $container = $(
        "<div class='select2-result clearfix'>" +
        "<div class='select2-result-meta'>" +
        "<div style='display:block'><div class='select2-result-text'></div>" +
        "<div ><span class='select2-result-cat-root'></span>" +
        "<span class='select2-result-cat-dependency'></span></div>" +
        "</div>" +
        "</div>"
    );

    $container.find(".select2-result-text").text(cat.text);
    if (cat.root != "") {
        $container.find(".select2-result-cat-root").text("ریشه");
        $container.find(".select2-result-cat-root").addClass("root");
    }

    if (cat.dependency != "") {
        $container.find(".select2-result-cat-dependency").text("مستقل");
        $container.find(".select2-result-cat-dependency").addClass("independent");
    }

    return $container;
}

function formatCatFilterSelection(cat) {
    return cat.text;
}