	ques_sort_arr = [];
	option_sort_arr = [];
$(function(){
	
	
	function remove_json_characters(stringfy){
				
				stringfy.replace(/\\n/g, "\\n")
						.replace(/\\'/g, "\\'")
						.replace(/\\"/g, '\\"')
						.replace(/\\&/g, "\\&")
						.replace(/\\r/g, "\\r")
						.replace(/\\t/g, "\\t")
						.replace(/\\b/g, "\\b")
						.replace(/\\f/g, "\\f");
					
				return stringfy;
				
			}
			
			
			 //html_editor
			
			 // أزرار التنسيق
			  $(".colorRed").click(function(){alert("S"); document.execCommand("foreColor", false, "red"); });
			  $(".colorBlue").click(function(){ document.execCommand("foreColor", false, "blue"); });
			  $(".addLink").click(function(){
				var url = prompt("أدخل الرابط:");
				if(url){
					document.execCommand("createLink", false, url); 
					$("#edit_ques_message_editor a").attr("target","_blank");
					$("#add_ques_message_editor a").attr("target","_blank");
				}
			  
			   
			   
			  });
			  
			  
			  $(".removeLink").click(function(){ document.execCommand("unlink", false, null); });
			  $(".clearFormat").click(function(){ document.execCommand("removeFormat", false, null); });
			  $(".bold").click(function(){document.execCommand("bold", false, null);});
			  $(".italic").click(function(){document.execCommand("italic", false, null);});
			  $(".underline").click(function(){document.execCommand("underline", false, null);});
  
  
			  

			  // عند الإرسال → نخزن المحتوى في input المخفي
			  $("#add_ques_message_button").click(function(){
				$("#add_ques_message_input").val($("#add_ques_message_editor").html());
			  });
			  
			   $("#edit_ques_message_button").click(function(){
				$("#edit_ques_message_input").val($("#edit_ques_message_editor").html());
			  });
  
  
			
			
	$(document).on('click',"#message_back", function (e) {
	
		$('.message').hide();	
		$(this).hide();	
	});
	
	$(document).on('click',"#message_condition_back", function (e) {
	
		$('.message_condition').hide();	
		$(this).hide();	
	});
	
	
	$(document).on('click',"#add_option_message", function (e) {
        e.stopPropagation();
    });
	
	$(document).on('click',".add_condition", function (e) {
		if($(this).attr('type')=='add'){if($(this).parent().next("#conditions_cont").hasClass("div_disabled")){return;}	}
		if($(this).attr('type')=='edit'){if($(this).parent().next("#conditions_edit_cont").hasClass("div_disabled")){return;}	}
		
			
			$("#message_condition_back").show();
			$("#add_condition_message").show();
			$("#conditions_marks").val("=");
			$("#add_condition_message_type").val("0");
			$("#add_condition_message_input_fixed").val("");
			$("#add_condition_message_input_list").val("اسم المستحق");
			$("#add_condition_message_input_fixed").show();
			$("#add_condition_message_input_list").hide();
			$("#add_condition_message_button").attr("type",$(this).attr("type"));
		
    });
	
	
	  
	$(document).on('click',".add_ques", function (e) { 
		  $("#add_ques_message_input").val("");
		  $("#add_ques_message_editor").html("");
		  e.stopPropagation();
		 
			$("#message_back").show();
			$("#add_ques_message").show();
			$("#add_ques_message_button").removeAttr("current_ids");
			$("#add_ques_message_button").attr("current_id",$(this).parent().attr("current_id"));
		  
		  
	
	});
	
	$(document).on('mousedown',".option_check", function (e) { 
		
		
		thisevent=$(this).parent().parent();
		e.stopPropagation();
		let checkedBeforeClick = $(this).prop("checked");
		//alert(checkedBeforeClick);
		if(checkedBeforeClick==0){
			
			setTimeout(function(){ $('.option_cont').removeClass("active");thisevent.click();console.log(thisevent.html()); }, 100);
		}
	});
	



	
	
	
	$(document).on('click',".add_ques_general", function (e) { 
		error=0;
		current_ids='';
		checkedBoxes=$(this).parent().find("input[type='checkbox']:checked");
		
	if(checkedBoxes.length>0){
		checkedBoxes.each(function() {
			console.log($(this).val()); // قيمة الـ checkbox
			current_ids+=$(this).parent().parent().attr("current_id") + "," ;
			
			if ($('div.ques_box[current_q="'+ $(this).parent().parent().attr("next_id") +'"]').length > 0) {
				error=1;
			}
			
		});
		
		if(error==0){
			$("#message_back").show();
			$("#add_ques_message").show();
			$("#add_ques_message_button").removeAttr("current_id");
			$("#add_ques_message_button").attr("current_ids",current_ids);
		}else{
			alert("احد الاختيارات له تشعب فرعى");
		}
	}else{
		alert("قم باختيار 2 على الاقل");
	}
	
	
	});
	

	
	
	
	
	$(document).on('click',".edit_ques", function (e) { 
	
		 e.stopPropagation();
		 
			 $("#message_back").show();
			 $("#edit_ques_message").show();
			 $("#edit_ques_message").find(".message_body").scrollTop(0);
			 $("#edit_ques_message_button").attr("current_id",$(this).attr("current_id"));
			 $("#edit_ques_message_input").val($(this).parent().find(".ques_text_text").html());
			 $("#edit_ques_message_editor").html($(this).parent().find(".ques_text_text").html());
			 $("#edit_ques_message_type").val($(this).attr("current_type"));
			 $("#edit_ques_required").val($(this).attr("current_required"));
			 $("#edit_ques_dgree").val($(this).attr("current_dgree"));
			 $("#edit_ques_show_answer").val($(this).attr("current_show_answer"));
			 $("#edit_ques_level").val($(this).attr("current_level"));
			 $("#edit_ques_message_images_text").val($(this).attr("current_images"));
			 $("#edit_ques_videos").val($(this).attr("current_videos"));
			

			 $("#edit_ques_preview_sortable").html("");		
			 const attr = $(this).attr("current_images") || "";
			 const current_images = attr.split(',').filter(Boolean);
			 current_images.forEach(current_image => {
				
				  const parts = current_image.split('.');
				  const ext = parts.pop().toLowerCase();      // الامتداد webp
				  const originalName = parts.join('.');       // الاسم الأصلي بدون الامتداد

				  // إضافة _thumb قبل الامتداد
				  const thumb_name = `${originalName}_thumb.${ext}`; 
				  
				  if (ext === 'webp') {
						$("#edit_ques_message_images_text").next('.file_preview-container').append('<div class="file_preview" data-img="' + current_image + '" id="' + current_image + '"> <img src="uploads/images/' + thumb_name + '" alt="' + current_image + '" /><div class="file_name_preview">'+current_image+'</div><div value="' + current_image + '" class="delete_preview">x</div></div>');

				  } else {
					console.log(item, '=> ليس صورة webp');
				  }
  
			});

		 
		 
    });



	$(document).on('click',".add_option", function (e) { 	
         
		 e.stopPropagation();
		 $("#add_option_message_input").val("");
		 $("#add_option_message_dgree").val($(this).closest('.ques_box').find('.edit_ques').attr('current_dgree'));
		 $("#message_back").show();
		 $("#add_option_message").show();
		 $("#add_option_message_button").attr("ques_id",$(this).attr("ques_id"));
		 
    });
	
	
	
	$(document).on('click',".edit_option", function (e) { 	
	
         e.stopPropagation();
		 $("#edit_option_message_button").attr("current_id",$(this).parent().attr("current_id"));
		 $("#edit_option_message_input").val($(this).parent().attr("current_text"));
		 $("#edit_option_message_dgree").val($(this).parent().attr("current_dgree"));
		 $("#edit_option_message_answer").val($(this).parent().attr("current_answer"));
		 $("#edit_option_message_images_text").val($(this).parent().attr("current_images"));
		 $("#conditions_edit_text").val($(this).parent().attr("current_conditions"));

		 
			 $("#edit_option_preview_sortable").html("");		
			 const attr = $(this).parent().attr("current_images") || "";
			 const current_images = attr.split(',').filter(Boolean);

			 current_images.forEach(current_image => {
				
				  const parts = current_image.split('.');
				  const ext = parts.pop().toLowerCase();      // الامتداد webp
				  const originalName = parts.join('.');       // الاسم الأصلي بدون الامتداد

				  // إضافة _thumb قبل الامتداد
				  const thumb_name = `${originalName}_thumb.${ext}`; 
				  
				  if (ext === 'webp') {
						$("#edit_option_message_images_text").next('.file_preview-container').append('<div class="file_preview" data-img="' + current_image + '" id="' + current_image + '"> <img src="uploads/images/' + thumb_name + '" alt="' + current_image + '" /><div class="file_name_preview">'+current_image+'</div><div value="' + current_image + '" class="delete_preview">x</div></div>');

				  } else {
					console.log(item, '=> ليس صورة webp');
				  }
  
			});
			
			
		 current_conditions=$(this).parent().attr("conditions");
		 if(current_conditions=='' || typeof current_conditions === "undefined"){
			$("#conditions_edit_cont").addClass('div_disabled');
		}else{
			
			$("#conditions_edit_cont").removeClass('div_disabled');
			current_condition = current_conditions.split(",");  
			$("#conditions_edit_cont").html("");
			
			// نعمل لوب ونطبع الأجزاء
			for ( i = 0; i < current_condition.length ; i++) {
			  if (current_condition[i] !== "") { // نتجنب القيم الفارغة
				//alert(current_condition[i]);
				$("#conditions_edit_cont").append("<div class='condition_cont' value='"+current_condition[i]+"'><div class='delete_condition' type='edit' >🗑️</div><div class='condition'>"+current_condition[i]+"</div></div>");
				$("#conditions_edit_text").val( current_conditions );
			
			  }
			}
			
			
		}
		 
		 
		 $("#message_back").show();
		 $("#edit_option_message").show();
		 
    });



	$(document).on('click',".option_cont", function (e) { 	

		if($(this).hasClass("active")){
			$(this).closest(".ques_box").nextAll(".ques_box").hide();
			$(this).removeClass("active");		
		}else{
			
			$(this).parent().find('.option_cont').removeClass("active");
			$(this).addClass("active");
			let parent = $(this).parent();
			parent.find(".option_cont").each(function () {
				if ($(this).find("input[type=checkbox]:checked").length > 0) {
				  	$(this).addClass("active");
				}
			});

			$(this).parent().parent().nextAll(".ques_box").hide();

			$(this).addClass('active');
			target=$(".ques_box[current_q='"+ $(this).attr("next_id") +"']");
			target.css('display','block');
			$('html, body').animate({
				scrollTop: target.offset().top
			 }, 800); // 800 = سرعة الحركة بالمللي ثانية
						
			
		}
	});
	
	
	
	$(document).on('click',"#add_ques_message_button", function (e) { 			
	
		thisevent=$(this);
		current_id=$(this).attr("current_id");
		current_ids=$(this).attr("current_ids");
		test_id=$(this).attr("test_id");
		ques_text=$("#add_ques_message_input").val();
		ques_type=$("#add_ques_message_type").val();
		ques_required=$("#add_ques_required").val();
		ques_show_answer=$("#add_ques_show_answer").val();
		ques_level=$("#add_ques_level").val();
		ques_dgree=$("#add_ques_dgree").val();
		ques_dgree=$("#add_ques_shuffle").val();
		ques_images=$("#add_ques_message_images_text").val();
		ques_videos=$("#add_ques_message_video_text").val();
		

		
		
		data={
			"current_id":current_id,
			"current_ids":current_ids,
			"test_id":test_id,
			"ques_text":ques_text,
			"ques_type":ques_type,
			"ques_images":ques_images,	
			"ques_required":ques_required,
			"ques_show_answer":ques_show_answer,
			"ques_level":ques_level,
			"ques_level":ques_shuffle,
			"ques_dgree":ques_dgree,
			"ques_videos":ques_videos
			
		};
	
	
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_add_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					new_ques="<div class='ques_box' current_sort='"+data.new_id+"' current_q='"+data.new_id+"' style='display: block;'>";
					new_ques+="<div title='اخفاء السؤال' class='ques_icon hide_ques'  status='0'>▼</div>";
					new_ques+="<div class='delete_ques' current_id='"+data.new_id+"' >🗑️</div>";
					new_ques+="<span current_id='"+data.new_id + "' current_type='" + data.current_type + "' current_text='" + data.current_text + "' current_images='" + data.current_images + "'   current_videos='" + data.current_videos + "'  current_required='" + data.current_required + "'  current_show_answer='" + data.current_show_answer + "'  current_level='" + data.current_level + "'  current_dgree='" + data.current_dgree + "' current_shuffle='" + data.current_shuffle + "'   class='edit_ques'>✏️</span>";
					new_ques+="<div class='save_options_sort'>💾</div>";
					new_ques+="<div class='ques_text' current_id='"+data.new_id+"' ><span class='ques_text_text'>" + ques_text + "</span><span class='ques_type'>" + ques_type + "</span><span class='ques_level'>مستوى: " + ques_level + "</span></div>";
					
					new_ques+="<div class='options_cont'><div class='options'></div>";
					new_ques+="<div class='add_option' ques_id='"+data.new_id+"'>➕ اختيار جديد</div>";
					new_ques+="</div>";
					new_ques+="</div>";
					
					$(".ques_container").append(new_ques);		
					$(".option_cont[current_id='"+current_id+"']").attr("next_id",data.new_id);
					alert(data.message);
					$("#message_back").hide();
					$("#add_ques_message").hide();
					$(".option_check").prop("checked", false);


				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	
	
	 
	$(document).on('click',"#edit_ques_message_button", function (e) { 	
		
	
		thisevent=$(this);
		current_id=$(this).attr("current_id");
		current_ids=$(this).attr("current_ids");
		ques_text=$("#edit_ques_message_input").val();
		ques_type=$("#edit_ques_message_type").val();
		ques_required=$("#edit_ques_required").val();
		ques_show_answer=$("#edit_ques_show_answer").val();
		ques_level=$("#edit_ques_level").val();
		ques_shuffle=$("#edit_ques_shuffle").val();
		ques_dgree=$("#edit_ques_dgree").val();
		ques_images=$("#edit_ques_message_images_text").val();
		ques_videos=$("#edit_ques_message_video_text").val();
		
		
		
		data={
			"current_id":current_id,
			"current_ids":current_ids,
			"ques_text":ques_text,
			"ques_type":ques_type,
			"ques_images":ques_images,	
			"ques_required":ques_required,
			"ques_show_answer":ques_show_answer,
			"ques_level":ques_level,
			"ques_shuffle":ques_shuffle,
			"ques_dgree":ques_dgree,
			"ques_videos":ques_videos		
		};
		
		
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_edit_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');
					$(".edit_ques[current_id='" + current_id + "']").attr('current_type',ques_type);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_images',ques_images);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_videos',ques_videos);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_text',ques_text);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_required',ques_required);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_show_answer',ques_show_answer);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_level',ques_level);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_dgree',ques_dgree);
					$(".edit_ques[current_id='" + current_id + "']").attr('current_shuffle',ques_shuffle);
					
					$(".ques_text[current_id='"+current_id+"'] .ques_text_text").html(ques_text);
					$(".ques_text[current_id='"+current_id+"'] .ques_type").text(ques_type);
					$(".ques_text[current_id='"+current_id+"'] .ques_level").text('مستوى: ' + ques_level);
					
					if(ques_type=='اكثر من اختيار من متعدد' || ques_type=='اختيار واحد من متعدد' || ques_type=='قائمة'){
						$(".ques_text[current_id='"+current_id+"']").next(".options_cont").removeClass("not_options");
					}else{
						$(".ques_text[current_id='"+current_id+"']").next(".options_cont").addClass("not_options");
						
					}
					alert(data.message);
					$("#message_back").hide();
					$("#edit_ques_message").hide();



				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	
	
	
	
	
	
	$(document).on('click',"#add_option_message_button", function (e) { 			
	
		thisevent=$(this);
		ques_id=$(this).attr("ques_id");
		option_text=$("#add_option_message_input").val();
		option_dgree=$("#add_option_message_dgree").val();
		option_answer=$("#add_option_message_answer").val();
		option_images=$("#add_option_message_images_text").val();
		option_conditions=$("#conditions_text").val();
		
		data={
			"ques_id":ques_id,
			"option_text":option_text,
			"option_dgree":option_dgree,
			"option_answer":option_answer,
			"option_images":option_images,
			"option_conditions":option_conditions				
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_add_option.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){

					alert(data.message);
					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					new_option="<div class='option_cont' current_text='" +  option_text + "' current_answer='" + option_answer + "' current_images='" + option_images + "' current_conditions='" + option_conditions + "' current_dgree='" + option_dgree + "' current_id='"+data.new_id+"' >";
					new_option+="<div class='option_text option_float text'><input class='option_check' type='checkbox'>"+option_text+"</div>";
					new_option+="<div class='edit_option option_float'   >✏️</div>";
					new_option+="<div class='delete_option option_float'>🗑️</div>";	
					new_option+="</div>";
					
					$(".ques_box[current_q='"+ ques_id +"']").find(".options").append(new_option);		
					
					
					$("#message_back").hide();
					$("#add_option_message").hide();



				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	
	

	$(document).on('click',"#edit_option_message_button", function (e) { 			

		
		option_id=$(this).attr("current_id");
		option_text=$("#edit_option_message_input").val();
		option_dgree=$("#edit_option_message_dgree").val();
		option_answer=$("#edit_option_message_answer").val();
		option_images=$("#edit_option_message_images_text").val();
		
		conditions=$("#conditions_edit_text").val();
		
		data={
			"option_id":option_id,
			"option_text":option_text,
			"option_dgree":option_dgree,
			"option_answer":option_answer,
			"option_images":option_images,
			"conditions":conditions				
		};
		
		
		
		
		
		
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_edit_option.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					$(".option_cont[current_id='"+option_id+"']").find(".option_text").html("<input class='option_check' type='checkbox'>"+option_text);	
					$(".option_cont[current_id='"+option_id+"']").attr("current_text",option_text);
					$(".option_cont[current_id='"+option_id+"']").attr("current_dgree",option_dgree);
					$(".option_cont[current_id='"+option_id+"']").attr("current_conditions",conditions);
					$(".option_cont[current_id='"+option_id+"']").attr("current_images",option_images);					
					$(".option_cont[current_id='"+option_id+"']").attr("current_answer",option_answer);

					
					alert(data.message);
					$("#message_back").hide();
					$("#edit_option_message").hide();




				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	
	
	
	$(document).on('click',".delete_option", function (e) { 			

		e.stopPropagation();
		thisevent=$(this);
		option_id=$(this).parent().attr("current_id");
		data={
			"option_id":option_id
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_delete_option.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					thisevent.parent().remove();					
					alert(data.message);




				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	

	$(document).on('click',".delete_ques", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		ques_id=$(this).attr("current_id");
		data={
			"ques_id":ques_id
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_delete_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					thisevent.parent().remove();					
					alert(data.message);




				}else{


					


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});
	
	$(document).on('click',".multi_ques", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		ques_id=$(this).attr("current_id");
		data={
			"ques_id":ques_id
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_multi_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					if(thisevent.hasClass('btn_disabled')){
						thisevent.removeClass('btn_disabled');
					}else{
						thisevent.addClass('btn_disabled');
					}
					alert("تم التحديث");




				}else{


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});	
	
	$(document).on('click',".loged_ques", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		ques_id=$(this).attr("current_id");
		data={
			"ques_id":ques_id
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_loged_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					if(thisevent.hasClass('btn_disabled')){
						thisevent.removeClass('btn_disabled');
					}else{
						thisevent.addClass('btn_disabled');
					}
					alert("تم التحديث");




				}else{


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});		
	
	
	$(document).on('click',".start_ques", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		ques_id=$(this).attr("current_id");
		data={
			"ques_id":ques_id
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_start_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					if(thisevent.hasClass('btn_disabled')){
						thisevent.removeClass('btn_disabled');
					}else{
						thisevent.addClass('btn_disabled');
					}
					alert("تم التحديث");




				}else{


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});		
	
		
	$(document).on('click',".delete_condition", function (e) { 	
			alert($(this).parent().attr("value"));
			if($(this).attr("type")=='edit'){
				$("#conditions_edit_text").val($("#conditions_edit_text").val().replace(","+$(this).parent().attr("value"), "") );
			}else{
				$("#conditions_text").val( $("#conditions_text").val().replace(","+$(this).parent().attr("value"), "") );
			}
			$(this).parent().remove();
	
	});
	
	
	$(document).on('change',"#add_condition_message_type", function (e) { 
		if($(this).val()==0){
			$("#add_condition_message_input_fixed").show();
			$("#add_condition_message_input_list").hide();
			
		}else if($(this).val()==1){
			
			$("#add_condition_message_input_list").show();
			$("#add_condition_message_input_fixed").hide();
			
			
		}
	});
	
	
	$(document).on('click',"#add_condition_message_button", function (e) { 	
			
			condition='';
			
			if($("#add_condition_message_type").val()=='0'){
				
				condition="[القيمة]" + "[" + $("#conditions_marks").val() + "]" + "[قيمة ثابتة]" + "[" +$ ("#add_condition_message_input_fixed").val() + "]";	
			
			}else if($("#add_condition_message_type").val()=='1'){
			
				condition="[القيمة]" + "[" + $("#conditions_marks").val() + "]" + "[بحث]" + "[" + $("#add_condition_message_input_list").val() +"]";	

			}else{
				return;
			}
			
			if($(this).attr("type")=='add'){
				$("#conditions_cont").append("<div class='condition_cont' value='"+condition+"'><div class='delete_condition' >🗑️</div><div class='condition'>"+condition+"</div></div>");
				$("#conditions_text").val( $("#conditions_text").val() + "," + condition );
			}else if($(this).attr("type")=='edit'){
				$("#conditions_edit_cont").append("<div class='condition_cont' value='"+condition+"'><div class='delete_condition' >🗑️</div><div class='condition'>"+condition+"</div></div>");
				$("#conditions_edit_text").val( $("#conditions_edit_text").val() + "," + condition );
			}
				
			
			
			$("#message_condition_back").hide();
			$("#add_condition_message").hide();
			
	
	});
	
	
	
	
	$(".ques_container").sortable({
        cancel: ".nosort",
        update: function() {
		   ques_sort_arr = [];
           $(".ques_container .ques_box").each(function(index) {
            ques_sort_arr.push({
                index: index + 1,
				current_q:$(this).attr('current_q')
            });
			
        });
		
		$(".save_ques_sort").show();
        console.log(ques_sort_arr); // ✅ هنا تشوف الترتيب الجديد في الكونسول
		
        }
    });

	$(".options").sortable({
        cancel: ".nosort",
        update: function() {
		   options_sort_arr = [];
           $(this).find(".option_cont").each(function(index) {

            options_sort_arr.push({
                index: index + 1,
				current_o:$(this).attr('current_id')
            });
			
			
        });
		
		$(this).closest('.ques_box').find(".save_options_sort").show();
        console.log(options_sort_arr); // ✅ هنا تشوف الترتيب الجديد في الكونسول
		
        }
    });

	
	

		
	$(document).on('click',".save_ques_sort", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		data={
			"sort_array":ques_sort_arr
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_save_sort_ques.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					thisevent.hide();
						
					alert("تم التحديث");




				}else{


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});		
	
		
		
			
	$(document).on('click',".save_options_sort", function (e) { 	

		e.stopPropagation();
		thisevent=$(this);
		data={
			"sort_array":options_sort_arr
		};
	
		$.ajax({


			method: 'post',
			data: remove_json_characters(JSON.stringify(data)),
			url : 'ajax_save_sort_options.php',
			beforeSend: function (xhr){ 

				//xhr.setRequestHeader('Authorization', localStorage.getItem("Inv_token")); 

			},

			async: false,
			success: function(data){

				if(data.status=='done'){


					//loading_success(data.message);
					//reload_data(data.structure,data.return_page,'hide_after');

					thisevent.hide();
						
					alert("تم التحديث");




				}else{


					if(data.message=='التوكن منتهى الصلاحية' || data.message=='لا يوجد توكن' || data.message=='توكن خطأ'){


						//loading_show(data.message);


						//$("#wait_success .modal-loading").load('../app/login_load.php',function(){

						//	$(".modal-loading").show();
						//	return false;

						//});



					}else{


						//loading_faild(data.message);		


					}


				}
					
					
					
					
			}, 
			faild: function(data){ 
			

				//loading_faild(data.message);		


			}	
		});
	
	});		
	
			
		

	$(document).on('click',".hide_ques", function (e) { 	
	
		if($(this).attr('status')=='0'){
			$(".ques_box").removeClass('active');
			$(this).parent().find('.options_cont').show();
			$(this).parent().addClass('active');
			$(this).attr('status','1');
		}else{
			$(".ques_box").removeClass('active');
			$(this).parent().find('.options_cont').hide();
			$(this).attr('status','0');
		}
		
	});
	
		$(document).on('click',".ques_text_text", function (e) { 
			$(this).closest('.ques_box').find('.hide_ques').click();
		});											
});


