function readDataFromPageAndBuildJsonByType(type) {
    
    let json = {};

    if(type == "title"){

        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['title_data']    = $('#title_data').val();
        json['align']         = $('#align').val();
        json['icon_code']     = $('#icon_code').val();
        json['color']         = $('#color').val();
        json['size']          = $('#size').val();
        json['text_weight']   = $('#text_weight').val();
        json['custom_class']  = $('#custom_class').val();

    }else if(type == "text"){

        let boxObj = {};

        let spansString = $('#spans').val().trim();
        if(
            (!spansString) ||
            (spansString.trim() == '') ||
            (spansString.trim() == "") ||
            (spansString.trim() == "null") ||
            (spansString.trim() == "undefined") ||
            (spansString.trim() == null)
            ) spansString = "[]";

        json['text_data']         = $('#text_data').val();
        json['visible_for']       = $('#visible_for').val();
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['custom_class']      = $('#custom_class').val();
        json['spans']             = JSON.parse(spansString);
        json['line_height']       = "10";
        json['color']             = $('#color').val();
        json['justify']           = $('#justify').val();
        json['load_with_webview'] = "0";
        json['box_active']        = $('#box_active').val();
        json['align']             = $('#align').val();

        boxObj['radius'] = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        // boxObj['background'] = $('#background').val();
        boxObj['background_color'] = $('#background_color').val();
        // boxObj['border_color'] = $('#border_color').val();
        boxObj['padding'] = ($('#padding').val().trim().length > 0) ? $('#padding').val() : "0";
        boxObj['border_width'] = "1";

        json['box'] = boxObj;

    }else if(type == "text2"){

        let boxObj = {};

        let quill = $('#text2-editor').data('quill');
        let delta = quill.getContents();
        let quillString = JSON.stringify(delta);

        //console.log("quillString: " + quillString);

        if(
            (!quillString) ||
            (quillString.trim() == '') ||
            (quillString.trim() == "") ||
            (quillString.trim() == "null") ||
            (quillString.trim() == "undefined") ||
            (quillString.trim() == null)
            ) quillString = "[]";

        json['quill_delta']       = JSON.parse(quillString);
        json['visible_for']       = $('#visible_for').val();
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['custom_class']      = $('#custom_class').val();
        json['line_height']       = "10";
        json['color']             = $('#color').val();
        json['align']             = $('#align').val();
        json['box_active']        = $('#box_active').val();
        json['text_direction']    = $('#text_direction').val();

        boxObj['radius'] = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        boxObj['background_color'] = $('#background_color').val();
        boxObj['padding'] = ($('#padding').val().trim().length > 0) ? $('#padding').val() : "0";
        boxObj['border_width'] = "1";

        json['box'] = boxObj;

    }else if(type == "table"){

        let table_data = $('#table_data').val();

        if(
            (!table_data) ||
            (table_data.trim() == '') ||
            (table_data.trim() == "") ||
            (table_data.trim() == "null") ||
            (table_data.trim() == "undefined") ||
            (table_data.trim() == null)
            ) table_data = "[]";

        json['table_data']       = JSON.parse(table_data);
        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['oddline']          = $('#oddline').val();
        json['header_bold']      = $('#header_bold').val();
        json['icon_code']        = $('#icon_code').val();
        json['border_color']     = $('#border_color').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['text_weight']      = $('#text_weight').val();

    }else if(type == "space"){

        json['height']  = ($('#height').val().trim().length > 0) ? $('#height').val() : "0"; 

    }else if(type == "formloader"){

        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['custom_class']  = $('#custom_class').val();
        json['id']            = ($('#form_id').val().trim().length > 0) ? $('#form_id').val() : "0";

    }else if(type == "pollloader"){

        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['custom_class']  = $('#custom_class').val();
        json['id']            = ($('#poll_id').val().trim().length > 0) ? $('#poll_id').val() : "0";

    }else if(type == "image"){

        json['visible_for']     = $('#visible_for').val();
        json['margin']          = $('#margin').val();
        json['margin_bottom']   = $('#margin_bottom').val();
        json['custom_class']    = $('#custom_class').val();
        json['image_url']       = $('#image_url').val();
        json['radius']          = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['height']          = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']   = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['text']            = $('#text').val();
        json['text_background'] = $('#text_background').val();
        json['text_color']      = $('#text_color').val();
        json['text_gravity']    = $('#text_gravity').val();
        json['intent_type']     = $('#intent_type').val();
        json['intent_data']     = $('#intent_data').val();
        json['size']            = "h2";

    }else if(type == "image_zoom"){

        json['visible_for']     = $('#visible_for').val();
        json['margin']          = $('#margin').val();
        json['margin_bottom']   = $('#margin_bottom').val();
        json['custom_class']    = $('#custom_class').val();
        json['image_zoom_url']  = $('#image_zoom_url').val();
        json['radius']          = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['height']          = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']   = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['insta_type_zoom'] = $('#insta_type_zoom').val();
        json['intent_type']     = $('#intent_type').val();
        json['intent_data']     = $('#intent_data').val();

    }else if(type == "image_slider"){
        
        let slider_data = $('#slider_data').val();

        if(
            (!slider_data) ||
            (slider_data.trim() == '') ||
            (slider_data.trim() == "") ||
            (slider_data.trim() == "null") ||
            (slider_data.trim() == "undefined") ||
            (slider_data.trim() == null)
            ) slider_data = "[]";

        json['slider_data']           = JSON.parse(slider_data);
        json['visible_for']           = $('#visible_for').val();
        json['margin']                = $('#margin').val();
        json['margin_bottom']         = $('#margin_bottom').val();
        json['custom_class']          = $('#custom_class').val();
        json['radius']                = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['height']                = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']         = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['text_color']            = $('#text_color').val();
        json['text_background_color'] = $('#text_background_color').val();
        json['indicator_color']       = $('#indicator_color').val();
        json['indicator']             = $('#indicator').val();
        json['text_weight']           = $('#text_weight').val();

    }else if(type == "image_dual"){

        json['visible_for']       = $('#visible_for').val();
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['custom_class']      = $('#custom_class').val();
        json['image1_url']        = $('#image1_url').val();
        json['image2_url']        = $('#image2_url').val();
        json['radius']            = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['height']            = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']     = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['start_image_share'] = ($('#start_image_share').val().trim().length > 0) ? $('#start_image_share').val() : "0";
        json['space_between']     = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['intent1_type']      = $('#intent1_type').val();
        json['intent1_data']      = $('#intent1_data').val();
        json['intent2_type']      = $('#intent2_type').val();
        json['intent2_data']      = $('#intent2_data').val();

    }else if(type == "button"){

        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['button_data']      = $('#button_data').val();
        json['background_color'] = $('#background_color').val();
        json['icon_code']        = $('#icon_code').val();
        json['color']            = $('#color').val();
        json['radius']           = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['intent_type']      = $('#intent_type').val();
        json['intent_data']      = $('#intent_data').val();
        json['btn_full_width']   = $('#btn_full_width').val();
        json['gravity']          = $('#gravity').val();
        json['padding']          = ($('#padding').val().trim().length > 0) ? $('#padding').val() : "0";

    }else if(type == "button_dual"){

        json['visible_for']       = $('#visible_for').val();
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['custom_class']      = $('#custom_class').val();
        json['button1_data']      = $('#button1_data').val();
        json['button2_data']      = $('#button2_data').val();
        json['background1_color'] = $('#background1_color').val();
        json['icon1_code']        = $('#icon1_code').val();
        json['color1']            = $('#color1').val();
        json['background2_color'] = $('#background2_color').val();
        json['icon2_code']        = $('#icon2_code').val();
        json['color2']            = $('#color2').val();
        json['radius']            = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['intent1_type']      = $('#intent1_type').val();
        json['intent1_data']      = $('#intent1_data').val();
        json['intent2_type']      = $('#intent2_type').val();
        json['intent2_data']      = $('#intent2_data').val();
        json['btn_full_width']    = $('#btn_full_width').val();
        json['gravity']           = $('#gravity').val();
        json['padding']           = ($('#padding').val().trim().length > 0) ? $('#padding').val() : "0";

    }else if(type == "audio"){

        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['audio_url']        = $('#audio_url').val();
        json['sticky']           = $('#sticky').val();
        json['title']            = $('#title').val();
        json['icon_code']        = $('#icon_code').val();
        json['color']            = $('#color').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['text_weight']      = $('#text_weight').val();
        json['radius']           = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";

    }else if(type == "video"){

        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['custom_class']  = $('#custom_class').val();
        json['video_url']     = $('#video_url').val();
        json['autoplay']      = $('#autoplay').val();
        json['height']        = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile'] = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['radius']        = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";

    }else if (type == "video_aparat"){
        
        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['custom_class']  = $('#custom_class').val();
        json['video_script']  = $('#video_script').val();
        json['height']        = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile'] = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['radius']        = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";

    }else if(type == "line_posts_byids"){

        json['visible_for']                               = $('#visible_for').val();
        json['margin']                                    = $('#margin').val();
        json['margin_bottom']                             = $('#margin_bottom').val();
        json['custom_class']                              = $('#custom_class').val();
        json['text_weight']                               = $('#text_weight').val();
        json['ids']                                       = $('#ids').val();
        json['line_post_first_item_as_image']             = $('#line_post_first_item_as_image').val();
        json['line_post_first_item_as_image_intent_type'] = $('#line_post_first_item_as_image_intent_type').val();
        json['line_post_first_item_as_image_intent_data'] = $('#line_post_first_item_as_image_intent_data').val();
        json['root_background_color']                     = $('#root_background_color').val();
        json['background_color']                          = $('#background_color').val();
        json['line_post_extreme_card_bg_color']           = $('#line_post_extreme_card_bg_color').val();
        json['line_post_extreme_card_txt_color']          = $('#line_post_extreme_card_txt_color').val();
        json['direction']                                 = $('#direction').val();
        json['theme']                                     = ($('#theme').val().trim().length > 0) ? $('#theme').val() : "0";
        json['list_divider']                              = $('#list_divider').val();
        json['inner_root_padding']                        = ($('#inner_root_padding').val().trim().length > 0) ? $('#inner_root_padding').val() : "0";
        json['round_corners']                             = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['space_between']                             = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['grid_width']                                = ($('#grid_width').val().trim().length > 0) ? $('#grid_width').val() : "0";
        json['grid_width_mobile']                         = ($('#grid_width_mobile').val().trim().length > 0) ? $('#grid_width_mobile').val() : "0";
        json['line_post_order']                           = $('#line_post_order').val();
        json['line_post_limit']                           = ($('#line_post_limit').val().trim().length > 0) ? $('#line_post_limit').val() : 12;
        json['aspect_ratio']                              = $('#aspect_ratio').val();

    }else if(type == "line_posts_bycat"){

        json['visible_for']                               = $('#visible_for').val();
        json['margin']                                    = $('#margin').val();
        json['margin_bottom']                             = $('#margin_bottom').val();
        json['custom_class']                              = $('#custom_class').val();
        json['text_weight']                               = $('#text_weight').val();
        json['cat_id']                                    = $('#cat_id').val();
        json['line_post_first_item_as_image']             = $('#line_post_first_item_as_image').val();
        json['line_post_first_item_as_image_intent_type'] = $('#line_post_first_item_as_image_intent_type').val();
        json['line_post_first_item_as_image_intent_data'] = $('#line_post_first_item_as_image_intent_data').val();
        json['root_background_color']                     = $('#root_background_color').val();
        json['background_color']                          = $('#background_color').val();
        json['line_post_extreme_card_bg_color']           = $('#line_post_extreme_card_bg_color').val();
        json['line_post_extreme_card_txt_color']          = $('#line_post_extreme_card_txt_color').val();
        json['direction']                                 = $('#direction').val();
        json['theme']                                     = ($('#theme').val().trim().length > 0) ? $('#theme').val() : "0";
        json['list_divider']                              = $('#list_divider').val();
        json['inner_root_padding']                        = ($('#inner_root_padding').val().trim().length > 0) ? $('#inner_root_padding').val() : "0";
        json['round_corners']                             = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['space_between']                             = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['grid_width']                                = ($('#grid_width').val().trim().length > 0) ? $('#grid_width').val() : "0";
        json['grid_width_mobile']                         = ($('#grid_width_mobile').val().trim().length > 0) ? $('#grid_width_mobile').val() : "0";
        json['line_post_order']                           = $('#line_post_order').val();
        json['line_post_limit']                           = ($('#line_post_limit').val().trim().length > 0) ? $('#line_post_limit').val() : 12;
        json['aspect_ratio']                              = $('#aspect_ratio').val();

    }else if(type == "line_posts_bytype"){

        json['visible_for']                               = $('#visible_for').val();
        json['margin']                                    = $('#margin').val();
        json['margin_bottom']                             = $('#margin_bottom').val();
        json['custom_class']                              = $('#custom_class').val();
        json['text_weight']                               = $('#text_weight').val();
        json['type']                                      = $('#type').val();
        json['line_post_first_item_as_image']             = $('#line_post_first_item_as_image').val();
        json['line_post_first_item_as_image_intent_type'] = $('#line_post_first_item_as_image_intent_type').val();
        json['line_post_first_item_as_image_intent_data'] = $('#line_post_first_item_as_image_intent_data').val();
        json['root_background_color']                     = $('#root_background_color').val();
        json['background_color']                          = $('#background_color').val();
        json['line_post_extreme_card_bg_color']           = $('#line_post_extreme_card_bg_color').val();
        json['line_post_extreme_card_txt_color']          = $('#line_post_extreme_card_txt_color').val();
        json['direction']                                 = $('#direction').val();
        json['theme']                                     = ($('#theme').val().trim().length > 0) ? $('#theme').val() : "0";
        json['list_divider']                              = $('#list_divider').val();
        json['inner_root_padding']                        = ($('#inner_root_padding').val().trim().length > 0) ? $('#inner_root_padding').val() : "0";
        json['round_corners']                             = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['space_between']                             = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['grid_width']                                = ($('#grid_width').val().trim().length > 0) ? $('#grid_width').val() : "0";
        json['grid_width_mobile']                         = ($('#grid_width_mobile').val().trim().length > 0) ? $('#grid_width_mobile').val() : "0";
        json['line_post_order']                           = $('#line_post_order').val();
        json['line_post_limit']                           = ($('#line_post_limit').val().trim().length > 0) ? $('#line_post_limit').val() : 12;
        json['aspect_ratio']                              = $('#aspect_ratio').val();

    }else if(type == "line_cats_bycat"){

        json['visible_for']                               = $('#visible_for').val();
        json['margin']                                    = $('#margin').val();
        json['margin_bottom']                             = $('#margin_bottom').val();
        json['custom_class']                              = $('#custom_class').val();
        json['text_weight']                               = $('#text_weight').val();
        json['cat_id']                                    = $('#cat_id').val();
        json['root_background_color']                     = $('#root_background_color').val();
        json['background_color']                          = $('#background_color').val();
        json['line_post_extreme_card_bg_color']           = $('#line_post_extreme_card_bg_color').val();
        json['line_post_extreme_card_txt_color']          = $('#line_post_extreme_card_txt_color').val();
        json['direction']                                 = $('#direction').val();
        json['list_divider']                              = $('#list_divider').val();
        json['inner_root_padding']                        = ($('#inner_root_padding').val().trim().length > 0) ? $('#inner_root_padding').val() : "0";
        json['round_corners']                             = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['space_between']                             = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['grid_width']                                = ($('#grid_width').val().trim().length > 0) ? $('#grid_width').val() : "0";
        json['grid_width_mobile']                         = ($('#grid_width_mobile').val().trim().length > 0) ? $('#grid_width_mobile').val() : "0";
        json['aspect_ratio']                              = $('#aspect_ratio').val();

    }else if (type == "post_slider"){
        
        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['text_weight']      = $('#text_weight').val();
        json['cat_id']           = $('#cat_id').val();
        json['background_color'] = $('#background_color').val();
        json['text_color']       = $('#text_color').val();
        json['direction']        = $('#direction').val();
        json['img_height']       = ($('#img_height').val().trim().length > 0) ? $('#img_height').val() : "0";
        json['img_height_mobile'] = ($('#img_height_mobile').val().trim().length > 0) ? $('#img_height_mobile').val() : "0";
        json['round_corners']    = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['line_post_order']  = $('#line_post_order').val();
        json['line_post_limit']  = ($('#line_post_limit').val().trim().length > 0) ? $('#line_post_limit').val() : "0";

    }else if (type == "html"){
        
        json['visible_for']   = $('#visible_for').val();
        json['margin']        = $('#margin').val();
        json['margin_bottom'] = $('#margin_bottom').val();
        json['code']          = $('#code').val();

    }else if (type == "timer"){
        
        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['ending_date']      = $('#ending_date').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['icon_code']        = $('#icon_code').val();
        json['round_corners']    = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['text_weight']      = $('#text_weight').val();

    }else if (type == "notif"){
        
        json['visible_for']      = $('#visible_for').val();
        json['text']             = $('#text').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['direction']        = $('#direction').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['icon_code']        = $('#icon_code').val();
        json['intent_type']      = $('#intent_type').val();
        json['intent_data']      = $('#intent_data').val();
        json['round_corners']    = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['text_weight']      = $('#text_weight').val();

    }else if (type == "account"){

        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['direction']        = $('#direction').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['icon_code']        = $('#icon_code').val();
        json['round_corners']    = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['text_weight']      = $('#text_weight').val();

    }else if (type == "date"){

        json['visible_for']      = $('#visible_for').val();
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['direction']        = $('#direction').val();
        json['text_color']       = $('#text_color').val();
        json['background_color'] = $('#background_color').val();
        json['icon_code']        = $('#icon_code').val();
        json['round_corners']    = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['text_weight']      = $('#text_weight').val();

    }else if (type == "text_button"){

        json['visible_for']           = $('#visible_for').val();
        json['margin']                = $('#margin').val();
        json['margin_bottom']         = $('#margin_bottom').val();
        json['custom_class']          = $('#custom_class').val();
        json['text']                  = $('#text').val();
        json['button_text']           = $('#button_text').val();
        json['button_color']          = $('#button_color').val();
        json['intent_type']           = $('#intent_type').val();
        json['intent_data']           = $('#intent_data').val();
        json['margin_bottom']         = $('#margin_bottom').val();
        json['direction']             = $('#direction').val();
        json['text_color']            = $('#text_color').val();
        json['root_background_color'] = $('#root_background_color').val();
        json['inner_root_padding']    = $('#inner_root_padding').val();
        json['icon_code']             = $('#icon_code').val();
        json['round_corners']         = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['gravity']               = $('#gravity').val();
        json['text_weight']           = $('#text_weight').val();

    }else if (type == "text_image"){

  
        json['visible_for']           = $('#visible_for').val();
        json['margin']                = $('#margin').val();
        json['margin_bottom']         = $('#margin_bottom').val();
        json['custom_class']          = $('#custom_class').val();
        json['image_url']             = $('#image_url').val();
        json['radius']                = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['image_pos']             = $('#image_pos').val();
        json['height']                = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']         = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['start_image_share']     = ($('#start_image_share').val().trim().length > 0) ? $('#start_image_share').val() : "0";
        json['space_between']         = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['ver_arrange_on_mobile'] = $('#ver_arrange_on_mobile').val();
        json['intent_type']           = $('#intent_type').val();
        json['intent_data']           = $('#intent_data').val();
        json['text']                  = $('#text').val();
        json['text_color']            = $('#text_color').val();
        json['text_justify']          = $('#text_justify').val();
        json['direction']             = $('#direction').val();
        json['text_weight']           = $('#text_weight').val();

    }else if (type == "map"){

        json['visible_for']      = $('#visible_for').val();
        json['default_location'] = $('#default_location').val();
        json['title']            = $('#title').val();
        json['default_zoom']     = $('#default_zoom').val();
        json['radius']           = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['margin']           = $('#margin').val();
        json['margin_bottom']    = $('#margin_bottom').val();
        json['custom_class']     = $('#custom_class').val();
        json['height']           = ($('#height').val().trim().length > 0) ? $('#height').val() : "0";
        json['height_mobile']    = ($('#height_mobile').val().trim().length > 0) ? $('#height_mobile').val() : "0";
        json['text_weight']      = $('#text_weight').val();

    }else if (type == "grid"){

        let grid_data = $('#grid_data').val();

        if(
            (!grid_data) ||
            (grid_data.trim() == '') ||
            (grid_data.trim() == "") ||
            (grid_data.trim() == "null") ||
            (grid_data.trim() == "undefined") ||
            (grid_data.trim() == null)
            ) grid_data = "[]";


        json['visible_for']       = $('#visible_for').val();
        json['custom_class']      = $('#custom_class').val();
        json['theme']             = ($('#theme').val().trim().length > 0) ? $('#theme').val() : "0";
        json['grid_data']         = JSON.parse(grid_data);
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['direction']         = $('#direction').val();
        json['text_color']        = $('#text_color').val();
        json['space_between']     = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['round_corners']     = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['grid_width']        = ($('#grid_width').val().trim().length > 0) ? $('#grid_width').val() : "0";
        json['grid_width_mobile'] = ($('#grid_width_mobile').val().trim().length > 0) ? $('#grid_width_mobile').val() : "0";
        json['gravity']           = $('#gravity').val();
        json['text_weight']       = $('#text_weight').val();
        json['aspect_ratio']      = $('#aspect_ratio').val();

    }else if(type == "image_quad") {

        let img_quad_data = $('#img_quad_data').val();

        if(
            (!img_quad_data) ||
            (img_quad_data.trim() == '') ||
            (img_quad_data.trim() == "") ||
            (img_quad_data.trim() == "null") ||
            (img_quad_data.trim() == "undefined") ||
            (img_quad_data.trim() == null)
            ) img_quad_data = "[]";

        json['visible_for']       = $('#visible_for').val();
        json['img_quad_data']     = JSON.parse(img_quad_data);
        json['margin']            = $('#margin').val();
        json['margin_bottom']     = $('#margin_bottom').val();
        json['custom_class']      = $('#custom_class').val();
        json['direction']         = $('#direction').val();
        json['text_color']        = $('#text_color').val();
        json['background_color']  = $('#background_color').val();
        json['img_height']        = ($('#img_height').val().trim().length > 0) ? $('#img_height').val() : "0";
        json['img_height_mobile'] = ($('#img_height_mobile').val().trim().length > 0) ? $('#img_height_mobile').val() : "0";
        json['space_between']     = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['round_corners']     = ($('#round_corners').val().trim().length > 0) ? $('#round_corners').val() : "0";
        json['text_padding']      = $('#text_padding').val();
        json['text_weight']       = $('#text_weight').val();

    }else if (type == "group"){

        let gcol1_data = $('#group_col1_data').val();

        if(
            (!gcol1_data) ||
            (gcol1_data.trim() == '') ||
            (gcol1_data.trim() == "") ||
            (gcol1_data.trim() == "null") ||
            (gcol1_data.trim() == "undefined") ||
            (gcol1_data.trim() == null)
            ) gcol1_data = "[]";

        let gcol2_data = $('#group_col2_data').val();

        if(
            (!gcol2_data) ||
            (gcol2_data.trim() == '') ||
            (gcol2_data.trim() == "") ||
            (gcol2_data.trim() == "null") ||
            (gcol2_data.trim() == "undefined") ||
            (gcol2_data.trim() == null)
            ) gcol2_data = "[]";

        json['visible_for']           = $('#visible_for').val();
        json['custom_class']          = $('#custom_class').val();
        json['group_col1_data']       = JSON.parse(gcol1_data);
        json['group_col2_data']       = JSON.parse(gcol2_data);
        json['radius']                = ($('#radius').val().trim().length > 0) ? $('#radius').val() : "0";
        json['space_between']         = ($('#space_between').val().trim().length > 0) ? $('#space_between').val() : "0";
        json['margin']                = $('#margin').val();
        json['margin_bottom']         = $('#margin_bottom').val();
        json['start_col_share']       = ($('#start_col_share').val().trim().length > 0) ? $('#start_col_share').val() : "50";
        json['gravity']               = $('#gravity').val();
        json['direction']             = $('#direction').val();
        json['divider']               = $('#divider').val();
        json['divider_color']         = $('#divider_color').val();
        json['background_color']      = $('#background_color').val();
        json['inner_root_padding']    = ($('#inner_root_padding').val().trim().length > 0) ? $('#inner_root_padding').val() : "0";
        json['ver_arrange_on_mobile'] = $('#ver_arrange_on_mobile').val();

    }

    

    return json;

}

function editBlockJsonAndSave(type) {
    
    let fullJsonString   = $('#big_json_area').val();
    let json_pos         = $('#json_pos').val();
    let page             = $('#page').val();
    let upper_page       = $('#upper_page').val();
    let page_tag         = $('#page_tag').val();
    let _body_or_preview = $('#body_or_preview').val();
    let _post_id         = $('#id').val();

    let _content_type = "post";
    if(page == "mainui") _content_type = "android-main";
    if(page == "mainuiweb") _content_type = "web-main";
    if(page == "group_column_body_editor"){
        if(upper_page == "mainui") _content_type = "android-main";
        if(upper_page == "mainuiweb") _content_type = "web-main";
    }

    //console.log("_post_id : " + _post_id);

    fullJsonString = convertStringToJsonArrayEmptyStringIfNull(fullJsonString); 
        
    let BigJson = JSON.parse(fullJsonString);

    BigJson[json_pos][type] = readDataFromPageAndBuildJsonByType(type);

    //group mode datas:
    let upperPage    = $('#upper_page').val();
    let groupJsonPos = $('#group_json_pos').val();
    let groupColNum  = $('#group_col_num').val();

    let toSaveOnNormalBigJsonString = "";
    // --- group column handling ---
    if (page_tag === "group") {

        toSaveOnNormalBigJsonString = JSON.stringify(BigJson);
        let upperFullJsonStringForGroups = $('#upper_big_json_area').val();

        let UpperBigJson = JSON.parse(upperFullJsonStringForGroups);

        // Safety: check if the index exists
        if (groupJsonPos < 0 || groupJsonPos >= UpperBigJson.length) {
            notyf.error("Invalid group JSON position");
            return;
        }

        let groupEntry = UpperBigJson[groupJsonPos];
        let keyName = Object.keys(groupEntry)[0];

        if (keyName !== "group") {
            notyf.error("group item not found on groupJsonPos");
            return;
        }

        // Access the 'group' object
        let groupData = groupEntry[keyName];

        if (groupColNum == "1") {
            groupData['group_col1_data'] = BigJson;
        } else if (groupColNum == "2") {
            groupData['group_col2_data'] = BigJson;
        }else{
            notyf.error("Invalid group column number");
            return;
        }

        // Replace in parent structure
        UpperBigJson[groupJsonPos][keyName] = groupData;

        // Finally, replace BigJson with the updated upper group JSON
        BigJson = UpperBigJson;

    }

    let finalRes = JSON.stringify(BigJson);

    //update big json text area:
    if (page_tag === "group") {
        $('#upper_big_json_area').val(finalRes);
        $('#big_json_area').val(toSaveOnNormalBigJsonString);
    }else{
        $('#big_json_area').val(finalRes);
    }

    let _toSave = {
        id             : _post_id,
        body_or_preview: _body_or_preview,
        content_type   : _content_type,
        data           : finalRes
    }

    

    $.ajax({
        type: "POST",
        url: mpPanelBaseUrl + "/update_big_json",
        data: _toSave,
        timeout: 25000,
        cache: false,
        success: function (data) {
            if (data == "ok") {
                notyf.success("تغییرات بلاک ذخیره شد");
                $('#end-of-block-editor').trigger('click');
            } else {
                notyf.error(data);
            }
        },
        error: function(e) {
            notyf.error('Error: ' + e.responseText);
        }
    });
  
}

function emptyJsonObjectBuilderByType(type) {
    
    let itemJson = {};
    let newItemJson = {};

    if(type == "title"){

        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['custom_class']  = "";
        newItemJson['text_weight']   = "400";
        newItemJson['title_data']    = "";
        newItemJson['align']         = "";
        newItemJson['icon_code']     = "";
        newItemJson['color']         = "";
        newItemJson['size']          = "h2";

        itemJson['title'] = newItemJson;

    }else if (type == "audio"){
        
        //we do this for json parsing in android:
        //json key types must be always same (int or string) so here we do check for empty
        //and replace it with 0 if user has not entered anything in number input.

        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['text_weight']      = "400";
        newItemJson['audio_url']        = "";
        newItemJson['sticky']           = "";
        newItemJson['title']            = "";
        newItemJson['icon_code']        = "";
        newItemJson['color']            = "";
        newItemJson['text_color']       = "";
        newItemJson['background_color'] = "";
        newItemJson['radius']           = "6";

        itemJson['audio'] = newItemJson;

    }else if (type == "button_dual"){

        //we do this for json parsing in android:
        //json key types must be always same (int or string) so here we do check for empty
        //and replace it with 0 if user has not entered anything in number input.

        newItemJson['visible_for']       = "all";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['custom_class']      = "";
        newItemJson['button1_data']      = "";
        newItemJson['button2_data']      = "";
        newItemJson['background1_color'] = "";
        newItemJson['icon1_code']        = "";
        newItemJson['color1']            = "#ffffff";
        newItemJson['background2_color'] = "";
        newItemJson['icon2_code']        = "";
        newItemJson['color2']            = "#ffffff";
        newItemJson['radius']            = "6";
        newItemJson['intent1_type']      = "";
        newItemJson['intent1_data']      = "";
        newItemJson['intent2_type']      = "";
        newItemJson['intent2_data']      = "";
        newItemJson['padding']           = "10";

        itemJson['button_dual'] = newItemJson;

    }else if (type == "button"){

        //we do this for json parsing in android:
        //json key types must be always same (int or string) so here we do check for empty
        //and replace it with 0 if user has not entered anything in number input.

        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['button_data']      = "";
        newItemJson['btn_full_width']   = "0";
        newItemJson['gravity']          = "center";
        newItemJson['padding']          = "10";
        newItemJson['background_color'] = "";
        newItemJson['icon_code']        = "";
        newItemJson['color']            = "#ffffff";
        newItemJson['radius']           = "6";
        newItemJson['intent_type']      = "";
        newItemJson['intent_data']      = "";

        itemJson['button'] = newItemJson;

    }else if (type == "image_dual"){

        newItemJson['visible_for']       = "all";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['custom_class']      = "";
        newItemJson['image1_url']        = "";
        newItemJson['image2_url']        = "";
        newItemJson['color1']            = "";
        newItemJson['color2']            = "";
        newItemJson['radius']            = "6";
        newItemJson['height']            = "220";
        newItemJson['height_mobile']     = "190";
        newItemJson['start_image_share'] = "50";
        newItemJson['space_between']     = "15";
        newItemJson['intent1_type']      = "";
        newItemJson['intent1_data']      = "";
        newItemJson['intent2_type']      = "";
        newItemJson['intent2_data']      = "";
        newItemJson['btn_full_width']   = "0";
        newItemJson['gravity']          = "center";
        newItemJson['padding']          = "10";
        
        itemJson['image_dual'] = newItemJson;

    }else if (type == "image_slider"){

        newItemJson['visible_for']           = "all";
        newItemJson['margin']                = "";
        newItemJson['margin_bottom']         = "";
        newItemJson['custom_class']          = "";
        newItemJson['slider_data']           = JSON.parse("[]");
        newItemJson['radius']                = "6";
        newItemJson['height']                = "220";
        newItemJson['height_mobile']         = "180";
        newItemJson['text_color']            = "";
        newItemJson['text_background_color'] = "";
        newItemJson['indicator_color']       = "";
        newItemJson['indicator']             = "";
        newItemJson['text_weight']           = "400";

        itemJson['image_slider'] = newItemJson;

    }else if (type == "image_zoom"){

        newItemJson['visible_for']     = "all";
        newItemJson['margin']          = "";
        newItemJson['margin_bottom']   = "";
        newItemJson['custom_class']    = "";
        newItemJson['image_zoom_url']  = "";
        newItemJson['radius']          = "6";
        newItemJson['height']          = "0";
        newItemJson['height_mobile']   = "0";
        newItemJson['insta_type_zoom'] = "";
        newItemJson['intent_type']     = "";
        newItemJson['intent_data']     = "";

        itemJson['image_zoom'] = newItemJson;

    }else if (type == "image"){

        newItemJson['visible_for']     = "all";
        newItemJson['margin']          = "";
        newItemJson['margin_bottom']   = "";
        newItemJson['custom_class']    = "";
        newItemJson['image_url']       = "";
        newItemJson['radius']          = "6";
        newItemJson['height']          = "0";
        newItemJson['height_mobile']   = "0";
        newItemJson['text']            = "";
        newItemJson['text_background'] = "";
        newItemJson['text_color']      = "";
        newItemJson['text_gravity']    = "";
        newItemJson['intent_type']     = "";
        newItemJson['intent_data']     = "";
        newItemJson['size']            = "h2";

        itemJson['image'] = newItemJson;

    }else if (type == "pollloader"){

        newItemJson['id']            = "0";
        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['custom_class']  = "";

        itemJson   ['pollloader']    = newItemJson;

    }else if (type == "formloader"){

        newItemJson['id']            = "0";
        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['custom_class']  = "";

        itemJson   ['formloader']    = newItemJson;

    }else if (type == "space"){

        newItemJson['height'] = "15";
        itemJson['space'] = newItemJson;

    }else if (type == "table"){

        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['table_data']       = JSON.parse("[]");
        newItemJson['oddline']          = "";
        newItemJson['header_bold']      = "";
        newItemJson['icon_code']        = "";
        newItemJson['border_color']     = "";
        newItemJson['text_color']       = "";
        newItemJson['background_color'] = "";
        newItemJson['text_weight']      = "400";

        itemJson['table'] = newItemJson;

    }else if (type == "text"){

        let boxObj = {};

        newItemJson['visible_for']       = "all";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['custom_class']      = "";
        newItemJson['text_data']         = "";
        newItemJson['spans']             = JSON.parse("[]");
        newItemJson['line_height']       = "10";
        newItemJson['color']             = "";
        newItemJson['justify']           = "";
        newItemJson['load_with_webview'] = "0";
        newItemJson['box_active']        = "";
        newItemJson['align']             = "";

        boxObj['radius'] = "6";
        boxObj['background_color'] = "";
        boxObj['padding'] = "10";
        boxObj['border_width'] = "1";

        newItemJson['box'] = boxObj;

        itemJson['text'] = newItemJson;

    }else if (type == "text2"){

        let boxObj = {};

        newItemJson['visible_for']    = "all";
        newItemJson['margin']         = "";
        newItemJson['margin_bottom']  = "";
        newItemJson['custom_class']   = "";
        newItemJson['quill_delta']    = JSON.parse("[]");
        newItemJson['line_height']    = "10";
        newItemJson['color']          = "";
        newItemJson['box_active']     = "0";
        newItemJson['align']          = "right";
        newItemJson['text_direction'] = "rtl";

        boxObj['radius'] = "6";
        boxObj['background_color'] = "";
        boxObj['padding'] = "10";
        boxObj['border_width'] = "1";

        newItemJson['box'] = boxObj;

        itemJson['text2'] = newItemJson;

    }else if (type == "video"){

        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['custom_class']  = "";
        newItemJson['video_url']     = "";
        newItemJson['autoplay']      = "0";
        newItemJson['height']        = "0";
        newItemJson['height_mobile'] = "0";
        newItemJson['radius']        = "6";

        itemJson['video'] = newItemJson;

    }else if (type == "video_aparat"){
        
        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['custom_class']  = "";
        newItemJson['video_script']  = "";
        newItemJson['height']        = "0";
        newItemJson['height_mobile'] = "0";
        newItemJson['radius']        = "6";

        itemJson['video_aparat'] = newItemJson;

    }else if (type == "line_posts_byids"){
        
        newItemJson['visible_for']                               = "all";
        newItemJson['margin']                                    = "";
        newItemJson['margin_bottom']                             = "";
        newItemJson['custom_class']                              = "";
        newItemJson['text_weight']                               = "400";
        newItemJson['ids']                                       = "";
        newItemJson['line_post_first_item_as_image']             = "";
        newItemJson['line_post_first_item_as_image_intent_type'] = "";
        newItemJson['line_post_first_item_as_image_intent_data'] = "";
        newItemJson['root_background_color']                     = "#ffffff00";
        newItemJson['background_color']                          = "#ffffff";
        newItemJson['line_post_extreme_card_bg_color']           = "";
        newItemJson['line_post_extreme_card_txt_color']          = "";
        newItemJson['direction']                                 = "rtl";
        newItemJson['theme']                                     = "0";
        newItemJson['list_divider']                              = "0";
        newItemJson['inner_root_padding']                        = "10";
        newItemJson['round_corners']                             = "6";
        newItemJson['space_between']                             = "15";
        newItemJson['grid_width']                                = "150";
        newItemJson['grid_width_mobile']                         = "120";
        newItemJson['line_post_order']                           = "date_desc";
        newItemJson['line_post_limit']                           = "12";
        newItemJson['aspect_ratio']                              = "1:1";

        itemJson['line_posts_byids'] = newItemJson;

    }else if (type == "line_posts_bycat"){
        
        newItemJson['visible_for']                               = "all";
        newItemJson['margin']                                    = "";
        newItemJson['margin_bottom']                             = "";
        newItemJson['custom_class']                              = "";
        newItemJson['text_weight']                               = "400";
        newItemJson['cat_id']                                    = "";
        newItemJson['line_post_first_item_as_image']             = "";
        newItemJson['line_post_first_item_as_image_intent_type'] = "";
        newItemJson['line_post_first_item_as_image_intent_data'] = "";
        newItemJson['root_background_color']                     = "#ffffff00";
        newItemJson['background_color']                          = "#ffffff";
        newItemJson['line_post_extreme_card_bg_color']           = "";
        newItemJson['line_post_extreme_card_txt_color']          = "";
        newItemJson['direction']                                 = "rtl";
        newItemJson['theme']                                     = "0";
        newItemJson['list_divider']                              = "0";
        newItemJson['inner_root_padding']                        = "10";
        newItemJson['round_corners']                             = "6";
        newItemJson['space_between']                             = "15";
        newItemJson['grid_width']                                = "150";
        newItemJson['grid_width_mobile']                         = "120";
        newItemJson['line_post_order']                           = "date_desc";
        newItemJson['line_post_limit']                           = "12";
        newItemJson['aspect_ratio']                              = "1:1";
        
        itemJson['line_posts_bycat'] = newItemJson;

    }else if (type == "line_posts_bytype"){
        
        newItemJson['visible_for']                               = "all";
        newItemJson['margin']                                    = "";
        newItemJson['margin_bottom']                             = "";
        newItemJson['custom_class']                              = "";
        newItemJson['text_weight']                               = "400";
        newItemJson['type']                                      = "";
        newItemJson['line_post_first_item_as_image']             = "";
        newItemJson['line_post_first_item_as_image_intent_type'] = "";
        newItemJson['line_post_first_item_as_image_intent_data'] = "";
        newItemJson['root_background_color']                     = "#ffffff00";
        newItemJson['background_color']                          = "#ffffff";
        newItemJson['line_post_extreme_card_bg_color']           = "";
        newItemJson['line_post_extreme_card_txt_color']          = "";
        newItemJson['direction']                                 = "rtl";
        newItemJson['theme']                                     = "0";
        newItemJson['list_divider']                              = "0";
        newItemJson['inner_root_padding']                        = "10";
        newItemJson['round_corners']                             = "6";
        newItemJson['space_between']                             = "15";
        newItemJson['grid_width']                                = "150";
        newItemJson['grid_width_mobile']                         = "120";
        newItemJson['line_post_order']                           = "date_desc";
        newItemJson['line_post_limit']                           = "12";
        newItemJson['aspect_ratio']                              = "1:1";
        
        itemJson['line_posts_bytype'] = newItemJson;

    }else if (type == "line_cats_bycat"){
        
        newItemJson['visible_for']                               = "all";
        newItemJson['margin']                                    = "";
        newItemJson['margin_bottom']                             = "";
        newItemJson['custom_class']                              = "";
        newItemJson['text_weight']                               = "400";
        newItemJson['cat_id']                           = "";
        newItemJson['root_background_color']            = "#ffffff00";
        newItemJson['background_color']                 = "#ffffff";
        newItemJson['line_post_extreme_card_bg_color']  = "";
        newItemJson['line_post_extreme_card_txt_color'] = "";
        newItemJson['direction']                        = "rtl";
        newItemJson['list_divider']                     = "0";
        newItemJson['inner_root_padding']               = "10";
        newItemJson['round_corners']                    = "6";
        newItemJson['space_between']                    = "15";
        newItemJson['grid_width']                       = "150";
        newItemJson['grid_width_mobile']                = "120";
        newItemJson['aspect_ratio']                     = "1:1";

        itemJson['line_cats_bycat'] = newItemJson;

    }else if (type == "post_slider"){
        
        newItemJson['visible_for']       = "all";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['custom_class']      = "";
        newItemJson['text_weight']       = "400";
        newItemJson['cat_id']            = "";
        newItemJson['background_color']  = "#ffffff50";
        newItemJson['text_color']        = "#000000";
        newItemJson['direction']         = "rtl";
        newItemJson['img_height']        = "180";
        newItemJson['img_height_mobile'] = "190";
        newItemJson['round_corners']     = "6";
        newItemJson['line_post_order']   = "date_desc";
        newItemJson['line_post_limit']   = "5";

        itemJson['post_slider'] = newItemJson;

    }else if (type == "html"){
        
        newItemJson['visible_for']   = "all";
        newItemJson['margin']        = "";
        newItemJson['margin_bottom'] = "";
        newItemJson['code']          = "";
        itemJson   ['html']          = newItemJson;

    }else if (type == "timer"){
        
        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['text_weight']      = "400";
        newItemJson['ending_date']      = "";
        newItemJson['text_color']       = "#000000";
        newItemJson['background_color'] = "#ffffff00";
        newItemJson['icon_code']        = "pho-clock-fill";
        newItemJson['round_corners']    = "6";

        itemJson   ['timer']            = newItemJson;

    }else if (type == "notif"){
        
        newItemJson['visible_for']      = "all";
        newItemJson['text']             = "";
        newItemJson['text_color']       = "#000000";
        newItemJson['background_color'] = "#ffc500";
        newItemJson['icon_code']        = "";
        newItemJson['round_corners']    = 6;
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['direction']        = "rtl";
        newItemJson['intent_type']      = "";
        newItemJson['intent_data']      = "";
        newItemJson['text_weight']      = "400";

        itemJson   ['notif']            = newItemJson;

    }else if (type == "account"){
        
        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['text_color']       = "#ffffff";
        newItemJson['background_color'] = "#0985d2";
        newItemJson['icon_code']        = "";
        newItemJson['round_corners']    = "6";
        newItemJson['margin_bottom']    = "0";
        newItemJson['direction']        = "rtl";
        newItemJson['text_weight']      = "400";

        itemJson   ['account']            = newItemJson;

    }else if (type == "date"){
        
        newItemJson['visible_for']      = "all";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['text_color']       = "#3D4451";
        newItemJson['background_color'] = "#B2D9FFB5";
        newItemJson['icon_code']        = "";
        newItemJson['round_corners']    = "6";
        newItemJson['margin_bottom']    = "0";
        newItemJson['direction']        = "rtl";
        newItemJson['text_weight']      = "400";

        itemJson   ['date']            = newItemJson;

    }else if (type == "text_button"){

        newItemJson['visible_for']           = "all";
        newItemJson['margin']                = "";
        newItemJson['margin_bottom']         = "";
        newItemJson['custom_class']          = "";
        newItemJson['text']                  = "";
        newItemJson['button_text']           = "";
        newItemJson['button_color']          = "#141B64";
        newItemJson['text_color']            = "#000000";
        newItemJson['icon_code']             = "";
        newItemJson['round_corners']         = "6";
        newItemJson['margin_bottom']         = "0";
        newItemJson['root_background_color'] = "";
        newItemJson['inner_root_padding']    = "0";
        newItemJson['intent_type']           = "";
        newItemJson['intent_data']           = "";
        newItemJson['direction']             = "rtl";
        newItemJson['gravity']               = "start";
        newItemJson['text_weight']           = "400";

        itemJson   ['text_button']            = newItemJson;

    }else if (type == "text_image"){

        newItemJson['visible_for']           = "all";
        newItemJson['margin']                = "";
        newItemJson['margin_bottom']         = "";
        newItemJson['custom_class']          = "";
        newItemJson['image_url']             = "";
        newItemJson['radius']                = "6";
        newItemJson['height']                = "200";
        newItemJson['height_mobile']         = "170";
        newItemJson['start_image_share']     = "50";
        newItemJson['image_pos']             = "start";
        newItemJson['ver_arrange_on_mobile'] = "0";
        newItemJson['space_between']         = "15";
        newItemJson['intent_type']           = "";
        newItemJson['intent_data']           = "";
        newItemJson['text']                  = "";
        newItemJson['text_color']            = "#000000";
        newItemJson['text_justify']          = "1";
        newItemJson['direction']             = "rtl";
        newItemJson['text_weight']           = "400";

        itemJson   ['text_image']            = newItemJson;

    }else if (type == "map"){

        newItemJson['visible_for']      = "all";
        newItemJson['default_location'] = "";
        newItemJson['title']            = "";
        newItemJson['default_zoom']     = "12";
        newItemJson['radius']           = "6";
        newItemJson['margin']           = "";
        newItemJson['margin_bottom']    = "";
        newItemJson['custom_class']     = "";
        newItemJson['height']           = "200";
        newItemJson['height_mobile']    = "140";
        newItemJson['text_weight']      = "400";

        itemJson   ['map']            = newItemJson;

    }else if (type == "grid"){

        newItemJson['visible_for']       = "all";
        newItemJson['grid_data']         = JSON.parse("[]");
        newItemJson['round_corners']     = "6";
        newItemJson['space_between']     = "15";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['custom_class']      = "";
        newItemJson['theme']             = "0";
        newItemJson['grid_width']        = "140";
        newItemJson['grid_width_mobile'] = "100";
        newItemJson['direction']         = "rtl";
        newItemJson['text_color']        = "#000000";
        newItemJson['gravity']           = "center";
        newItemJson['text_weight']       = "400";
        newItemJson['aspect_ratio']      = "1:1";

        itemJson   ['grid']            = newItemJson;

    }else if (type == "image_quad"){

        newItemJson['visible_for']       = "all";
        newItemJson['custom_class']      = "";
        newItemJson['img_quad_data']     = JSON.parse("[]");
        newItemJson['round_corners']     = "6";
        newItemJson['space_between']     = "15";
        newItemJson['margin']            = "";
        newItemJson['margin_bottom']     = "";
        newItemJson['text_padding']      = "2";
        newItemJson['img_height']        = "190";
        newItemJson['img_height_mobile'] = "150";
        newItemJson['direction']         = "rtl";
        newItemJson['text_color']        = "#000000";
        newItemJson['background_color']  = "#ffffff50";
        newItemJson['text_weight']       = "400";

        itemJson   ['image_quad']            = newItemJson;


    }else if (type == "group"){

        newItemJson['visible_for']           = "all";
        newItemJson['custom_class']          = "";
        newItemJson['group_col1_data']       = JSON.parse("[]");
        newItemJson['group_col2_data']       = JSON.parse("[]");
        newItemJson['radius']                = "8";
        newItemJson['space_between']         = "15";
        newItemJson['margin']                = "";
        newItemJson['margin_bottom']         = "";
        newItemJson['start_col_share']       = "50";
        newItemJson['gravity']               = "top";
        newItemJson['direction']             = "rtl";
        newItemJson['divider']               = "0";
        newItemJson['divider_color']         = "#ABABAB33";
        newItemJson['background_color']      = "";
        newItemJson['inner_root_padding']    = "10";
        newItemJson['ver_arrange_on_mobile'] = "1";

        itemJson   ['group']            = newItemJson;

    }
    
    return itemJson;

}

function buildNewEmptyBlockJsonAndSave(type) {
    
    let itemJson = emptyJsonObjectBuilderByType(type);
    if(Object.keys(itemJson).length == 0){
        notyf.error("unknown type: " + type);
        return
    }
    saveBlockJsonToBigDataAndDatabase(itemJson,"empty-block-adder");

}

function saveBlockJsonToBigDataAndDatabase(itemJson,from) {
    
    let fullJsonString = $('#big-data-blocks-array-text-area').val();

    fullJsonString = convertStringToJsonArrayEmptyStringIfNull(fullJsonString); 

    let BigJson = JSON.parse(fullJsonString);

    let pos = $('#block-add-or-modify-position').attr('data-pos');
    if(pos == "-1"){
        BigJson.push(itemJson);
    }else{
        let newPos = parseInt(pos) + 1;
        BigJson.splice(newPos, 0, itemJson);
    }

    saveBigJsonToDatabaseWithGroupHandling(BigJson,from);
  
}

function saveBigJsonToDatabaseWithGroupHandling(BigJson,from) {

    let successMessage = "تغییرات ذخیره شد";
    if(from == "empty-block-adder") successMessage = "بلاک جدید اضافه شد";
    if(from == "single-block-paster") successMessage = "بلاک جدید کپی شد";
    if(from == "big-json-paster") successMessage = "تمام بلاک‌ها کپی شدند";
    
    let page_tag = $('#page_tag').val();
    let _body_or_preview = $('#body_or_preview').val();
    let _content_type = $('#content_type').val();
    let _post_id = $('#id').val();

    let toSaveOnNormalBigJsonString = "";

    // --- group column handling ---
    if (page_tag === "group") {

        toSaveOnNormalBigJsonString = JSON.stringify(BigJson);
        let upperFullJsonStringForGroups = $('#upper-big-data-blocks-array-text-area-for-groups').val();
        let groupJsonPos = parseInt($('#group_json_pos').val());
        let groupColNum = parseInt($('#group_col_num').val());

        let UpperBigJson = JSON.parse(upperFullJsonStringForGroups);

        // Safety: check if the index exists
        if (groupJsonPos < 0 || groupJsonPos >= UpperBigJson.length) {
            notyf.error("Invalid group JSON position");
            return;
        }

        let groupEntry = UpperBigJson[groupJsonPos];
        let keyName = Object.keys(groupEntry)[0];

        if (keyName !== "group") {
            notyf.error("group item not found on groupJsonPos");
            return;
        }

        // Access the 'group' object
        let groupData = groupEntry[keyName];

        if (groupColNum == "1") {
            groupData['group_col1_data'] = BigJson;
        } else if (groupColNum == "2") {
            groupData['group_col2_data'] = BigJson;
        }else{
            notyf.error("Invalid group column number");
            return;
        }

        // Replace in parent structure
        UpperBigJson[groupJsonPos][keyName] = groupData;

        // Finally, replace BigJson with the updated upper group JSON
        BigJson = UpperBigJson;

    }

    let finalRes = JSON.stringify(BigJson);

    //update big json text area:
    if (page_tag === "group") {
        $('#upper-big-data-blocks-array-text-area-for-groups').val(finalRes);
        $('#big-data-blocks-array-text-area').val(toSaveOnNormalBigJsonString);
    }else{
        $('#big-data-blocks-array-text-area').val(finalRes);
    }

    let _toSave = {
        id: _post_id,
        body_or_preview: _body_or_preview,
        content_type: _content_type,
        data: finalRes
    }

    $.ajax({
        type: "POST",
        url: mpPanelBaseUrl + "/update_big_json",
        data: _toSave,
        timeout: 25000,
        cache: false,
        success: function (data) {
            if (data == "ok") {
                notyf.success(successMessage);
                if(from == "empty-block-adder") {
                    $('#end-of-post-block-add').trigger('click');
                } else if(from == "single-block-paster" || from == "big-json-paster") {
                    $('#end-of-post-json-paster').attr('data-from',from);
                    $('#end-of-post-json-paster').trigger('click');
                }
            } else {
                notyf.error(data);
            }
        },
        error: function(e) {
            notyf.error('Error: ' + e.responseText);
        }
    });
  
}

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


    //edit blocks
    $('#block-editor-save-button').click(function () {

        Swal.fire({
            title: 'از ذخیره تغییرات بلاک مطمئن هستید؟',
            text: '',
            icon: '',
            showCancelButton: true,
            confirmButtonColor: '#007bff',
            cancelButtonColor: '#909090',
            confirmButtonText: 'ذخیره',
            cancelButtonText: 'انصراف'
        }).then((result) => {
            if (result.isConfirmed) {
                let type = $(this).attr('data-type');
                editBlockJsonAndSave(type);
            }
        })
        
    });
    
}); //end of jqury doc ready