let quizDiv2 = $(".quiz-other-select-two");

quizDiv2.select2({
    ajax: {
        url: mpPanelBaseUrl + "/quiz-request",
        type: "post",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: $.trim(params.term),
                quiz_id: $.trim($('#id').val())
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    language: {
        inputTooShort: function () {
            return "جستجو در آزمونها";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    dir: "rtl",
    minimumInputLength: 0,
    templateResult: formatQuiz,
    templateSelection: formatQuizSelection
});

let quizClearBtn2 = quizDiv2.parent().find('.clear-s2');

quizDiv2.on("change",function(e){
    let selectedValue = quizDiv2.val();
    if(selectedValue == null || selectedValue == 0 || selectedValue == "" ){
        quizClearBtn2.hide();
    }else{
        quizClearBtn2.show();
    }
});

quizClearBtn2.click(function(){
    quizDiv2.val(0);
    quizDiv2.trigger('change');
})








let quizDiv = $(".quiz-select-two");

quizDiv.select2({
    ajax: {
        url: mpPanelBaseUrl + "/quiz-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 "جستجو نتیجه‌ای نداشت";
        }
    },
    minimumInputLength: 0,
    templateResult: formatQuiz,
    templateSelection: formatQuizSelection
});

let quizClearBtn = quizDiv.parent().find('.clear-s2');

quizDiv.on("change",function(e){
    let selectedValue = quizDiv.val();
    if(selectedValue == null || selectedValue == 0 || selectedValue == "" ){
        quizClearBtn.hide();
    }else{
        quizClearBtn.show();
    }
});

quizClearBtn.click(function(){
    quizDiv.val(0);
    quizDiv.trigger('change');
});





function formatQuiz(quiz) {
    if (quiz.loading) {
        return quiz.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>" +
        "</div>"
    );

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

    return $container;
}

function formatQuizSelection(quiz) {
    return quiz.text;
}

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

function formatFilterQuiz(quiz) {
    if (quiz.loading) {
        return quiz.text;
    }
    let $container = $(
        "<div class='select2-result clearfix'>" +
        "<div class='select2-result-meta'>" +
        "<div class='select2-result-text'></div>" +
        "</div>" +
        "</div>"
    );

    $container.find(".select2-result-text").text(quiz.text);
    return $container;
}

function formatFilterQuizSelection(quiz) {
    return quiz.text;
}


$(".active-filter-select-two").select2({
    dir: "rtl",
    language: {
        inputTooShort: function () {
            return "وضعیت تایید";
        },
        noResults: function () {
            return "جستجو نتیجه‌ای نداشت";
        }
    },
    minimumInputLength: 0,
    templateResult: formatConfirm,
    templateSelection: formatConfirmSelection
});

function formatConfirm(item) {
    if (item.loading) {
        return item.text;
    }

    let $container = $(
        "<div class='select2-result clearfix'>" +
        "<div class='select2-result-meta'>" +
        "<div class='select2-result-text'></div>" +
        "<div class='select2-result-summary'></div>" +
        "</div>" +
        "</div>"
    );

    $container.find(".select2-result-text").text(item.text);
    $container.find(".select2-result-summary").text(item.summary);

    return $container;
}

function formatConfirmSelection(item) {
    return item.text;
}