

jQuery(document).ready(function () {
	
	let variation_index = 0;
	let last_variation_id = 0;




	jQuery(".expand_all_variation_edit").click(function(){
		jQuery(".elex_mv_mini_max_sec_edit").each( function (
			index,
			element
		) {
				var select = jQuery( element );
				select.slideDown("slow");

				  
		} );
	});
	jQuery(".close_all_variation_edit").click(function(){
		jQuery(".elex_mv_mini_max_sec_edit").each( function (
			index,
			element
		) {
				var select = jQuery( element );
					select.slideUp("slow");
				   
		} );
	});

	jQuery(document).on("click", ".elex_mv_mini_max_btn_edit", function () {
		var section = jQuery(this).closest(".elex_mv_parent_variations_edit").find(".elex_mv_mini_max_sec_edit");
		
		if (section.is(":visible")) {
			section.slideUp("slow");
			jQuery(this).addClass("elex_mv_max_btn_edit");
		} else {
			section.slideDown("slow");
			jQuery(this).removeClass("elex_mv_max_btn_edit");
		}
	});

	jQuery("#add_price_edit").click( function(){
		value_s = window.prompt(
			"Add price to all variations that don't have a price."
			);

			jQuery(".elex_mv_product_variation_regular_price_edit").each(function(idx, elem) {
							j_val = jQuery(this).val();
							if(!j_val){
								jQuery(this).val(value_s);
							}  

						});

	});

	jQuery( '#field_to_edit').on( 'change mouseup', function(){
		this.value;
		switch ( this.value ) {
			case 'toggle_enabled':
			 
					jQuery(".elex_mv_product_variation_enabled_edit").each(function(idx, elem) {
						if( jQuery(elem).prop('checked') ){
							jQuery(elem).prop('checked', false);
						}else{
							jQuery(elem).prop('checked', true);

						}

					} );   

				break;
			case 'delete_all':
				if (
					window.confirm(
						elex_mv_bulk_option_variation_obj.i18n_delete_all_variations
					)
				){

					delete_variation_id=[];
					jQuery('.elex_mv_variation_id_edit').each(function(idx, elem) {


						delete_variation_id.push(jQuery(this).data('id'));
					});
					data ={
						'variation_ids': delete_variation_id,
						'nonce': elex_mv_coupon_ajaxurl_object.nonce
					}

					jQuery.ajax({
							url : elex_mv_coupon_ajaxurl_object.ajax_url + "?action=elex_mv_delete_variations",
							method: "post",                      
							data: data,
							success: function ( response ) {

							},

					});
					jQuery( '.default-form-values_edit').html('');
					jQuery( '.elex_mv_add_variations_parent_edit').html('');

					jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index', 0);
					jQuery('.elex_mv_expand_close_edit_variation').addClass('d-none');

				}
			break;

			case 'variable_regular_price':
			case 'variable_sale_price':
			case 'variable_stock':
			case 'variable_low_stock_amount':
			case 'variable_weight':
			case 'variable_length':
			case 'variable_width':
			case 'variable_height':
			case 'variable_download_limit':
			case 'variable_download_expiry':

				value_field = window.prompt(
					elex_mv_bulk_option_variation_obj.i18n_enter_a_value
					);
				jQuery("[name^='"+ this.value+ "']").val(value_field);    
				

			break;
			case 'variable_regular_price_increase':
			case 'variable_regular_price_decrease':
			case 'variable_sale_price_increase':
			case 'variable_sale_price_decrease':
				value_s = window.prompt(
					elex_mv_bulk_option_variation_obj.i18n_enter_a_value_fixed_or_percent
				);
				value_s = parseInt(value_s);
				if( this.value.includes('increase') ){
					if(this.value.includes('regular')){

					jQuery(".elex_mv_product_variation_regular_price_edit").each(function(idx, elem) {
							j_val = parseInt( jQuery(this).val() );
							if(j_val){
								j_val = j_val + j_val * value_s / 100;
								jQuery(this).val(j_val);
							}  

						});

					}else{

						jQuery(".elex_mv_product_variation_sale_price_edit").each(function(idx, elem) {
							j_val = parseInt( jQuery(this).val() );
							if(j_val){
								j_val = j_val + j_val * value_s / 100;
								jQuery(this).val(j_val);
							}  

						});

					}
				}else{

					if(this.value.includes('regular')){

					jQuery(".elex_mv_product_variation_regular_price_edit").each(function(idx, elem) {
						j_val = parseInt( jQuery(this).val() );
						if(j_val){

							j_val = j_val - j_val * value_s / 100;
							jQuery(this).val(j_val);
						}  

					});

					}else{

					jQuery(".elex_mv_product_variation_sale_price_edit").each(function(idx, elem) {
						j_val = parseInt( jQuery(this).val() );
						if(j_val){
							j_val =  j_val - j_val * value_s / 100;
							jQuery(this).val(j_val);
						}  
					});

					}
				}

				break;
		}            
	} );

		

	


	jQuery('.elex_generate_variations_button_edit').on('click', function(){

		var  post_id =jQuery( '.elex-save-attributes_edit' ).attr("elex_mv_id_prod")

		var data = {
			action: 'elex_mv_link_all_variations',
			post_id: post_id,
			nonce: elex_mv_coupon_ajaxurl_object.nonce

		};

		jQuery.post(
			elex_mv_coupon_ajaxurl_object.ajax_url,
			data,
			function ( response ) {
				load_variations(post_id);

			}
		);

	})

	jQuery('.elex_add_manually_button_edit').on('click', function(){
		var  post_id =jQuery( '.elex-save-attributes_edit' ).attr("elex_mv_id_prod")

		var data = {
		action: 'elex_mv_load_data_manual_variations',
		post_id: post_id,
		nonce: elex_mv_coupon_ajaxurl_object.nonce

		};

	jQuery.post(
		elex_mv_coupon_ajaxurl_object.ajax_url,
		data,
		function ( response ) {
			if(response.success){
					data = JSON.parse( response.data )


					variation_id = data.variation_id;
					ship_class_data =  JSON.parse( data.shipping_class);

					product_attributes =  JSON.parse( data.product_attributes);
					default_attribute =  JSON.parse( data.default_attribute);

					jQuery('.elex_mv_add_variations_parent_edit').prepend( jQuery( '#template-for-variations_edit' ).html() );

					variation_index = jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index') ?  jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index') : 0;


					jQuery('.variable_parent_post_id_edit').attr('value', post_id);

					if( ( Object.keys( product_attributes ) ).length == 0){
						window.alert('Please enable use of attributes for variations!');
						return;
					}

					Object.keys( product_attributes ).forEach(function(pro_atribute){
						options_jquery ='';

						options_jquery += ' <option value=""> any ' + pro_atribute + '</option>';


							Object.keys( product_attributes[pro_atribute] ).forEach(function(attr_value){
							 
								options_jquery += ' <option value="'+product_attributes[pro_atribute][attr_value]+'">' + product_attributes[pro_atribute][attr_value] + '</option>';

						});
						jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_attribute_variation_values_select_edit").append(' <select name= \"attribute_' + pro_atribute + '[' + variation_index + ']' +'\" >'+options_jquery+ '</select>' );

					})

					if( jQuery(".default-form-values_edit").html() == '' ){

							jQuery(".default-form-values_edit").append( '<div class="fs-6 mb-1">Default form Values:</div>' );

					  
							Object.keys( product_attributes ).forEach(function(pro_atribute){


									opt_jquery ='';
									opt_jquery += ' <option value=""> any ' + pro_atribute + '</option>';


									Object.keys( product_attributes[pro_atribute] ).forEach(function(attr_value){
											if( product_attributes[pro_atribute][attr_value] == default_attribute[pro_atribute] ){
												opt_jquery += ' <option selected value="'+product_attributes[pro_atribute][attr_value]+'">' + product_attributes[pro_atribute][attr_value] + '</option>';
											}else{
											opt_jquery += ' <option value="'+product_attributes[pro_atribute][attr_value]+'">' + product_attributes[pro_atribute][attr_value] + '</option>';

											}

								});

							  

									jQuery(".default-form-values_edit").append( ' <select name= \"default_attribute_' + pro_atribute  +'\" >'+opt_jquery+ '</select>' );
									jQuery(".default-form-values_edit").removeClass('d-none');


							});
					}


					shipping_option='';
					ship_class_data.forEach(function (cu_value, curr_index, arr){

					   
							shipping_option += '<option value="' + cu_value.term_id +'"> '+ cu_value.name +' </option>' 



						})

				


					  
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_variation_id_edit").html( variation_id);
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_variation_id_edit").attr( 'data-id', variation_id );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".variable_post_id_edit").attr('name', 'variable_post_id[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".variable_post_id_edit").val(variation_id);
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_sku_edit").attr('name', 'variable_sku[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_regular_price_edit").attr('name', 'variable_regular_price[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_sale_price_edit").attr('name', 'variable_sale_price[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_variation_product_schedule_btn").attr('id', 'elex_mv_variation_product_schedule_btn_id-' + variation_index  );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_variation_product_schedule_sec").attr('id', 'elex_mv_variation_product_schedule_sec_id-' + variation_index  );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_schedule_cancel_btn").attr('id', 'elex_mv_product_schedule_cancel_btn_id-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_sale_start_date").attr('name', 'variable_sale_price_dates_from[' + variation_index + ']' );
					
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_sale_end_date").attr('name', 'variable_sale_price_dates_to[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_stock_quantity_edit").attr('name', 'variable_stock[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_allow_backorders_edit").attr('name', 'variable_backorders[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_low_stock_threshold_edit").attr('name', 'variable_low_stock_amount[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_stock_status_edit").attr('name', 'variable_stock_status[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_weight_edit").attr('name', 'variable_weight[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_length_edit").attr('name', 'variable_length[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_width_edit").attr('name', 'variable_width[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_height_edit").attr('name', 'variable_height[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_shipping_class_edit").attr('name', 'variable_shipping_class[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_shipping_class_edit").append(shipping_option);

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_description_edit").attr('name', 'variable_description[' + variation_index + ']' );

					
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_manage_stock_check_edit").attr('name', 'variable_manage_stock[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_manage_stock_check_edit").attr('id', 'elex_mv_variation_manage_stock_check_edit-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_stock_sec").attr('id', 'elex_mv_stock_sec-' + variation_index );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_stock_status_sec").attr('id', 'elex_mv_stock_status_sec-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_enabled_edit").attr('id', 'elex_mv_variation_enabled_edit-' + variation_index );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_enabled_edit").attr('name', 'variable_enabled[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_enabled_edit").attr('checked', 'checked' );

					
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex-multi-vendor-for-woocommerce-filetag-variation").attr('name', 'upload_image_id[' + variation_index + ']' );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex-multi-vendor-for-woocommerce-filetag-variation").attr('id', 'elex-multi-vendor-for-woocommerce-filetag-variation-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elexPreviewImageUploadVariation").attr('id', 'elexPreviewImageUploadVariation_' + variation_index  );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex-multi-vendor-for-woocommerce-remove-image-btn-variation").attr('id', 'elex-multi-vendor-for-woocommerce-remove-image-variation-' + variation_index  );

					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_limit_variation_edit").attr('id', 'elex_mv_download_limit_variation_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_limit_variation_edit").attr('name', 'variable_download_limit[' + variation_index + ']' );
					
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_expiry_variation_edit").attr('id', 'elex_mv_download_expiry_variation_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_expiry_variation_edit").attr('name', 'variable_download_expiry[' + variation_index + ']' );
					
						
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_edit_variable_table_download_file").attr('id', 'elex_mv_edit_variable_table_download_file_id-' + variation_index );
					jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex-multi-vendor-for-woocommerce-add-variable-download-file-row").attr('id', 'elex-multi-vendor-for-woocommerce-add-download-file-row_id-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_downloadable_check_edit").attr('id', 'elex_mv_product_variation_downloadable_check_edit-' + variation_index );
					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_downloadable_check_edit").attr('name', 'variable_is_downloadable[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_virtual_edit").attr('id', 'elex_mv_product_variation_virtual_edit-' + variation_index );
					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_virtual_edit").attr('name', 'variable_is_virtual[' + variation_index + ']' );

					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-edit-variable-download-file-div").attr('id', 'elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-' + variation_index );

					jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_shipping_variation_section").attr('id', 'elex_mv_shipping_variation_section-' + variation_index );

						jQuery(document.body).on( 'click', "#elex_mv_variation_product_schedule_btn_id-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							jQuery('#elex_mv_variation_product_schedule_sec_id-' + variat_index).removeClass('d-none')
						
						})
						jQuery(document.body).on( 'click', "#elex_mv_product_schedule_cancel_btn_id-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							jQuery('#elex_mv_variation_product_schedule_sec_id-' + variat_index).addClass('d-none')
						
						})

						jQuery(document.body).on( 'change', "#elex_mv_product_variation_downloadable_check_edit-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							if( jQuery(this).is(":checked") ){
								jQuery("#elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-" + variat_index ).removeClass('d-none');
							} else{
								jQuery("#elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-" + variat_index ).addClass('d-none');

							}
						})

						jQuery(document.body).on( 'change', "#elex_mv_product_variation_virtual_edit-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							if( jQuery(this).is(":checked") ){
								jQuery("#elex_mv_shipping_variation_section-" + variat_index ).addClass('d-none');
							} else{
								jQuery("#elex_mv_shipping_variation_section-" + variat_index ).removeClass('d-none');

							}
						})

						jQuery(document).on('click', '#elex-multi-vendor-for-woocommerce-add-download-file-row_id-' + variation_index , function(){
							
							var variat_index = this.id.split('-').pop();

							curr_dwn_index = jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variat_index).attr('curr_dwn_index') ? jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variation_index).attr('curr_dwn_index') : 0 ;
			  
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').prepend(jQuery('#template-for-var-product-file-download').html());
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_name_file_download" ).attr('id', 'elex_mv_name_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_name_file_download" ).attr('name', '_wc_files_names[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_hash_file_download" ).attr('id', 'elex_mv_hash_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_hash_file_download" ).attr('name', '_wc_files_hashes[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_url_file_download" ).attr('id', 'elex_mv_url_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_url_file_download" ).attr('name', '_wc_files_urls[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn" ).attr('id', 'elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn" ).attr('data-variat', variat_index );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-file-download" ).attr('id', 'elex-multi-vendor-for-woocommerce-file-download-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-file-download" ).attr('data-variat', variat_index );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product" ).attr('id', 'elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product" ).attr('data-variat', variat_index );

							
							curr_dwn_index++;
							jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variat_index).attr('curr_dwn_index', curr_dwn_index);
			  
						});


							jQuery( document.body ).on( 'change', '#elex-multi-vendor-for-woocommerce-file-download-id-' + variation_index, function ( event ) {
	  
							var reader;
						
							var dwn_no_index = this.id.split('-').pop();
							var variat_index = jQuery(this).data('variat');

							var formData = new FormData();
						
							if (this.files && this.files[0]) {
								reader = new FileReader();
								reader.onload = function (e) {
						
						
								// jQuery("#elexPreviewImageUploadVariation_" + variat_index).attr("src", e.target.result);
						
						
								// jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ dwn_no_index).removeClass("d-none");
						
						
								};
						
								reader.readAsDataURL(this.files[0]);
						
								formData.append('file',this.files[0] ); 
								formData.append('nonce',elex_mv_coupon_ajaxurl_object.nonce); 

								jQuery.ajax({
								url : elex_mv_coupon_ajaxurl_object.ajax_url + "?action=elex_mv_save_image_wordpress",
								method: "post",
								encType: 'multipart/form-data',
								processData: false,
								contentType: false,
								data: formData,
								success: function ( response ) {
						
								jQuery('[name="_wc_files_urls['+ variat_index +']['+ dwn_no_index +']"]').val(response.data)
								},
						
							});
						
							}
						  
							})


						jQuery(document).on('change', "[id^='elex_mv_variation_manage_stock_check_edit-']", function () {
							var variat_index = this.id.split('-').pop();


							if(jQuery(this).is(":checked")){
								jQuery('#elex_mv_stock_sec-'+variat_index).removeClass('d-none');
								jQuery('#elex_mv_stock_status_sec-'+variat_index).addClass('d-none');

							}else{
								jQuery('#elex_mv_stock_sec-'+variat_index).addClass('d-none');
								jQuery('#elex_mv_stock_status_sec-'+variat_index).removeClass('d-none');

							}


						})


						jQuery(document).on('change', "[id^='elex-multi-vendor-for-woocommerce-filetag-variation-']", function () {
							var reader;

							var variat_index = this.id.split('-').pop();

							if (this.files && this.files[0]) {
								reader = new FileReader();
								reader.onload = function (e) {


								jQuery("#elexPreviewImageUploadVariation_" + variat_index).attr("src", e.target.result);


								jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ variat_index).removeClass("d-none");

								};
								reader.readAsDataURL(this.files[0]);

							}
						} )

						jQuery(document).on('click', "[id^='elex-multi-vendor-for-woocommerce-remove-image-variation-']", function(){


							var vari_index = this.id.split('-').pop();

							jQuery("#elexPreviewImageUploadVariation_"+ vari_index).attr(
								"src",
								elex_mv_coupon_ajaxurl_object.plugin_path + "images/default.jpg"
							);
							jQuery("#elex-multi-vendor-for-woocommerce-filetag-variation-"+ vari_index).val(""); 
							jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ vari_index).addClass("d-none");
	  
						} )
					
					variation_index++;

					jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index', variation_index);
					jQuery('.elex_mv_edit_number_of_variation').html(variation_index);

					jQuery('.elex_mv_edit_number_of_variation').html(jQuery('.elex_mv_add_variations_parent_edit').children().size());


			}
		}
	);
	   

	})
	

	jQuery(document.body).on('click', '.elex_mv_remove_variation_button_edit', function(){

		if ( window.confirm("This variation will be deleted. Please confirm.") ){
				ar=[]
				ar.push( jQuery(this).parent().parent().parent().find(".elex_mv_variation_id_edit").data('id') );

				data ={
					'variation_ids': ar,
					'nonce': elex_mv_coupon_ajaxurl_object.nonce

				}

				jQuery.ajax({
						url : elex_mv_coupon_ajaxurl_object.ajax_url + "?action=elex_mv_delete_variations",
						method: "post",                      
						data: data,
						success: function ( response ) {

						},

				});
				jQuery(this).parent().parent().parent().parent().remove();


				jQuery('.elex_mv_edit_number_of_variation').html(jQuery('.elex_mv_add_variations_parent_edit').children().size());

				if( jQuery('.elex_mv_add_variations_parent_edit').children().size() == 0 ){
					jQuery('.elex_mv_expand_close_edit_variation').addClass('d-none');

				}

			
			}
	})
   

 

	function load_variations(post_id){


		var data = {
			action: 'elex_mv_load_variations',
			post_id: post_id,
			nonce: elex_mv_coupon_ajaxurl_object.nonce

		};
		jQuery.post(
			elex_mv_coupon_ajaxurl_object.ajax_url,
			data,
			function ( response ) {
				if(response.success){
					data = JSON.parse( response.data )

					jQuery('.elex_mv_add_variations_parent_edit').html('');

					variation_data =  JSON.parse( data.variation_data);
					ship_class_data =  JSON.parse( data.shipping_class);

					product_attributes =  JSON.parse( data.product_attributes);
					default_attribute =  JSON.parse( data.default_attribute);

					default_check =0;
					jQuery('.elex_mv_add_variations_parent_edit').html('');
					if(variation_data.length === 0){
						jQuery('.elex_mv_expand_close_edit_variation').addClass('d-none');
					//   window.alert('Please enable use of attributes for variations!');
						return;

					}else{
						jQuery('.elex_mv_expand_close_edit_variation').removeClass('d-none');

					}
					if(Object.keys( product_attributes ).length === 0){
						jQuery('.elex_mv_expand_close_edit_variation').addClass('d-none');
						window.alert('Please enable use of attributes for variations!');

						return;
  
						}else{
							jQuery('.elex_mv_expand_close_edit_variation').removeClass('d-none');
  
						}
					variation_data.forEach(function (current_value, curr_index, arr){

						jQuery('.elex_mv_add_variations_parent_edit').append( jQuery( '#template-for-variations_edit' ).html() );

						variation_index = jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index') ?  jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index'): 0;

						jQuery('.variable_parent_post_id_edit').attr('value', current_value.parent_id);

						object_atribute =  current_value.attributes
						Object.keys( object_atribute ).forEach(function( curr_key ){



							options_jquery ='';
							options_jquery += ' <option value=""> any ' + curr_key + '</option>';

							pro_atribute =   product_attributes[curr_key];

							Object.keys( pro_atribute ).forEach(function(attr_value){

								if( object_atribute[curr_key] == pro_atribute[attr_value] ){
									options_jquery += ' <option selected value="'+pro_atribute[attr_value]+'">' + pro_atribute[attr_value] + '</option>';

								}else{
									options_jquery += ' <option value="'+pro_atribute[attr_value]+'">' + pro_atribute[attr_value] + '</option>';

								}
							})

							jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_attribute_variation_values_select_edit").append(' <select name= \"attribute_' + curr_key + '[' + variation_index + ']' +'\" >'+options_jquery+ '</select>' );
						

						});

						if( jQuery(".default-form-values_edit").html() == '' ){

							jQuery(".default-form-values_edit").append( '<div class="fs-6 mb-1">Default form Values:</div>' );

							Object.keys( object_atribute ).forEach(function( curr_key ){

								if(default_check == 0 ){

									opt_jquery ='';
									opt_jquery += ' <option value=""> any ' + curr_key + '</option>';

									pro_atribute =   product_attributes[curr_key];



									Object.keys( pro_atribute ).forEach(function(attr_value){
									
										if( default_attribute[curr_key] == pro_atribute[attr_value] ){
										opt_jquery += ' <option selected value="'+pro_atribute[attr_value]+'">' + pro_atribute[attr_value] + '</option>';

										}else{
										opt_jquery += ' <option value="'+pro_atribute[attr_value]+'">' + pro_atribute[attr_value] + '</option>';

										}

									})

							  

									jQuery(".default-form-values_edit").append( ' <select name= \"default_attribute_' + curr_key  +'\" >'+opt_jquery+ '</select>' );
									jQuery(".default-form-values_edit").removeClass('d-none');

								}

							});
							default_check=1;       

						}

						shipping_option='';

						ship_class_data.forEach(function (cu_value, curr_index, arr){

							if(cu_value.term_id == current_value.shipping_class_id){
							shipping_option += '<option selected value="' + cu_value.term_id +'"> '+ cu_value.name +' </option>' 

							}else{
							shipping_option += '<option value="' + cu_value.term_id +'"> '+ cu_value.name +' </option>' 

							}


						})
						  
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_variation_id_edit").html( current_value.id );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_variation_id_edit").attr( 'data-id', current_value.id );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".variable_post_id_edit").attr('name', 'variable_post_id[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".variable_post_id_edit").val(current_value.id);
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_sku_edit").attr('name', 'variable_sku[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_regular_price_edit").attr('name', 'variable_regular_price[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_sale_price_edit").attr('name', 'variable_sale_price[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_variation_product_schedule_btn").attr('id', 'elex_mv_variation_product_schedule_btn_id-' + variation_index  );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_variation_product_schedule_sec").attr('id', 'elex_mv_variation_product_schedule_sec_id-' + variation_index  );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_schedule_cancel_btn").attr('id', 'elex_mv_product_schedule_cancel_btn_id-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_sale_start_date").attr('name', 'variable_sale_price_dates_from[' + variation_index + ']' );
						
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_sale_end_date").attr('name', 'variable_sale_price_dates_to[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_stock_quantity_edit").attr('name', 'variable_stock[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_allow_backorders_edit").attr('name', 'variable_backorders[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_low_stock_threshold_edit").attr('name', 'variable_low_stock_amount[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().first().find(".elex_mv_product_variation_stock_status_edit").attr('name', 'variable_stock_status[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_weight_edit").attr('name', 'variable_weight[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_length_edit").attr('name', 'variable_length[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_width_edit").attr('name', 'variable_width[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_height_edit").attr('name', 'variable_height[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_shipping_class_edit").attr('name', 'variable_shipping_class[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_shipping_class_edit").append(shipping_option);

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_description_edit").attr('name', 'variable_description[' + variation_index + ']' );
					  
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-filetag-variation").attr('name', 'upload_image_id[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-filetag-variation").attr('id', 'elex-multi-vendor-for-woocommerce-filetag-variation-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elexPreviewImageUploadVariation").attr('id', 'elexPreviewImageUploadVariation_' + variation_index  );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-remove-image-btn-variation").attr('id', 'elex-multi-vendor-for-woocommerce-remove-image-variation-' + variation_index  );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_manage_stock_check_edit").attr('name', 'variable_manage_stock[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_manage_stock_check_edit").attr('id', 'elex_mv_variation_manage_stock_check_edit-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_stock_sec").attr('id', 'elex_mv_stock_sec-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_stock_status_sec").attr('id', 'elex_mv_stock_status_sec-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_enabled_edit").attr('id', 'elex_mv_variation_enabled_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_enabled_edit").attr('name', 'variable_enabled[' + variation_index + ']' );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_enabled_edit").attr('checked', 'checked' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_limit_variation_edit").attr('id', 'elex_mv_download_limit_variation_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_limit_variation_edit").attr('name', 'variable_download_limit[' + variation_index + ']' );
					
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_expiry_variation_edit").attr('id', 'elex_mv_download_expiry_variation_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_download_expiry_variation_edit").attr('name', 'variable_download_expiry[' + variation_index + ']' );
					
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_downloadable_check_edit").attr('id', 'elex_mv_product_variation_downloadable_check_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_downloadable_check_edit").attr('name', 'variable_is_downloadable[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_virtual_edit").attr('id', 'elex_mv_product_variation_virtual_edit-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_product_variation_virtual_edit").attr('name', 'variable_is_virtual[' + variation_index + ']' );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-edit-variable-download-file-div").attr('id', 'elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_shipping_variation_section").attr('id', 'elex_mv_shipping_variation_section-' + variation_index );

						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex_mv_edit_variable_table_download_file").attr('id', 'elex_mv_edit_variable_table_download_file_id-' + variation_index );
						jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elex-multi-vendor-for-woocommerce-add-variable-download-file-row").attr('id', 'elex-multi-vendor-for-woocommerce-add-download-file-row_id-' + variation_index );

						jQuery("#elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-" + variation_index ).addClass('d-none');

						jQuery(document.body).on( 'click', "#elex_mv_variation_product_schedule_btn_id-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							jQuery('#elex_mv_variation_product_schedule_sec_id-' + variat_index).removeClass('d-none')
						
						})
						jQuery(document.body).on( 'click', "#elex_mv_product_schedule_cancel_btn_id-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							jQuery('#elex_mv_variation_product_schedule_sec_id-' + variat_index).addClass('d-none')
						
						})

						jQuery(document.body).on( 'change', "#elex_mv_product_variation_downloadable_check_edit-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							if( jQuery(this).is(":checked") ){
								jQuery("#elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-" + variat_index ).removeClass('d-none');
							} else{
								jQuery("#elex-multi-vendor-for-woocommerce-edit-variable-download-file-div-" + variat_index ).addClass('d-none');

							}
						})

						jQuery(document.body).on( 'change', "#elex_mv_product_variation_virtual_edit-" + variation_index , function(){
							var variat_index = this.id.split('-').pop();

							if( jQuery(this).is(":checked") ){
								jQuery("#elex_mv_shipping_variation_section-" + variat_index ).addClass('d-none');
							} else{
								jQuery("#elex_mv_shipping_variation_section-" + variat_index ).removeClass('d-none');

							}
						})

						jQuery(document).on('click', '#elex-multi-vendor-for-woocommerce-add-download-file-row_id-' + variation_index , function(){
							
							var variat_index = this.id.split('-').pop();

							curr_dwn_index = jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variat_index).attr('curr_dwn_index') ? jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variation_index).attr('curr_dwn_index') : 0 ;
			  
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').prepend(jQuery('#template-for-var-product-file-download').html());
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_name_file_download" ).attr('id', 'elex_mv_name_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_name_file_download" ).attr('name', '_wc_files_names[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_hash_file_download" ).attr('id', 'elex_mv_hash_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_hash_file_download" ).attr('name', '_wc_files_hashes[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_url_file_download" ).attr('id', 'elex_mv_url_file_download_id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex_mv_url_file_download" ).attr('name', '_wc_files_urls[' + variat_index + '][' + curr_dwn_index + ']' );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn" ).attr('id', 'elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn" ).attr('data-variat', variat_index );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-file-download" ).attr('id', 'elex-multi-vendor-for-woocommerce-file-download-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-file-download" ).attr('data-variat', variat_index );

							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product" ).attr('id', 'elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-' + curr_dwn_index );
							jQuery('#elex_mv_edit_variable_table_download_file_id-' + variat_index ).find('tbody').children('tr:first').find( ".elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product" ).attr('data-variat', variat_index );

							
							curr_dwn_index++;
							jQuery("#elex-multi-vendor-for-woocommerce-add-download-file-row_id-" + variat_index).attr('curr_dwn_index', curr_dwn_index);
			  
						});


							jQuery( document.body ).on( 'change', '#elex-multi-vendor-for-woocommerce-file-download-id-' + variation_index, function ( event ) {
	  
							var reader;
						
							var dwn_no_index = this.id.split('-').pop();
							var variat_index = jQuery(this).data('variat');

							var formData = new FormData();
						
							if (this.files && this.files[0]) {
								reader = new FileReader();
								reader.onload = function (e) {
						
						
								// jQuery("#elexPreviewImageUploadVariation_" + variat_index).attr("src", e.target.result);
						
						
								// jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ dwn_no_index).removeClass("d-none");
						
						
								};
						
								reader.readAsDataURL(this.files[0]);
						
								formData.append('file',this.files[0] ); 
								formData.append('nonce',elex_mv_coupon_ajaxurl_object.nonce); 

								jQuery.ajax({
								url : elex_mv_coupon_ajaxurl_object.ajax_url + "?action=elex_mv_save_image_wordpress",
								method: "post",
								encType: 'multipart/form-data',
								processData: false,
								contentType: false,
								data: formData,
								success: function ( response ) {
							
						
								jQuery('[name="_wc_files_urls['+ variat_index +']['+ dwn_no_index +']"]').val(response.data)
								},
						
							});
						
							}
						  
							})

						jQuery(document).on('change', "[id^='elex_mv_variation_manage_stock_check_edit-']", function () {
							var variat_index = this.id.split('-').pop();


							if(jQuery(this).is(":checked")){
								jQuery('#elex_mv_stock_sec-'+variat_index).removeClass('d-none');
								jQuery('#elex_mv_stock_status_sec-'+variat_index).addClass('d-none');


							}else{
								jQuery('#elex_mv_stock_sec-'+variat_index).addClass('d-none');
								jQuery('#elex_mv_stock_status_sec-'+variat_index).removeClass('d-none');

							}


						})

						jQuery(document).on('change', "[id^='elex-multi-vendor-for-woocommerce-filetag-variation-']", function () {
							var reader;

							var variat_index = this.id.split('-').pop();

							if (this.files && this.files[0]) {
								reader = new FileReader();
								reader.onload = function (e) {

								// jQuery('.elex_mv_add_variations_parent_edit').children().last().find(".elexPreviewImageUploadVariation").attr('src', e.target.result  );

								jQuery("#elexPreviewImageUploadVariation_" + variat_index).attr("src", e.target.result);


								jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ variat_index).removeClass("d-none");


								};
								reader.readAsDataURL(this.files[0]);

							}
						} )

						jQuery(document).on('click', "[id^='elex-multi-vendor-for-woocommerce-remove-image-variation-']", function(){


							var vari_index = this.id.split('-').pop();

							jQuery("#elexPreviewImageUploadVariation_"+ vari_index).attr(
								"src",
								elex_mv_coupon_ajaxurl_object.plugin_path + "images/default.jpg"
							);
							jQuery("#elex-multi-vendor-for-woocommerce-filetag-variation-"+ vari_index).val(""); 
							jQuery("#elex-multi-vendor-for-woocommerce-remove-image-variation-"+ vari_index).addClass("d-none");
	  
						} )

						variation_index++;
						jQuery('.elex_mv_add_variations_parent_edit').attr('variation_curr_index', variation_index);
						jQuery('.elex_mv_edit_number_of_variation').html(jQuery('.elex_mv_add_variations_parent_edit').children().size());

				   
						 
					})

		

				}
			  


			}
		);


	}

	jQuery('.elex_mv_save_variations_edit').click(function(){

		wrapper = jQuery('.elex_mv_add_variations_parent_main_edit');
		dis = jQuery(this);
		data = { };

		data = get_variations_fields(wrapper, 'elex_mv_save_variations_edit');
		//    data.action = "elex_mv_save_variations";

		jQuery.ajax({
			url : elex_mv_coupon_ajaxurl_object.ajax_url + "?action=elex_mv_save_variations",
			method: "post",
			encType: 'multipart/form-data',
			processData: false,
			contentType: false,
			data: data,
			success: function ( response ) {

				if ( response.error ) {
					// Error.
					window.alert( response.error );
				}else if(response.success == false){

					window.alert( response.data.error );

				} else{

					var additem = document.getElementById("wsdesk_additem_toast-edit");
					jQuery("#toast_message_mv-edit").html("Variations are saved");
					var toast = new bootstrap.Toast(additem);
					toast.show();


				if(dis.data('button') == 'next' ){
				next_page_iterate(jQuery(".nav-tabs > .nav-item > .active")
						.parent()
						.next("li"));
				}


				}
			},

		});

	})
	function next_page_iterate(elem){

if( elem.hasClass('d-none') ){
	next_page_iterate(elem.next('li'));
}else{
		elem.find("button")
		.trigger("click");
}
}
	function  get_variations_fields ( fields ) {

		var formData = new FormData(
			document.getElementById("variation_edit")
		);
		formData.append('nonce', elex_mv_coupon_ajaxurl_object.nonce)

		// var data = jQuery( ':input', fields ).serializeJSON();

		// jQuery( '.variations-defaults select' ).each( function (
		//     index,
		//     element
		// ) {
		//     var select = jQuery( element );
		//     formData[ select.attr( 'name' ) ] = select.val();
		// } );

		return formData;
	}


})

