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

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


$(".product-select-two").select2({
    ajax: {
        url: mpPanelBaseUrl + "/product-request",
        type: "post",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: $.trim(params.term)
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    // formatInputTooShort:,
    language: {
        inputTooShort: function () {
            return "جستجو در محصولات تکی";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    dir: "rtl",
    minimumInputLength: 0,
    templateResult: formatPost,
    templateSelection: formatPostSelection
});


$(".physical-product-select-two").select2({
    ajax: {
        url: mpPanelBaseUrl + "/physical-product-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: formatPost,
    templateSelection: formatPostSelection
});

$(".physical-product-full-data-select-two").select2({
    ajax: {
        url: mpPanelBaseUrl + "/physical-product-full-data-request",
        type: "post",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: $.trim(params.term)
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: false
    },
    language: {
        inputTooShort: function () {
            return "جستجو در محصولات فروشگاهی";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    dir: "rtl",
    minimumInputLength: 0,
    templateResult: formatPost,
    templateSelection: formatPostSelection
});


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


$(".all-post-but-pp-select-two").select2({
    ajax: {
        url: mpPanelBaseUrl + "/all-post-but-pp-request",
        type: "post",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: $.trim(params.term)
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    // formatInputTooShort:,
    language: {
        inputTooShort: function () {
            return "جستجو در پست‌ها، برگه‌ها، محصولات‌تکی";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    dir: "rtl",
    minimumInputLength: 0,
    templateResult: formatPost,
    templateSelection: formatPostSelection
});


function formatPost(post) {
    if (post.loading) {
        return post.text;
    }
    let $container = $(
        "<div class='select2-result clearfix'>" +
        "<div class='select2-result-id'></div>" +
        "<div class='select2-result-image'><img src='" + mpPanelBaseUrl + "/core/_dist/img/default_image2.jpg" + "'/></div>" +
        "<div class='select2-result-meta'>" +
        "<div class='select2-result-text'></div>" +
        "<div class='select2-result-summary'></div>" +
        "</div>" +
        "</div>"
    );

    $container.find(".select2-result-id").text(post.id);
    $container.find(".select2-result-text").text(post.text);
    $container.find(".select2-result-summary").text(post.summary);
    if (post.image != "")
        $container.find(".select2-result-image img").attr("src", post.image);

    return $container;
}

function formatPostSelection(post) {
    return post.text;
}