$(function(){
	
	
	function success_message(message){
		
		$("#sp-message div").html(message);
		$("#sp-message div").removeClass("alert-danger");
		$("#sp-message div").removeClass("alert-light");
		$("#sp-message div").removeClass("alert");
		$("#sp-message div").addClass("alert alert-success");
		$("#sp-message").fadeIn();
		$(".sp-modal_overlay").fadeIn();
	}
	
	function faild_message(message){
		
		$("#sp-message div").html(message);
		$("#sp-message div").removeClass("alert-success");
		$("#sp-message div").removeClass("alert-light");
		$("#sp-message div").removeClass("alert");
		$("#sp-message div").addClass("alert alert-danger");				
		$("#sp-message").fadeIn();
		$(".sp-modal_overlay").fadeIn();
	}
	
	function waiting_message(message){
		
		$("#sp-message div").html(message);
		$("#sp-message div").removeClass("alert-success");
		$("#sp-message div").removeClass("alert-danger");
		$("#sp-message div").removeClass("alert");
		$("#sp-message div").addClass("alert alert-light");				
		$("#sp-message").fadeIn();
		$(".sp-modal_overlay").fadeIn();
	}
	
	
	form_validate=function(){


		


		form_validate_stauts='notdone';


		var forms = document.querySelectorAll('.needs-validation');





		  // Loop over them and prevent submission


		  Array.prototype.slice.call(forms)


			.forEach(function (form) {


				





			  form.addEventListener('submit', function (event) {


				if (!form.checkValidity()) {


				  event.preventDefault();


				  event.stopPropagation()


				}


				


				form.classList.add('was-validated')


			  }, false)


			});


			


			form_validate_stauts='done';





			


			


			//alert("sucess");


	}




	
	
	 function convertFormToJSON(form) {

        const array = $(form).serializeArray(); // Encodes the set of form elements as an array of names and values.


        const json = {};


        $.each(array, function () {


          json[this.name] = this.value || "";


        });


        return json;


      }

	  
	 
	  
	$(document).on('change', '#search_gov', function() {
		//alert($("#search_gov option:selected").attr("gov_id"));
		$("#search_city option").hide();
		$("#search_city option[gov_id='"+$("#search_gov option:selected").attr("gov_id")+"']").show();
		$("#init_city").show();
		$("#search_city").val('جميع المدن');
		
	});

	
	
	//alert("S");
	$("#cp_page_loader_header_btn_back").click(function(){
		
		$("#cp_page_loader_cont").hide();
	});
	
	
	$(".cp_cat").click(function(){
		
		$(".box_contains span").removeClass("active")
		$(this).find(".box_contains").find("span").addClass("active");
		page=website_url+"/ajax_" + $(this).attr("url") + ".php";
		//page='users/index.html';
		$("#cp_page_loader_header_btn_title").text($(this).attr("title"));
		//alert($(this).attr("title"));
		
		
		$("#cp_page_loader").load(page,function(data){

		});
		
		$("#cp_page_loader_cont").animate({
			
			"right":"0px"
		}, 200,function(){
			

		});
		


	});
	
	
	$("#cp_memeber_back").on("click",function(){
			$("#cp_page_loader_cont").animate({
			
			"right":"100vw"
			
			}, 250,function(){
				
				$("#cp_page_loader").html('');
				
			});
		
		});
		
		
	
	
	$(document).on('click', '.return_order', function() {
		 var object_clicked = $(this);
		$.post(website_url+"/"+"ajax_return_order.php",{

			value:$(this).attr("product_id")

		},function(data){
		
			
			//alert(data);
			
			if(data=='done'){
				$("#sp-message div").html("تم اضافة طلب الارجاع");
				
				$("#sp-message div").removeClass("alert-danger");
				$("#sp-message div").removeClass("alert");
				$("#sp-message div").addClass("alert alert-success");
				$("#sp-message").fadeIn();
				
				object_clicked.removeClass('return_order');
				object_clicked.addClass("btn4_disable");
				object_clicked.html('تم تقديم الطلب');
				
			}
					
			
		
		}).success(function(){

			


	
		});
		
	
		
	});	
	
	
	
	/*
	$(document).on('click', '.video_title', function() {
		object=$(this)
		$.post(website_url+"/"+"ajax_decrypt.php",{

			value:$(this).attr("video")

		},function(data){
		
			//alert(object.parent().attr("class"));
			$("#video_loading").html('');
			object.parent().parent().find("#video_loading").html("<iframe  class='embedded-video-16-9' type='text/html'  src='https://www.youtube.com/embed/"+data+"?controls=0&showinfo=0&autoplay=1' frameborder='0'></iframe>");
			$(".video_title").removeClass("active");
			object.addClass("active");
		
		});
		

		
	});	
	
	*/
	$(document).on('click', '.video_title', function() {
		object=$(this)
		$.post(website_url+"/"+"ajax_decrypt.php",{

			value:$(this).attr("video")

		},function(data){
			//alert(object.parent().attr("class"));
			$("#video_loading").html('');
			$(".sp-modal_content").html("<div class='videoWrapper'><iframe class='embedded-video-16-9' type='text/html' src='https://www.dailymotion.com/embed/video/"+data+"?queue-autoplay-next=0&queue-enable=0&sharing-enable=0&ui-logo=0&ui-start-screen-info=0 frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture; fullscreen'></iframe></div>");
			$(".video_title").removeClass("active");
			object.addClass("active");
			$(".sp-modal_content").fadeIn();
			$(".sp-modal_overlay").fadeIn();
		
		});
		

		
	});	
	
		$(document).on('click', '.sp-modal_overlay , #sp-message', function() {
			
			$(".sp-modal_content").fadeOut();
			$(".sp-modal_overlay").fadeOut();
			$(".sp-modal_content").html("");

			
		});
	

	
	
	last_form_data='';
	$(document).on('submit', 'form', function(e) {
		

		thisevent=$(this);
		e.preventDefault();
		const form = $(e.target);
		const json = convertFormToJSON(form);
		
		
		
		
		form_files='';
		$('#images_upload_input').each(function( index ) {
			form_files+=$(this).val();
			
		});
		
		$('#files_upload_input').each(function( index ) {
			form_files+=$(this).val();
			
		});
		
		
		var formData = new FormData(this);
		
		console.log(formData);
		
		var formDataCompine = new FormData();
       

        for(i=0;i<images_arr.length;i++){
			//alert('image_arr');
			 formDataCompine.append('images[]',images_arr[i]);
		}

		for(i=0;i<files_arr.length;i++){
			 //alert('files_arr');
			 formDataCompine.append('files[]',files_arr[i]);
		}
		
		
        // Append form fields to FormData
        var formFields = $(this).serializeArray();
        $.each(formFields, function(i, field) {
            formDataCompine.append(field.name, field.value);
        });

        // Append files array to FormData

		console.log(files_arr);
		console.log(formDataCompine);
		

		
		//console.log(last_form_data);
		//console.log(JSON.stringify(json)+form_files);
		
		
		//التاكد بانه قام بتغييرات فى الفورم
		if(last_form_data===JSON.stringify(json)+form_files){
			
			faild_message('لم تقم بتغييرات');
		
		}else{

			last_form_data=JSON.stringify(json)+form_files;
			
			waiting_message('برجاء الانتظار');
			
			$.ajax({
				
			type : 'POST',
			url : website_url+"/"+"ajax_members_personal_update.php",
			//url : website_url+"/upload_script/"+"ajax_upload_images.php",
			data: formDataCompine,
			processData: false,
			contentType: false,
			
			success: function(data){
			
				
				if(data.status=='done'){
				
					
					success_message(data.message);
					images_arr=[];
					files_arr=[];
					$("#images").val();
					$("#files").val();
					$("#old_images").val(data.old_images);
					$("#old_files").val(data.old_files);
					
					
					
				}else{
				
					if(typeof(data.message) != "undefined"){
						faild_message(data.message);
					}else{
						faild_message('هناك خطأ');
					}
				}
			
			}
		
		});
	
		}
		

	});
	
	
	
			
	
	$(document).click(function() {
		$("#sp-message").fadeOut();
	});
	
		$(document).scroll(function() {
		$("#sp-message").fadeOut();
	});
	
	
setInterval(function() {
	//$(".ytp-gradient-top").css("visibility",'hidden');
	//alert($(".ytp-gradient-top").attr("data-layer"));
	//var iframe = $('iframe'); // or some other selector to get the iframe
	//alert($(".ytp-gradient-top", iframe.contents()).attr("data-layer"));
	
}, 10000);
	
	
	
	

  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });

});