let quizOptionsTextAreaSelectorID = null;
let quizAnswersTextAreaSelectorID = null;

function jsonQuizQRowHtmlMacker(json, keys) {

    let _q_num = (json != null ? json['q_num'] : 0);
    let _q_ans = 0;
    $.each(keys, function (key, value) {
        if (_q_num == value['q']) {
            _q_ans = (value['a'] != "") ? parseInt(value['a']) : 0;
        }
    });

    let htmlRow = "";
    htmlRow += '<div class="json_quizq_card card card-outline card-json-item collapsed-card global-json-handler-card">';
    htmlRow += '<div class="card-header json_quizq_box">';
    htmlRow += '<div class="my-handle"><i class="ph ph-caret-up-down"></i></div>';
    htmlRow += '<div class="textcontainer">';
    htmlRow += '<span class="json_quizq_showtext" >' + (json != null ? json['question'] : "-") + '</span><br>';
    htmlRow += '<span class="quizq_options_number" >تعداد گزینه‌ها: '+(json != null ? ($.isArray(json['options']) ? json['options'].length : '0') : "0")+'</span> ';
    htmlRow += '<span class="quizq_answer_index '+((_q_ans==0)?"index_unknown":"index_known")+'" >'+((_q_ans==0)?'پاسخ نامشخص':'پاسخ: گزینه '+_q_ans)+'</span>';
    htmlRow += '</div>';
    htmlRow += '<div class="card-tools">';
    htmlRow += '<button type="button" class="json_handler_box_opener_btn btn btn-primary btn-sm"><i class="ph ph-pencil"></i></button>';
    htmlRow += '<button type="button" class="quizq_delete_btn btn btn-danger btn-sm"><i class="ph ph-trash"></i></button>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    htmlRow += '<div class="card-body" style="display: none;">';
    htmlRow += '<span>صورت سوال: </span><textarea class="js_quizq_question rtl-right" rows="2">' + (json != null ? json['question'] : "") + '</textarea>';
    
    let no_image_style = 'style="display:none"';
    let has_image_style = '';
    let json_image_url = json != null ? json['img'] : "";
    if(json_image_url == ""){
        no_image_style = '';
        has_image_style = 'style="display:none"';
    }
    let imgClass = 'js_quizq_img';
    //let counter = json['id'];
    let counter = randomUID(21);
    htmlRow += '<span>تصویر برای سوال: </span><div class="image_url_box super-file-select-box""><input id="cat-image-' + counter + '" type="text" class="'+imgClass+' form-control ltr mppanel_image_url" value="' + json_image_url + '">';
    htmlRow += '<div class="sfsb-wrapper">';
    htmlRow += '<div class="section-right"><div '+has_image_style+' class="image-wrapper"><a class="img-preview-a" target="_blank" href="'+json_image_url+'"><img class="img-preview" src="'+json_image_url+'"/></a></div><div '+no_image_style+' class="dropzone general_file_dropzone"></div></div>';
    htmlRow += '<div class="section-left"><span class="ffwrapper">فرمت‌های مورد قبول:<br><span class="fileformats">.png,.gif,.jpg,.jpeg,.webp</span></span><span '+has_image_style+' class="btn-remove-image">حذف و انتخاب مجدد</span><span '+no_image_style+' class="btn-select-image">انتخاب از مدیریت فایل</span></div>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    
    htmlRow += '<span>آدرس ویس برای این سوال: </span><input type="text" class="js_quizq_voice form-control" value="' + (json != null ? json['voice'] : "") + '">';
    let identifier = uid();
    htmlRow += '<textarea style="display:none;" id="quizq_options_' + identifier + '" class="js_quizq_options top15" rows="3">' + (json != null ? JSON.stringify(json['options']) : "[]") + '</textarea>';
    htmlRow += '<textarea style="display:none;" id="quizq_answers_' + identifier + '" class="quizq_answer top15" rows="3">' + _q_ans + '</textarea>';
    htmlRow += '<div class="optionsbox"><span style="margin-left: 20px;">تعداد گزینه‌های تعریف شده: <span class="ocounter">' + (json != null ? ($.isArray(json['options']) ? json['options'].length : '0') : "0") + '</span></span><button class="btn btn-primary btn-sm quiz_options_edit">ویرایش گزینه‌ها</button></div>';
    htmlRow += '<span class="minitext">بعد از ایجاد تغییرات بروی این آیتم، بروی ثبت تغییرات کلیک کنید. دقت کنید ثبت تغییرات به معنی ذخیره در سرور نمیباشد</span>';
    htmlRow += '<div class="local_submit_parent">';
    htmlRow += '<span class="json_quizq_submit_array btn btn-primary btn-sm">ثبت تغییرات</span>';
    htmlRow += '<span class="json_quizq_cancel btn btn-gray btn-sm">انصراف</span>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    htmlRow += '</div>';

    return htmlRow;
}



function jsonQuizQOptionRowHtmlMacker(json, is_answer) {
    let htmlRow = "";
    htmlRow += '<div class="json_quizq_option_card card card-outline card-json-item collapsed-card global-json-handler-card">';
    htmlRow += '<div style="text-align:right" class="card-header json_quizq_option_box">';
    htmlRow += '<div class="my-handle"><i class="ph ph-caret-up-down"></i></div>';
    htmlRow += '<div style="display:inline-block;" class="sorttextcontainer">';
    htmlRow += '<span class="json_quizq_option_showtext" >' + (json != null ? json['title'] : "-") + '</span><br>';
    htmlRow += '</div>';
    htmlRow += '<div class="card-tools">';
    htmlRow += '<button type="button" class="json_handler_box_opener_btn btn btn-primary btn-sm"><i class="ph ph-pencil"></i></button>';
    htmlRow += '<button type="button" class="quizq_options_delete_btn btn btn-danger btn-sm"><i class="ph ph-trash"></i></button>';
    htmlRow += '<button type="button" class="quizq_options_select_answer_btn quiz-key-default btn btn-secondary btn-sm ' + (is_answer ? "quiz-key-selected" : "") + '"><i class="la la-check"></i></button>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    htmlRow += '<div class="card-body" style="display: none;text-align:right;">';
    htmlRow += '<span>صورت گزینه پاسخ: </span><textarea class="js_quizq_option_title rtl-right" rows="2">' + (json != null ? json['title'] : "") + '</textarea>';
    
    let no_image_style = 'style="display:none"';
    let has_image_style = '';
    let json_image_url = json != null ? json['img'] : "";
    if(json_image_url == ""){
        no_image_style = '';
        has_image_style = 'style="display:none"';
    }
    let imgClass = 'js_quizq_option_img';
    //let counter = json['id'];
    let counter = randomUID(21);
    htmlRow += '<span>تصویر / گیف برای گزینه پاسخ: </span><div class="image_url_box super-file-select-box""><input id="cat-image-' + counter + '" type="text" class="'+imgClass+' form-control ltr mppanel_image_url" value="' + json_image_url + '">';
    htmlRow += '<div class="sfsb-wrapper">';
    htmlRow += '<div class="section-right"><div '+has_image_style+' class="image-wrapper"><a class="img-preview-a" target="_blank" href="'+json_image_url+'"><img class="img-preview" src="'+json_image_url+'"/></a></div><div '+no_image_style+' class="dropzone general_file_dropzone"></div></div>';
    htmlRow += '<div class="section-left"><span class="ffwrapper">فرمت‌های مورد قبول:<br><span class="fileformats">.png,.gif,.jpg,.jpeg,.webp</span></span><span '+has_image_style+' class="btn-remove-image">حذف و انتخاب مجدد</span><span '+no_image_style+' class="btn-select-image">انتخاب از مدیریت فایل</span></div>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    
    htmlRow += '<span class="minitext">بعد از ایجاد تغییرات بروی این آیتم، بروی ثبت تغییرات کلیک کنید. دقت کنید ثبت تغییرات به معنی ذخیره در سرور نمیباشد</span>'; 
    htmlRow += '<div class="local_submit_parent">';
    htmlRow += '<span class="json_quizq_option_submit_array btn btn-primary btn-sm">ثبت تغییرات</span>';
    htmlRow += '<span class="json_quizq_option_cancel btn btn-gray btn-sm">انصراف</span>';
    htmlRow += '</div>';
    htmlRow += '</div>';
    htmlRow += '</div>';

    return htmlRow;

}



const jsonQuizQCancelBtn = function () {

    let root_of_current = $(this).parent().parent().parent().parent().parent();
    let card = root_of_current.find('.json_quizq_card');
    root_of_current.find('.json_handler_box_opener_btn').show();
    card.addClass("collapsed-card");
    let card_body = card.find('.card-body');
    card_body.slideUp();

};

const jsonQuizQOptionCancelBtn = function () {

    //let root_of_current = $(this).parent().parent().parent().parent().parent();
    let card = $('.json_handler_quizq_options_array').find('.json_quizq_option_card');
    //root_of_current.find('.json_handler_box_opener_btn').show();
    card.find('.json_handler_box_opener_btn').show();
    card.addClass("collapsed-card");
    let card_body = card.find('.card-body');
    card_body.slideUp();

};


const deleteQuizQRowsBtn = function () {


    const root_card = $(this).parent().parent().parent();
    const sorable_list = $(this).parent().parent().parent().parent();


    Swal.fire({
        title: 'این آیتم را حذف میکنید؟',
        icon: '',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#007bff',
        confirmButtonText: 'حذف آیتم',
        cancelButtonText: 'انصراف'
    }).then((result) => {
        if (result.isConfirmed) {


            root_card.remove();

            if (sorable_list.children().length == 0) {
                const htmlrow = ThereIsNoRowHereHtml();
                $(".json_handler_quizq_array").find(".showbox").find("#sorable_list").html(htmlrow);
            }

            //refresh textarea data:
            jsonQuizQArraySubmitBtnHandler(false, null);


        }

    })


};

const deleteQuizOptionsRowsBtn = function () {

    const root_card = $(this).parent().parent().parent();
    const sorable_list = $(this).parent().parent().parent().parent();

    Swal.fire({
        title: 'این گزینه پاسخ را حذف میکنید؟',
        icon: '',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#007bff',
        confirmButtonText: 'حذف گزینه',
        cancelButtonText: 'انصراف'
    }).then((result) => {
        if (result.isConfirmed) {


            root_card.remove();

            if (sorable_list.children().length == 0) {
                const htmlrow = ThereIsNoRowHereHtml();
                $(".json_handler_quizq_options_array").find(".showbox").find("#sorable_list").html(htmlrow);
            }

            //todo
            //refresh textarea data:
            //jsonQuizQArraySubmitBtnHandler(false,null);


        }
    })
};

const selectAnswerQuizOptionsRowsBtn = function () {

    const root_card = $(this).parent().parent().parent();
    const sorable_list = $(this).parent().parent().parent().parent();
    let select_btns = sorable_list.find('.quizq_options_select_answer_btn');
    for (var i = 0; i < select_btns.length; i++) {
        $(select_btns[i]).removeClass("quiz-key-selected");
    }
    $(this).addClass("quiz-key-selected");

    let selectedIndex = 0;
    for (var i = 0; i < select_btns.length; i++) {
        if (select_btns[i].classList.contains('quiz-key-selected')) {
            selectedIndex = i + 1;
        }
    }

    notyf.open({
        type: 'info',
        message: 'گزینه '+selectedIndex+' به عنوان پاسخ صحیح مشخص گردید.'
    });
};


function jsonQuizQArraySubmitBtn(event) {

    let submitFromSingleCard = event.data.submitFromSingleCard;

    //https://stackoverflow.com/a/9467172/4565796
    let card = $(this).parent().parent().parent();
    jsonQuizQArraySubmitBtnHandler(submitFromSingleCard, card);

};


function jsonQuizQArraySubmitBtnHandler(submitFromSingleCard, card) {

    let root_of_current = $('.json_handler_quizq_array');
    let textareaquestion = root_of_current.find('#quiz_data_questions');
    let textareaanswer = root_of_current.find('#quiz_data_keys');
    const sortableList = root_of_current.find('#sorable_list');
    root_of_current.find('.json_handler_box_opener_btn').show();

    if (submitFromSingleCard) {
        //something in a single card changed! so update card header and close current card
        let _question = card.find('.js_quizq_question').val();
        card.find('.json_quizq_showtext').html(_question);
        card.addClass("collapsed-card");
        let card_body = card.find('.card-body');
        card_body.slideUp();
    }

    let arr = [];
    let arrAns = [];
    sortableList.children().each(function (i, li) {

        let qnumber = i + 1;

        const card = $(li);
        let _question = card.find('.js_quizq_question').val();
        let _img = card.find('.js_quizq_img').val();
        let _voice = card.find('.js_quizq_voice').val();
        let _options = card.find('.js_quizq_options').val();
        if(!li.classList.contains('no-item-found')){
            let obj = {
                q_num: qnumber,
                question: _question,
                img: _img,
                voice: _voice,
                options: JSON.parse(_options)
            };



            let _answer = card.find('.quizq_answer').val();
            let objAns = {
                q: qnumber.toString(),
                a: _answer
            };


                arr[i] = obj;
                arrAns[i] = objAns;
        }
    });



    let stringjs = JSON.stringify(arr);
    textareaquestion.html(stringjs);

    let stringjsanswer = JSON.stringify(arrAns);
    textareaanswer.html(stringjsanswer);

    notyf.open({
        type: 'info',
        message: 'تغییرات آرایه ای سوالات ثبت شد'
    });

};







function jsonQuizQOptionArraySubmitBtn(event) {


    let submitFromSingleCard = event.data.submitFromSingleCard;
    //https://stackoverflow.com/a/9467172/4565796
    let card = $(this).parent().parent().parent();
    jsonQuizQOptionArraySubmitBtnHandler(submitFromSingleCard, card);

};


function jsonQuizQOptionArraySubmitBtnHandler(submitFromSingleCard, card) {

    let root_of_current = $('.json_handler_quizq_options_array');
    let textarea = root_of_current.find('.json_text_area');
    const sortableList = root_of_current.find('#sorable_list');
    root_of_current.find('.json_handler_box_opener_btn').show();

    if (submitFromSingleCard) {
        //something in a single card changed! so update card header and close current card
        let _option_title = card.find('.js_quizq_option_title').val();
        card.find('.json_quizq_option_showtext').html(_option_title);
        card.addClass("collapsed-card");
        let card_body = card.find('.card-body');
        card_body.slideUp();
    }

    let arr = [];
    sortableList.children().each(function (i, li) {

        let onumber = i + 1;

        const card = $(li);            
        let _option_title = card.find('.js_quizq_option_title').val();
        let _img = card.find('.js_quizq_option_img').val();

        let obj = {
            a_num: onumber,
            title: _option_title,
            img: _img
        };

        if(!li.classList.contains('no-item-found')){
            arr[i] = obj; 
        }

    });



    let stringjs = JSON.stringify(arr);

    //console.log("quiz_q json = " + stringjs);

    textarea.val(stringjs);


    notyf.open({
        type: 'info',
        message: 'تغییرات گزینه های پاسخ ثبت شد'
    });

};

const quizOptionsEditBtn = function () {

    let root_of_current = $(this).parent().parent(); //card body
    let options_textarea = root_of_current.find('.js_quizq_options');
    let key_textarea = root_of_current.find('.quizq_answer');
    quizOptionsTextAreaSelectorID = options_textarea.attr('id');
    quizAnswersTextAreaSelectorID = key_textarea.attr('id');

    let json = options_textarea.val();
    let questionkey = (key_textarea.val() != "") ? parseInt(key_textarea.val()) : 0;
    //todo: open dialog
    //alert('test: ' + quizOptionsTextAreaSelectorID);

    jsonArray = JSON.parse(json);

    htmlToShow = '';
    htmlToShow += '<div class="fancybox_scrollbox">';
    htmlToShow += '<div class="json_handler_quizq_options_array">';
    htmlToShow += '<textarea style="display:none" class="form-control json_text_area" rows="2">' + json + '</textarea>';
    htmlToShow += '<div style="text-align:right"><span class="bold" style="margin-left:15px">مدیریت گزینه های پاسخ</span><button id="add_json_quizq_option_array_item" class="btn btn-sm btn-primary top15 bottom15">افزودن گزینه پاسخ جدید</button></div>';
    htmlToShow += '<div class="showbox">';
    htmlToShow += '<div id="sorable_list" class="list-group col">';

    if (jsonArray.length == 0) {
        const htmlrow = ThereIsNoRowHereHtml();
        htmlToShow += htmlrow;
    } else {
        $.each(jsonArray, function (key, value) {
            const is_answer = (key == (questionkey - 1)) ? true : false;
            const htmlrow = jsonQuizQOptionRowHtmlMacker(value, is_answer);
            htmlToShow += htmlrow;
        });
    }

    htmlToShow += '</div>';
    htmlToShow += '</div>';

    htmlToShow += '<div style="text-align:right"><button id="json_quizq_options_sort_submit" class="btn btn-sm btn-primary top15">ثبت ترتیب</button></div>';

    htmlToShow += '<div style="text-align:left"><button id="json_quizq_options_finish" class="btn btn-sm btn-primary top25">ثبت تغییرات گزینه های پاسخ</button></div>';


    htmlToShow += '</div>';
    htmlToShow += '</div>';

    htmlToShow += '';
    htmlToShow += '';
    htmlToShow += '';


    $.fancybox.open(
        htmlToShow, {
            touch: false,
            afterShow: () => {

                $('.fancybox_scrollbox #sorable_list').sortable({
                    group: 'list',
                    animation: 200,
                    delay: 100, // time in milliseconds to define when the sorting should start
                    delayOnTouchOnly: true, // only delay if user is using touch
                    handle: ".my-handle", // Drag handle selector within list items
                    ghostClass: 'ghost'
                });

                //init sort btn
                $('#json_quizq_options_sort_submit').click({
                    submitFromSingleCard: false
                }, jsonQuizQOptionArraySubmitBtn);


                //all inits of quiz optionss
                $('.json_handler_box_opener_btn').click(jsonHandlersBoxOpener);
                $('.quizq_options_delete_btn').click(deleteQuizOptionsRowsBtn);
                // $('.quizq_options_select_answer_btn').click(selectAnswerQuizOptionsRowsBtn);
                $(".json_handler_quizq_options_array").on("click", ".quizq_options_select_answer_btn", selectAnswerQuizOptionsRowsBtn);

                $('.json_quizq_option_submit_array').off("click");
                //https://stackoverflow.com/a/9467172/4565796
                $('.json_quizq_option_submit_array').click({
                    submitFromSingleCard: true
                }, jsonQuizQOptionArraySubmitBtn);
                $('.json_quizq_option_cancel').click(jsonQuizQOptionCancelBtn);

                $('.mppanel_image_url').on("input", changeImagePreview);
                newGenImageHandler();
                $('#add_json_quizq_option_array_item').click(quizAddOptionRow);

                $('#json_quizq_options_finish').click(function () {

                    //refresh fancybox textarea data:
                    jsonQuizQOptionArraySubmitBtnHandler(false, null);

                    //get options json to transfer
                    let dataToTransfer = $('.json_handler_quizq_options_array').find('.json_text_area').val();

                    let parentOptionsTextarea = $('#' + quizOptionsTextAreaSelectorID);
                    let parentKeyTextarea     = $('#' + quizAnswersTextAreaSelectorID);
                    let answers               = $('.json_handler_quizq_options_array').find(".quizq_options_select_answer_btn");
                    let selectedIndex         = 0;

                    for (let i = 0; i < answers.length; i++) {
                        if (answers[i].classList.contains('quiz-key-selected')) {
                            selectedIndex = i + 1;
                        }
                    }
                    parentKeyTextarea.val(selectedIndex);
                    //refresh options count:
                    const ocount = JSON.parse(dataToTransfer);
                    parentOptionsTextarea.parent().find('.optionsbox').find('.ocounter').html(ocount.length);
                    parentOptionsTextarea.parent().parent().find('.textcontainer').find('.quizq_options_number').html('تعداد گزینه‌ها: '+(ocount.length));

                    const answerPreview = parentOptionsTextarea.parent().parent().find('.textcontainer').find('.quizq_answer_index');
                    if(selectedIndex != 0){
                        answerPreview.removeClass('index_unknown');
                        answerPreview.addClass('index_known');
                        answerPreview.html('پاسخ: گزینه '+selectedIndex);
                    }else{
                        answerPreview.removeClass('index_known');
                        answerPreview.addClass('index_unknown');
                        answerPreview.html('پاسخ نامشخص');
                    }

                    //transfer data:
                    parentOptionsTextarea.val(dataToTransfer);
                    $.fancybox.close();

                });




            }
        }
    );


};





const quizAddOptionRow = function () {

    const root = $(this).parent().parent();
    root.find('.no-item-found').remove();

    const options = root.find('.json_quizq_option_box');

    const htmlrow = jsonQuizQOptionRowHtmlMacker(null, false);
    root.find(".showbox").find("#sorable_list").prepend(htmlrow);

    $('.json_handler_box_opener_btn').click(jsonHandlersBoxOpener);
    $('.quizq_options_delete_btn').click(deleteQuizOptionsRowsBtn);
    $('.json_quizq_option_submit_array').off("click");
    //https://stackoverflow.com/a/9467172/4565796
    $('.json_quizq_option_submit_array').click({
        submitFromSingleCard: true
    }, jsonQuizQOptionArraySubmitBtn);
    $('.json_quizq_option_cancel').click(jsonQuizQOptionCancelBtn);
    $('.mppanel_image_url').on("input", changeImagePreview);
    newGenImageHandler();
    
}















$(function () { // start of document ready:



    //Quiz Question Part: (Json Handler)

    //https://stackoverflow.com/a/3273355/4565796

    if ($(".json_handler_quizq_array")[0]) { //there is json quizq array list:

        const jsonStringQuestions = $.trim($('#quiz_data_questions').val());
        let jsonQuizQArrayObj;

        if (jsonStringQuestions == '') {
            jsonQuizQArrayObj = [];

        } else {
            jsonQuizQArrayObj = JSON.parse(jsonStringQuestions);
        }

        const jsonStringKeys = $.trim($('#quiz_data_keys').val());
        let jsonQuizKArrayObj;

        if (jsonStringKeys == '') {
            jsonQuizKArrayObj = [];

        } else {
            jsonQuizKArrayObj = JSON.parse(jsonStringKeys);
        }

        if (jsonQuizQArrayObj.length == 0) {
            const htmlrow = ThereIsNoRowHereHtml();
            $(".json_handler_quizq_array").find(".showbox").find("#sorable_list").html(htmlrow);
        } else {
            $.each(jsonQuizQArrayObj, function (key, value) {
                //console.log("vs: " + value);
                const htmlrow = jsonQuizQRowHtmlMacker(value, jsonQuizKArrayObj);
                $(".json_handler_quizq_array").find(".showbox").find("#sorable_list").append(htmlrow);
            });
        }

        $('.json_handler_box_opener_btn').click(jsonHandlersBoxOpener);
        $('.quizq_delete_btn').click(deleteQuizQRowsBtn);
        $('.json_quizq_submit_array').off("click");
        //https://stackoverflow.com/a/9467172/4565796
        $('.json_quizq_submit_array').click({
            submitFromSingleCard: true
        }, jsonQuizQArraySubmitBtn);
        $('.json_quizq_cancel').click(jsonQuizQCancelBtn);
        //$('.intent_type_code_input').each(intentNamesPrinter);
        $('.intent_type_code_input').each(function (){intentNamesPrinter($(this));});
        $('.mppanel_image_url').on("input", changeImagePreview);
        newGenImageHandler();

    }




    $('#add_json_quizq_array_item').click(function () {

        const root = $(this).parent();
        root.find('.no-item-found').remove();

        const htmlrow = jsonQuizQRowHtmlMacker(null);
        root.find(".showbox").find("#sorable_list").prepend(htmlrow);

        $('.json_handler_box_opener_btn').click(jsonHandlersBoxOpener);
        $('.quizq_delete_btn').click(deleteQuizQRowsBtn);
        $('.json_quizq_submit_array').off("click");
        //https://stackoverflow.com/a/9467172/4565796
        $('.json_quizq_submit_array').click({
            submitFromSingleCard: true
        }, jsonQuizQArraySubmitBtn);
        $('.json_quizq_cancel').click(jsonQuizQCancelBtn);
        $('.mppanel_image_url').on("input", changeImagePreview);
        newGenImageHandler();
        
    });


    //when save sort click, just submit all childs:
    //https://stackoverflow.com/a/9467172/4565796
    $('#save_json_quizq_array_sort').click({
        submitFromSingleCard: false
    }, jsonQuizQArraySubmitBtn);

    //init
    // There was a problem when clicking this button,which caused to open multiple windows. However,
    // we solved the problem by triggering the click event dependable on its static parent.
    $(".json_handler_quizq_array").on("click", ".quiz_options_edit", quizOptionsEditBtn);
    // $(".json_handler_quizq_array").on("click", ".quizq_options_select_answer_btn", quizOptionsEditBtn);

    $('#quiz_questions_edit_save').click(function () {

        let sendData = {
            id: $.trim($('#id').val()),
            quiz_data: $.trim($('#quiz_data_questions').val()),
            quiz_key: $.trim($('#quiz_data_keys').val())
        }

        RequestSuperHandler(
            $(this), //Current Obj
            'آیا از ذخیره تغییرات سوالات آزمون بروی سرور مطمئنید؟', //Alert Title
            "", //Alert Desc
            1, // alertType -> 1:confirm - 2:delete
            '/update_quiz_data', //Request URL
            "", //Return URL
            sendData
        );

    });

}); //end of jqury doc ready