jQuery(document).ready(function() {

		var selectedOptions = {};
		var selectedOptionsProd = {};

		

		jQuery(document).on( 'click', '[id^=delete_row_category_selected_for_commission_button_id-]', function(){

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

			

			jQuery(document).find( "#elex_mv_category_commission_template_tr_id-" + elle_number ).remove();
			

		})

		jQuery(document).on( 'click', '[id^=delete_row_product_selected_for_commission_button_id-]', function(){

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

			jQuery( "#elex_mv_product_commission_template_tr_id-" + elle_number ).remove();
			
		})


		var category_row_num = 0;
		var product_row_num = 0;

		jQuery(".add_row_categories_selected_for_commission_button").click(function(){

			jQuery(".elex_mv_category_commission_body").append( jQuery("#elex_mv_category_commission_template").html() );


			category_row_num =  jQuery(".elex_mv_category_commission_body").data('category_row_num_val') ? jQuery(".elex_mv_category_commission_body").data('category_row_num_val') : 0;
			
			jQuery(".elex_mv_category_commission_body").children().last().find(".category_select_dropdown_for_commission_vendor").attr('name', "categories_selected_forCommission[" + category_row_num + "][]" );
			jQuery(".elex_mv_category_commission_body").children().last().find(".category_select_dropdown_for_commission_vendor").attr('id', "category_select_dropdown_for_commission_vendor_id-" + category_row_num  );

			jQuery(".elex_mv_category_commission_body").children().last().attr('id', "elex_mv_category_commission_template_tr_id-" + category_row_num  );

			jQuery(".elex_mv_category_commission_body").children().last().find(".category_com_dropdown_for_commission_vendor").attr('name', "category_commission_vendor[" + category_row_num + "]" );

			jQuery(".elex_mv_category_commission_body").children().last().find(".delete_row_category_selected_for_commission_button").attr('id', "delete_row_category_selected_for_commission_button_id-" + category_row_num );
			jQuery(".elex_mv_category_commission_body").data('category_row_num_val', ++category_row_num );


			jQuery(".category_select_dropdown_for_commission_vendor").select2({
				placeholder: "Search Category",
				minimumInputLength: 2,
				// initSelection: true, 

				ajax: {
					url: elex_mv_coupon_ajaxurl_object.ajax_url,
					type: 'POST',

					data: function (params) {
						var query = {
							store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
							search_term: params.term,
							status:'publish',
							action: 'elex_mv_api_endpoint_for_categories',
							nonce: elex_mv_coupon_ajaxurl_object.nonce,
						}
						return query;
					},
					processResults: function (response) {
						return {
							results: response.data.map(function (cat) {
								return {
									text: cat.name,
									id: cat.term_id + ':' + cat.name  ,
									disabled: selectedOptions[cat.term_id + ":" + cat.name]

								}
							}),

						};
					}

				}

			})

		

			jQuery(".category_select_dropdown_for_commission_vendor").on("select2:select", function(e) {
					var selectedValue = e.params.data.id;


					selectedOptions[selectedValue] = true;


				});
				
				jQuery(".category_select_dropdown_for_commission_vendor").on("select2:unselect", function(e) {
					var deselectedValue = e.params.data.id;
					delete selectedOptions[deselectedValue];

				});
			
						
		});


				

		jQuery(".add_row_products_selected_for_commission_button").click(function(){

			jQuery(".elex_mv_product_commission_body").append( jQuery("#elex_mv_product_commission_template").html() );
			product_row_num = jQuery(".elex_mv_product_commission_body").data('product_row_num_val') ? jQuery(".elex_mv_product_commission_body").data('product_row_num_val') : 0;
			jQuery(".elex_mv_product_commission_body").children().last().find(".product_select_dropdown_for_commission_vendor").attr('name', "products_selected_forCommission[" + product_row_num + "][]" );
			jQuery(".elex_mv_product_commission_body").children().last().find(".product_select_dropdown_for_commission_vendor").attr('id', "product_select_dropdown_for_commission_vendor_id-" + product_row_num  );

			jQuery(".elex_mv_product_commission_body").children().last().attr('id', "elex_mv_product_commission_template_tr_id-" + product_row_num  );

	

			jQuery(".elex_mv_product_commission_body").children().last().find(".product_com_dropdown_for_commission_vendor").attr('name', "product_commission_vendor[" + product_row_num + "]" );
			jQuery(".elex_mv_product_commission_body").children().last().find(".delete_row_product_selected_for_commission_button").attr('id', "delete_row_product_selected_for_commission_button_id-" + product_row_num );

			jQuery(".elex_mv_product_commission_body").data('product_row_num_val', ++product_row_num);

			jQuery(".product_select_dropdown_for_commission_vendor").select2({
				placeholder: "Search Product",
				minimumInputLength: 2,
				// initSelection: true, 

				ajax: {
					url: elex_mv_coupon_ajaxurl_object.ajax_url,
					type: 'POST',

					data: function (params) {
						var query = {
							store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
							search_term: params.term,
							status:'publish',
							action: 'elex_mv_api_endpoint_for_products',
							nonce: elex_mv_coupon_ajaxurl_object.nonce,
						}
						return query;
					},
					processResults: function (response) {
						return {
							results: response.data.map(function (prod) {
								return {
									text: prod.name,
									id: prod.id + ':' + prod.name,
									disabled: selectedOptionsProd[prod.id + ':' + prod.name]

								}
							}),

						};
					}

				}

			});

			jQuery(".product_select_dropdown_for_commission_vendor").on("select2:select", function(e) {
					var selectedValue = e.params.data.id;


					selectedOptionsProd[selectedValue] = true;


				});
				
				jQuery(".product_select_dropdown_for_commission_vendor").on("select2:unselect", function(e) {
					var deselectedValue = e.params.data.id;
					delete selectedOptionsProd[deselectedValue];
				

				});

		});



		jQuery(".category_select_dropdown_for_commission_vendor").select2({
			placeholder: "Search Category",
			minimumInputLength: 2,
			// initSelection: true, 

			ajax: {
				url: elex_mv_coupon_ajaxurl_object.ajax_url,
				type: 'POST',

				data: function (params) {
					var query = {
						store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
						search_term: params.term,
						status:'publish',
						action: 'elex_mv_api_endpoint_for_categories',
						nonce: elex_mv_coupon_ajaxurl_object.nonce,
					}
					return query;
				},
				processResults: function (response) {
					return {
						results: response.data.map(function (cat) {
							return {
								text: cat.name,
								id: cat.term_id + ':' + cat.name,
							}
						}),

					};
				}

			}

		});

		jQuery(".product_select_dropdown_for_commission_vendor").select2({
			placeholder: "Search Product",
			minimumInputLength: 2,
			// initSelection: true, 

			ajax: {
				url: elex_mv_coupon_ajaxurl_object.ajax_url,
				type: 'POST',

				data: function (params) {
					var query = {
						store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
						search_term: params.term,
						status:'publish',
						action: 'elex_mv_api_endpoint_for_products',
						nonce: elex_mv_coupon_ajaxurl_object.nonce,
					}
					return query;
				},
				processResults: function (response) {
					return {
						results: response.data.map(function (prod) {
							return {
								text: prod.name,
								id: prod.id + ':' + prod.name,
							}
						}),

					};
				}

			}

		});


	jQuery('#apply_vendor_commission_switch').change(function(){

		if(jQuery('#apply_vendor_commission_switch').is(":checked")){
			jQuery(".elex-vendor-commission-screen").removeClass('d-none');
			jQuery(".elex_global_commission_display_field").addClass('d-none');

		}else{
			jQuery(".elex-vendor-commission-screen").addClass('d-none');
			jQuery(".elex_global_commission_display_field").removeClass('d-none');

		}

	})


	jQuery('#category_commission_switch_vendor_edit').change(function(){

		if(jQuery('#category_commission_switch_vendor_edit').is(":checked")){
			jQuery(".category_commission_switch_vendor_screen").removeClass('d-none');

		}else{
			jQuery(".category_commission_switch_vendor_screen").addClass('d-none');

		}

	})

	jQuery('#product_commission_switch_vendor_edit').change(function(){

		if(jQuery('#product_commission_switch_vendor_edit').is(":checked")){
			jQuery(".product_commission_switch_vendor_screen").removeClass('d-none');

		}else{
			jQuery(".product_commission_switch_vendor_screen").addClass('d-none');

		}

	})

	
	jQuery('#global_capabilities').change(function(){
		if (jQuery('#global_capabilities').is(":checked")) {
				jQuery('.elex_mv_vendor_capabilities_div').addClass('d-none');
			} else {
				jQuery('.elex_mv_vendor_capabilities_div').removeClass('d-none');

			}

		})

	jQuery('.elex-multi-vendor-for-woocommerce-edit-vendor-btn').click(function() {


		vendorId = jQuery(this).data('vendor-id');
		status = jQuery(this).data('status');

		jQuery("#elex-multi-vendor-for-woocommerce-edit-vendor").addClass("active");

		var edit_page_link = elex_mv_admin_ajaxurl_object.ajax_url;
		edit_page_link = edit_page_link + '?action=elex_mv_edit_vendor_display';
		var form = {
			id: vendorId,
			nonce: elexMvManageVendorsParams.nonce
		};
		jQuery.ajax({
			url: edit_page_link,
			type: 'POST',
			data: form,
			enctype: 'multipart/form-data',
			success: function(result) {
				jQuery("#elex_multivendor_vendor_edit_user_id").val(vendorId);
				jQuery("#elex_multivendor_vendor_edit_status").val(status);
				jQuery("#elex_multivendor_vendor_edit_user_name").val(result.data.store_user_name);
				jQuery("#elex_multivendor_vendor_edit_prev_user_name").val(result.data.store_user_name);

				jQuery("#elex_multivendor_vendor_edit_store_name").val(result.data.store_name);
				jQuery("#elex_multivendor_vendor_edit_prev_store_name").val(result.data.store_name);

				jQuery("#elex_multivendor_vendor_edit_email").val(result.data.store_email);
				jQuery("#elex_multivendor_vendor_edit_phone_number").val(result.data.store_phone_number);
				var input_phone_number = window.intlTelInput( document.querySelector("#elex_multivendor_vendor_edit_phone_number"), {
					autoPlaceholder: "polite",
				} );
				input_phone_number.setCountry(result.data.store_country_iso);
				jQuery("#elex_multivendor_vendor_edit_dial_code").val(result.data.store_dial_code);
				jQuery("#elex_multivendor_vendor_edit_country_iso").val(result.data.store_country_iso);
				jQuery("#elex_multivendor_vendor_edit_city").val(result.data.store_city);
				jQuery("#elex_multivendor_vendor_edit_postal_code").val(result.data.store_postal_code);
				jQuery("#elex_multivendor_vendor_edit_paypal_email").val(result.data.store_paypal_email);
				jQuery("#elex_multivendor_vendor_edit_commission").val(result.data.store_commission);
				jQuery("#elex_multivendor_vendor_edit_country").val(result.data.store_country);


				if (result.data.store_country) {

					loadStates(result.data.store_country).then(function(a1) {
						jQuery("#elex_multivendor_vendor_edit_state").select2().val(result.data.store_state).trigger('change');

					});
				}


				jQuery("#elex_multivendor_vendor_edit_state").select2().val(result.data.store_state).trigger('change');

				jQuery("#elex_multivendor_vendor_edit_store_full_addr1").val(result.data.store_address1);

				jQuery("#elex_multivendor_vendor_edit_store_full_addr2").val(result.data.store_address2);

				if (result.data.store_image) {
					elexPreviewImageVendorEditUpload.setAttribute('src', result.data.store_image);
				} else {
					elexPreviewImageVendorEditUpload.setAttribute('src', elexMvManageVendorsParams.plugin_url + 'assets/images/default-store-icon.svg');
				}

				if (!result.data.vendor_to_sell_without_approval) {
					jQuery("#vendor_to_sell_without_approval").removeAttr('checked');
				}
				if (!result.data.vendor_to_allow_vendor_shipping) {
					jQuery("#vendor_to_allow_vendor_shipping").removeAttr('checked');
				}

				if (!result.data.vendor_to_override_creds) {
					jQuery("#vendor_to_override_creds").removeAttr('checked');
				}
				if (!result.data.vendor_to_choose_ship_services) {
					jQuery("#vendor_to_choose_ship_services").removeAttr('checked');
				}
				if (!result.data.vendor_to_use_flat_rate_service_set_by_admin) {
					jQuery("#vendor_to_use_flat_rate_service_set_by_admin").removeAttr('checked');
				}
				if (!result.data.vendor_to_change_order_status) {
					jQuery("#vendor_to_change_order_status").removeAttr('checked');
				}
				if (!result.data.vendor_to_generate_coupons) {
					jQuery("#vendor_to_generate_coupons").removeAttr('checked');
				}
				if (!result.data.vendor_to_create_category) {
					jQuery("#vendor_to_create_category").removeAttr('checked');

				}

				if (!result.data.global_capabilities) {
					jQuery("#global_capabilities").removeAttr('checked');
					jQuery(".elex_mv_vendor_capabilities_div").removeClass('d-none');
				} else {
					jQuery(".elex_mv_vendor_capabilities_div").addClass('d-none');

				}


				if (!result.data.category_commission_switch_vendor) {
					jQuery("#category_commission_switch_vendor_edit").removeAttr('checked');
					jQuery(".category_commission_switch_vendor_screen").addClass('d-none');

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

				}
				if (!result.data.product_commission_switch_vendor) {
					jQuery("#product_commission_switch_vendor_edit").removeAttr('checked');
					jQuery(".product_commission_switch_vendor_screen").addClass('d-none');

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

				}
	
		
		
		if(!result.data.show_vendor_detail_on_woocom_store_page){
			jQuery("#show_vendor_detail_on_woocom_store_page").removeAttr('checked');
		} 
		if(!result.data.show_vendor_detail_on_woocom_product_page){
			jQuery("#show_vendor_detail_on_woocom_product_page").removeAttr('checked');
		}


		if(!result.data.apply_vendor_commission_switch){
			jQuery("#apply_vendor_commission_switch").removeAttr('checked');
			jQuery(".elex-vendor-commission-screen").addClass('d-none');
			jQuery(".elex_global_commission_display_field").removeClass('d-none');

			jQuery(".elex_global_commission_display_field").html('<small> Global Commission : ' + result.data.global_commission_for_each_sale + '%</small>');

		} else{

			jQuery("#apply_vendor_commission_switch").attr('checked', 'checked');
			jQuery(".elex-vendor-commission-screen").removeClass('d-none');
			jQuery(".elex_global_commission_display_field").addClass('d-none');
			jQuery(".elex_global_commission_display_field").html( '<small> Global Commission : ' + result.data.global_commission_for_each_sale + '%</small>');

		}
	

		if(result.data.all_categories_selected_for_commission){
			var all_cat = result.data.all_categories_selected_for_commission;


			// all_cat = Object.keys(all_cat).map((key) => [ all_cat[key]]);

			category_commissions = result.data.category_commission_vendor;


			
			jQuery(".elex_mv_category_commission_body").html('');

			Object.keys(all_cat).forEach(function(term){

				

				jQuery(".elex_mv_category_commission_body").append( jQuery("#elex_mv_category_commission_template").html() );
				
				category_row_num = jQuery(".elex_mv_category_commission_body").data('category_row_num_val') ;
				jq_element_req= jQuery(".elex_mv_category_commission_body").children().last().find(".category_select_dropdown_for_commission_vendor");
				jQuery(".elex_mv_category_commission_body").children().last().find(".category_select_dropdown_for_commission_vendor").attr('name', "categories_selected_forCommission[" + category_row_num + "][]" );
				jQuery(".elex_mv_category_commission_body").children().last().find(".category_select_dropdown_for_commission_vendor").attr('id', "category_select_dropdown_for_commission_vendor_id-" + category_row_num  );
				jQuery(".elex_mv_category_commission_body").children().last().attr('id', "elex_mv_category_commission_template_tr_id-" + category_row_num  );

				jQuery(".elex_mv_category_commission_body").children().last().find(".category_com_dropdown_for_commission_vendor").attr('name', "category_commission_vendor[" + category_row_num + "]" );
				jQuery(".elex_mv_category_commission_body").children().last().find(".category_com_dropdown_for_commission_vendor").attr('value', category_commissions[category_row_num] );

				jQuery(".elex_mv_category_commission_body").children().last().find(".delete_row_category_selected_for_commission_button").attr('id', "delete_row_category_selected_for_commission_button_id-" + category_row_num );

				jQuery(".elex_mv_category_commission_body").data('category_row_num_val', ++category_row_num);


			


				all_cat[term].forEach(function(term_val_str){


				selectedOptions[term_val_str] = true;

				});


				jQuery(".category_select_dropdown_for_commission_vendor").select2({
					placeholder: "Search Category",
					minimumInputLength: 2,
					// initSelection: true, 

					ajax: {
						url: elex_mv_coupon_ajaxurl_object.ajax_url,
						type: 'POST',

						data: function (params) {
							var query = {
								store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
								search_term: params.term,
								status:'publish',
								action: 'api_endpoint_for_categories',
								nonce: elex_mv_coupon_ajaxurl_object.nonce,
							}
							return query;
						},
						processResults: function (response) {
							return {
								results: response.data.map(function (cat) {
									return {
										text: cat.name,
										id: cat.term_id + ":" + cat.name,
										disabled: selectedOptions[cat.term_id + ":" + cat.name]

									}
								}),

							};
						}

					}
				})

				all_cat[term].forEach(function(term_val_str){

					const term_val = term_val_str.split(":");

					jq_element_req.append(
							'<option value="' +
							term_val_str +
							'" selected="selected">' +
							term_val[1]+
							'</option>'
						);
						jq_element_req.trigger( 'change' );

				});

				
				jQuery(".category_select_dropdown_for_commission_vendor").on("select2:select", function(e) {
					var selectedValue = e.params.data.id;


					selectedOptions[selectedValue] = true;


				});
				
				jQuery(".category_select_dropdown_for_commission_vendor").on("select2:unselect", function(e) {
					var deselectedValue = e.params.data.id;
					delete selectedOptions[deselectedValue];
				

				});
				

			});


		}

		if(result.data.all_products_selected_for_commission){
			var all_prod = result.data.all_products_selected_for_commission;

			product_commissions = result.data.product_commission_vendor;
			jQuery(".elex_mv_product_commission_body").html('');

			Object.keys(all_prod).forEach(function(term){


			
				jQuery(".elex_mv_product_commission_body").append( jQuery("#elex_mv_product_commission_template").html() );
				
				product_row_num = jQuery(".elex_mv_product_commission_body").data('product_row_num_val') ;
				jq_element_req= jQuery(".elex_mv_product_commission_body").children().last().find(".product_select_dropdown_for_commission_vendor");
				jQuery(".elex_mv_product_commission_body").children().last().find(".product_select_dropdown_for_commission_vendor").attr('name', "products_selected_forCommission[" + product_row_num + "][]" );
				jQuery(".elex_mv_product_commission_body").children().last().find(".product_select_dropdown_for_commission_vendor").attr('id', "product_select_dropdown_for_commission_vendor_id-" + product_row_num  );
				jQuery(".elex_mv_product_commission_body").children().last().attr('id', "elex_mv_product_commission_template_tr_id-" + product_row_num  );



				jQuery(".elex_mv_product_commission_body").children().last().find(".product_com_dropdown_for_commission_vendor").attr('name', "product_commission_vendor[" + product_row_num + "]" );
				jQuery(".elex_mv_product_commission_body").children().last().find(".product_com_dropdown_for_commission_vendor").attr('value', product_commissions[product_row_num] );

				jQuery(".elex_mv_product_commission_body").children().last().find(".delete_row_product_selected_for_commission_button").attr('id', "delete_row_product_selected_for_commission_button_id-" + product_row_num );

				jQuery(".elex_mv_product_commission_body").data('product_row_num_val', ++product_row_num);


				all_prod[term].forEach(function(term_val_str){


				selectedOptionsProd[term_val_str] = true;

				});


				jQuery(".product_select_dropdown_for_commission_vendor").select2({
					placeholder: "Search Category",
					minimumInputLength: 2,
					// initSelection: true, 

					ajax: {
						url: elex_mv_coupon_ajaxurl_object.ajax_url,
						type: 'POST',

						data: function (params) {
							var query = {
								store_id: jQuery( "#elex_multivendor_vendor_edit_user_id" ).val(),
								search_term: params.term,
								status:'publish',
								action: 'elex_mv_api_endpoint_for_products',
								nonce: elex_mv_coupon_ajaxurl_object.nonce,
							}
							return query;
						},
						processResults: function (response) {
							return {
								results: response.data.map(function (prod) {
									return {
										text: prod.name,
										id: prod.id + ':' + prod.name,
										disabled: selectedOptionsProd[prod.id + ':' + prod.name]

									}
								}),

							};
						}

					}
				})

				all_prod[term].forEach(function(term_val_str){

					const term_val = term_val_str.split(":");

					jq_element_req.append(
							'<option value="' +
							term_val_str +
							'" selected="selected">' +
							term_val[1]+
							'</option>'
						);
						jq_element_req.trigger( 'change' );

				});

			
					
				jQuery(".product_select_dropdown_for_commission_vendor").on("select2:select", function(e) {
					var selectedValue = e.params.data.id;


					selectedOptionsProd[selectedValue] = true;


				});
				
				jQuery(".product_select_dropdown_for_commission_vendor").on("select2:unselect", function(e) {
					var deselectedValue = e.params.data.id;
					delete selectedOptionsProd[deselectedValue];
				

				});

			});


		}

		}

	})
	
})


		

		jQuery("#elex_multivendor_vendor_edit_state").select2({
			placeholder: "Search State",
			minimumInputLength: 2,
		});
		// Function to load states based on the selected country
		function loadStates(countryCode) {
			var stateSelect = jQuery("#elex_multivendor_vendor_edit_state");

			// Clear the current options
			stateSelect.empty();

			// Load new options based on the selected country
			return new Promise(function(resolve, reject) {


				jQuery.ajax({
					url: elex_mv_coupon_ajaxurl_object.ajax_url,
					type: 'post',
					data: {
						action: 'elex_mv_load_states',
						country: countryCode,
						nonce: elexMvManageVendorsParams.load_states_nonce,
					},
					success: function(result) {
						if (result.data) {
							stateSelect.parent().show();

							stateSelect.next(".select2-container").show();
							stateSelect.append(result.data);
							stateSelect.trigger('change');
							resolve(true) // Resolve promise and go to then()

						} else {

							stateSelect.parent().hide();
							stateSelect.next(".select2-container").hide();
							resolve(true) // Resolve promise and go to then()

						}
					},
				});
			});

		}

		// Event listener to load states when the country changes
		jQuery("#elex_multivendor_vendor_edit_country").on('change', function() {
			var countryCode = jQuery(this).val();

			loadStates(countryCode);
		});

		// Load states on page load (if a country is already selected)
		var countryCode = jQuery("#elex_multivendor_vendor_edit_country").val();
		if (countryCode) {
			loadStates(countryCode);
		}

		var input_phone_number = window.intlTelInput( document.querySelector("#elex_multivendor_vendor_edit_phone_number"), {
			autoPlaceholder: "polite",
		} );

		jQuery("#elex_multivendor_vendor_edit_dial_code").val(input_phone_number.getSelectedCountryData().dialCode);
		jQuery("#elex_multivendor_vendor_edit_country_iso").val(input_phone_number.getSelectedCountryData().iso2);

		document.querySelector("#elex_multivendor_vendor_edit_phone_number").addEventListener("countrychange", function() {
			jQuery("#elex_multivendor_vendor_edit_dial_code").val(input_phone_number.getSelectedCountryData().dialCode);
			jQuery("#elex_multivendor_vendor_edit_country_iso").val(input_phone_number.getSelectedCountryData().iso2);
		});


		jQuery(".elex-multi-vendor-for-woocommerce-vendor-edit-filetag-btn").click(function(e) {
			e.preventDefault();
			// changeImageVendorEdit();
			jQuery("#elex-multi-vendor-for-woocommerce-vendor-edit-filetag").trigger('click');
		});

		jQuery("#elex-multi-vendor-for-woocommerce-vendor-edit-filetag").on('change', function() {
			if (this.files.length) {
				const reader = new FileReader();

				reader.onload = function(e) {
					elexPreviewImageVendorEditUpload.setAttribute('src', e.target.result);
				}
				reader.readAsDataURL(this.files[0]);
			}
			jQuery("#elex_multivendor_vendor_edit_deleted_image").val('');

		});

		jQuery(".elex-multi-vendor-for-woocommerce-vendor-edit-filetag-delete-btn").click(function() {
			elexPreviewImageVendorEditUpload.setAttribute('src', elexMvManageVendorsParams.plugin_url + 'assets/images/default-store-icon.svg');
			jQuery("#elex-multi-vendor-for-woocommerce-vendor-edit-filetag").val('');
			jQuery("#elex_multivendor_vendor_edit_deleted_image").val(true);

		});

		jQuery('.vendor-edit-page-save-btn').click(function(e) {
			e.preventDefault();
			var link = elex_mv_admin_ajaxurl_object.ajax_url;
			var formData;
			link += "?action=elex_mv_edit_vendor_details";
			var formData = new FormData(document.getElementById("vendor-edit-details-form"));

			formData.set('edit_page', true);

			if (jQuery(this).data('button-id') == 1) {
				formData.set('first_page', 'yes');
			}
			if (jQuery(this).data('button-id') == 3) {
				jQuery(".elex-multi-vendor-for-woocommerce-loader-vendor").removeClass('d-none')
			}
			if (jQuery(this).data('button-id') == 4) {
				formData.set('for_checkBox', true);
			}

			dis = this;

			jQuery(this).prop('disabled', true);

			jQuery.ajax({
				url: link,
				type: 'POST',
				data: formData,
				enctype: 'multipart/form-data',
				processData: false,
				contentType: false,
				success: function(result) {

					jQuery(".elex-multi-vendor-for-woocommerce-loader-vendor").addClass('d-none');
					jQuery('.vendor-edit-page-save-btn').prop('disabled', false);
					if (result.success) {

						jQuery(".elex-multi-vendor-for-woocommerce-add-vendor-form-next-btn").attr('elex_mv_id_vendor', result.data);
						var additem = document.getElementById('elex_edit_vendor_admin_toast');
						jQuery(additem).find('.elex-toast-icon-success').removeClass('d-none');
						jQuery(additem).find('.elex-toast-icon-error').addClass('d-none');
						jQuery("#elex_edit_vendor_admin_toast_msg").html('Updated Successfully');

						var toast = new bootstrap.Toast(additem);
						toast.show();
					} else {
						var additem = document.getElementById('elex_edit_vendor_admin_toast');
						jQuery(additem).find('.elex-toast-icon-success').addClass('d-none');
						jQuery(additem).find('.elex-toast-icon-error').removeClass('d-none');
						var errorMsg = window.elexMvFormatResponseError(result);
						jQuery("#elex_edit_vendor_admin_toast_msg").html(errorMsg);
						var toast = new bootstrap.Toast(additem);
						toast.show();
					}

				},
				error: function(xhr) {
					jQuery(".elex-multi-vendor-for-woocommerce-loader-vendor").addClass('d-none');
					jQuery('.vendor-edit-page-save-btn').prop('disabled', false);

					var errorMsg = window.elexMvFormatResponseError(xhr);

					var additem = document.getElementById('elex_edit_vendor_admin_toast');
					jQuery(additem).find('.elex-toast-icon-success').addClass('d-none');
					jQuery(additem).find('.elex-toast-icon-error').removeClass('d-none');
					jQuery("#elex_edit_vendor_admin_toast_msg").html(errorMsg);

					var toast = new bootstrap.Toast(additem);
					toast.show();
				}



			});

		})


		jQuery('.elex-multi-vendor-for-woocommerce-edit-vendor-close-btn').click(function() {
			jQuery(".elex-multi-vendor-for-woocommerce-popup").removeClass("active");
			location.reload();
		})

	})
