window.elexMvFormatResponseError = function(responseOrXhr, defaultMsg) {
	var errorMsg = defaultMsg || 'An error occurred. Please try again.';
	var response = responseOrXhr;

	if (responseOrXhr && typeof responseOrXhr.responseJSON !== 'undefined') {
		response = responseOrXhr.responseJSON;
	} else if (responseOrXhr && typeof responseOrXhr.responseText !== 'undefined') {
		return responseOrXhr.responseText;
	}

	if (response && response.data) {
		var data = response.data;
		if (typeof data === 'string') {
			errorMsg = data;
		} else if (typeof data === 'object' && data !== null) {
			errorMsg = Object.keys(data).map(function(key) {
				var val = data[key];
				return Array.isArray(val) ? val.join('<br>') : val;
			}).join('<br>');
		}
	} else if (typeof response === 'string') {
		errorMsg = response;
	}

	return errorMsg;
};

jQuery(function () {
	jQuery('.approve').click(function (e) {
		e.preventDefault();

		const params = {
			vendor_ids: [jQuery(this).data('vendor-id')],
			status: 'publish',
			nonce : elexMvManageVendorsParams.nonce
		};

		params.action = 'elex_mv_update_vendor_status';
		
		jQuery.post(ajaxurl, params, function (res) {
			if (res.success) {
				var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
				jQuery("#toast_message_mv_admin_bulk").html('Approved');
				var toast = new bootstrap.Toast(additem)
				toast.show()  
				setTimeout(function(){
					window.location.reload();
				}, 2000);
			} else {
				var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
				jQuery("#toast_message_mv_admin_bulk").html('Failed');
				var toast = new bootstrap.Toast(additem)
				toast.show()  
				setTimeout(function(){
					window.location.reload();
				}, 2000);
			}
		});
	});

	jQuery('.reject').click(function(e){
		e.preventDefault();

		const params = {
			vendor_ids: [jQuery(this).data('vendor-id')],
			status: 'reject',
			nonce : elexMvManageVendorsParams.nonce	

		};
		if( jQuery(this).data('status') == 'reject' ){

		var additem = document.getElementById("elex_popup_vendor_reject_item_toast");


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

		jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").data('status', params['status']);
		jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").data('vendor-id', params['vendor_ids']);
		}else{
			params.action = 'elex_mv_update_vendor_status';
			jQuery.post(ajaxurl, params, function (res) {
				if (res.success) {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
					jQuery("#toast_message_mv_admin_bulk").html('Rejected');
					var toast = new bootstrap.Toast(additem)
					toast.show()  
					setTimeout(function(){
					window.location.reload();
					}, 2000);
				} else {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
					jQuery("#toast_message_mv_admin_bulk").html('Failed');
					var toast = new bootstrap.Toast(additem)
					toast.show()  
					setTimeout(function(){
					window.location.reload();
					}, 2000);
				}
			});
		}
		
	})

	jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").click(function () {
			message_inp = jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-input").val();
			const params = {
				vendor_ids: jQuery(this).data('vendor-id'),
				status: 'reject',
				nonce : elexMvManageVendorsParams.nonce,
				message : message_inp

			};
			params.action = 'elex_mv_update_vendor_status';
			jQuery.post(ajaxurl, params, function (res) {
				if (res.success) {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
					jQuery("#toast_message_mv_admin_bulk").html('Rejected');
					var toast = new bootstrap.Toast(additem)
					toast.show()  
					setTimeout(function(){
					window.location.reload();
					}, 2000);
				} else {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast');
					jQuery("#toast_message_mv_admin_bulk").html('Failed');
					var toast = new bootstrap.Toast(additem)
					toast.show()  
					setTimeout(function(){
					window.location.reload();
					}, 2000);
				}
			});
		})

})


jQuery(document).ready(function() {

	if ( elexMvManageVendorsParams.vendor_search ) {
		var newOption = jQuery("<option selected='selected'></option>").val(elexMvManageVendorsParams.vendor_search).text(elexMvManageVendorsParams.vendor_search);
		jQuery("#vendor-select-for-search").append(newOption).trigger('change');
	}

	jQuery("#vendor-select-for-search").select2({
		placeholder: "Search Vendor",
		minimumInputLength: 2,
		ajax: {
			url: elex_mv_coupon_ajaxurl_object.ajax_url,
			type: 'GET',

			data: function (params) {
				var query = {
					vendor_search: params.term,
					// status:'publish',
					action: 'elex_mv_api_endpoint_for_vendors',
					nonce: elexMvManageVendorsParams.nonce,
				}
				return query;
			},
			processResults: function (response) {
				return {
					results: response.data.map(function (store) {
						return {
							text: store.post_title,
							id: store.post_title,
						}
					}),

				};
			}

		}

	});

	

	jQuery("#search-vendor-btn").click(function(){
		const search_val = jQuery("#vendor-select-for-search").val();
		if(!search_val){
			return;
		}
		const u= new URL(location);
		const nonce = jQuery("#admin_vendor_page_nonce").val();
		u.searchParams.set('vendor_search', search_val)
		u.searchParams.set('admin_vendor_page_nonce', nonce)
		window.location.replace(u);
	})

	jQuery("#vendor-select-for-search-cross").click(function(){
		const u = new URL(location);
		const nonce = jQuery("#admin_vendor_page_nonce").val();
		u.searchParams.set('admin_vendor_page_nonce', nonce)
		u.searchParams.delete('vendor_search')
		window.location.replace(u);
	})
  


	jQuery('.vendor-all-checkbox').change(function(){
		if( jQuery('.vendor-all-checkbox').is(":checked")){
			jQuery('.vendor-individ-checkbox').prop('checked', true)
		}else{
			jQuery('.vendor-individ-checkbox').prop('checked', false);

		}
	})

	jQuery('#manage-vendor-bulk-action-select-btn').click(function(){


		req_vendor_ids = jQuery("input[name='vendor-individ-checkbox[]']")
				.map(function(){
				if(jQuery(this).is(":checked")){

					return jQuery(this).val();
				}
			}).get();

			

		const params = {
					vendor_ids: req_vendor_ids,
					status: jQuery("#manage-vendor-bulk-action-select").val(),
					action : 'elex_mv_bulk_action_for_manage_vendor',
					nonce : elexMvManageVendorsParams.nonce
		};
		if( jQuery("#manage-vendor-bulk-action-select").val() == 'reject' ){
				var additem = document.getElementById("elex_popup_vendor_reject_item_toast");
				var toast = new bootstrap.Toast(additem);
				toast.show();

				jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").data('status', params['status']);
				jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").data('vendor-id', params['vendor_ids']);
		}else{
			jQuery.post(ajaxurl, params, function (res) {
				if (res.success) {

					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Vendor Status Changed');
					var toast = new bootstrap.Toast(additem)
					toast.show()					
					setTimeout(function(){
						window.location.reload();
					}, 3000);
				} else {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Error in changing Status');
					var toast = new bootstrap.Toast(additem)
					toast.show()						
					setTimeout(function(){
						window.location.reload();
					}, 3000);
				}
			});
		}
		
	})
	jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-confirm-btn").click(function () {
		message_inp = jQuery("#elex-multi-vendor-for-woocommerce-reject-vendor-input").val();
		const params = {
					vendor_ids: req_vendor_ids,
					status: jQuery("#manage-vendor-bulk-action-select").val(),
					action : 'bulk_action_for_manage_vendor',
					message : message_inp,
					nonce : elexMvManageVendorsParams.nonce
		};
		jQuery.post(ajaxurl, params, function (res) {
				if (res.success) {

					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Vendor Status Changed');
					var toast = new bootstrap.Toast(additem)
					toast.show()					
					setTimeout(function(){
						window.location.reload();
					}, 3000);
				} else {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Error in changing Status');
					var toast = new bootstrap.Toast(additem)
					toast.show()						
					setTimeout(function(){
						window.location.reload();
					}, 3000);
				}
			});
	});

	jQuery("#elex-multi-vendor-for-woocommerce-vendor-filetag").change(function() {
		
		changeImageVendor(this);
	});

	function changeImageVendor(input) {
		var reader;

		if (input.files && input.files[0]) {
			reader = new FileReader();

			reader.onload = function(e) {
				elexPreviewImageVendorUpload.setAttribute('src', e.target.result);

				jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-div-ven').removeClass('d-none');
				jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-ven').html(input.files[0].name);
			}
			reader.readAsDataURL(input.files[0]);
		}
	};

	jQuery('.elex-multi-vendor-for-woocommerce-product-form-remove-image-ven').click(function(){
		jQuery('#elexPreviewImageVendorUpload').attr('src', elex_mv_coupon_ajaxurl_object.plugin_path + 'images/default.jpg');
		jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-div-ven').addClass('d-none');
		jQuery("#elex-multi-vendor-for-woocommerce-vendor-filetag").val(''); 
	})

	jQuery(".elex-enable-disable-switch-val").change(function(e){

		const params = {
					vendor_ids: [jQuery(this).data('vendor-id')],
					status: 'publish'
		};
		if (jQuery(this).is(":checked")){
				params['status'] = 'publish'
		}else{
			params['status'] = 'private';
		}
		params.action = 'elex_mv_enable_disable_vendor';
		params.nonce = elexMvManageVendorsParams.nonce;

		jQuery.post(ajaxurl, params, function (res) {
				if (res.success) {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Vendor Status Changed');
					var toast = new bootstrap.Toast(additem)
					toast.show()
					setTimeout(function(){
						window.location.reload();
					}, 3000);

				} else {
					var additem = document.getElementById('elex_mv_vendor_buk_action_admin_toast')
					jQuery("#toast_message_mv_admin_bulk").html('Vendor Status Not Changed');
					var toast = new bootstrap.Toast(additem)
					toast.show()
					setTimeout(function(){
						window.location.reload();
					}, 3000);
				}
			});
	})


	
	jQuery("#elex-multi-vendor-for-woocommerce-add-product").removeClass("active");

	jQuery(".elex-multi-vendor-for-woocommerce-add-vendor-btn").click(function() {
		jQuery("#elex-multi-vendor-for-woocommerce-add-vendor").addClass("active");
	})

	jQuery(".elex-multi-vendor-for-woocommerce-close-vendor-btn").click(function() {
		jQuery("#elex-multi-vendor-for-woocommerce-add-vendor").removeClass("active");
		jQuery("#elex-multi-vendor-for-woocommerce-add-product").removeClass("active");
	  
		jQuery('#vendor-add-details-form')[0].reset();

		jQuery('#elexPreviewImageUpload').attr('src', "elexMvManageVendorsParams.plugin_url + 'images/default.jpg'");
	});

	jQuery('ul.elex-multi-vendor-for-woocommerce-add-vendor-tabs').find('button[data-bs-toggle="tab"]').on('shown.bs.tab', function(e) {
		//update progress

		var step = jQuery(e.target).data('step');
		var percent = (parseInt(step) /2) * 100;
		// jQuery('.progress-bar').css({
		//     width: percent + '%'
			
		// }); //e.relatedTarget // previous tab
		jQuery('.progress-vendor').children('.progress-bar-active').css({width: percent+ '%'  });
		jQuery('.progress-vendor').children('.progress-bar-inactive').css({  width: (100 - percent ) + '%' });
		

	})


	$prev_count = jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').children('button').data('step');
	if($prev_count === 0){
		jQuery('.elex-multi-vendor-for-woocommerce-add-product-form-prev-btn').addClass('d-none')
	}else{
		jQuery('.elex-multi-vendor-for-woocommerce-add-product-form-prev-btn').removeClass('d-none')
	}


	jQuery('.elex-multi-vendor-for-woocommerce-add-product-form-prev-btn').click(function() {
		// jQuery('.nav-tabs > .nav-item > .active').parent().prev('li').find('button').trigger('click');
		$fas = jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').children('button').data('step');
		
		$parent_id_no = ('#elex-multi-vendor-for-woocommerce-add-vendor-'+($fas + 1 ));
		jQuery($parent_id_no).removeClass("active");
		$prev_parent_id_no = ('#elex-multi-vendor-for-woocommerce-add-vendor-'+($fas));
		jQuery($prev_parent_id_no).addClass("active");
		jQuery($prev_parent_id_no).removeClass("elex-multi-vendor-for-woocommerce-on");
		jQuery($prev_parent_id_no).find('button').click();

		$prev_count = jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').children('button').data('step');
		if($prev_count === 0){
			jQuery(this).addClass('d-none')
		}else{
			jQuery(this).removeClass('d-none')
		}
	
		
	

	});

 
	
	jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-form-next-btn').click(function(e){
		e.preventDefault();
		var link = elex_mv_admin_ajaxurl_object.ajax_url;
		var formData;	
		link += "?action=elex_mv_add_vendor_details" ;
		var formData = new FormData(document.getElementById("vendor-add-details-form"));
		
		if(jQuery(this).attr('elex_mv_id_vendor')){
			formData.set('user_id', jQuery(this).attr('elex_mv_id_vendor'));
		}
		if(jQuery(this).data('button-id') == 1){
			formData.set('bypass_validation', false);
		}
		if(jQuery(this).data('button-id') == 1){
			formData.set('for_checkBox', true);
		}
		dis=this;
		if(jQuery(this).data('button-id') == 1){
			formData.set('first_page','yes')

		}


		if(jQuery(this).data('button-id') == 3){
			// formData.set('is_setup_finished', true);
			formData.set('status','publish')
			formData.set('add_vendor','yes')

			formData.set('nonce',elexMvManageVendorsParams.nonce);
		}else{
			formData.set('status','draft')

		}
	   
	   
		jQuery.ajax({
		url: link ,
		type: 'POST',
		data: formData,
		enctype: 'multipart/form-data',
		processData: false,
		contentType: false,
		success: function(result) {
				if(result.success && result.success!==false){
					
					jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').next('li.nav-item').children('button').trigger('click');
					jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').next('li.nav-item').addClass("active");
					jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').prev('li.nav-item').addClass("elex-multi-vendor-for-woocommerce-on");
					jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').prev('li.nav-item').removeClass("active");
					$prev_count = jQuery('.elex-multi-vendor-for-woocommerce-add-vendor-tabs > .nav-item.active').children('button').data('step');
					if($prev_count === 0){
						jQuery('.elex-multi-vendor-for-woocommerce-add-product-form-prev-btn').addClass('d-none')
					}else{
						jQuery('.elex-multi-vendor-for-woocommerce-add-product-form-prev-btn').removeClass('d-none')
					}

					jQuery(".elex-multi-vendor-for-woocommerce-add-vendor-form-next-btn").attr('elex_mv_id_vendor',result.data);

					var additem = document.getElementById('elex_add_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_add_vendor_admin_toast_msg_text").html('Updated Successfully');

					var toast = new bootstrap.Toast(additem)
					toast.show();
					
					if(jQuery(dis).data('button-id') == 3){
						location.reload();
					}
				}else{
					var errorMsg = window.elexMvFormatResponseError(result);
					var additem = document.getElementById('elex_add_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_add_vendor_admin_toast_msg_text").html(errorMsg);
					var toast = new bootstrap.Toast(additem)
					toast.show();
				}
		},
		error: function(xhr, status, error) {
			var errorMsg = window.elexMvFormatResponseError(xhr);
			var additem = document.getElementById('elex_add_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_add_vendor_admin_toast_msg_text").html(errorMsg);
			var toast = new bootstrap.Toast(additem)
			toast.show();
		}
		});
		
	});

});
