jQuery( '#clear-filter-for-manage-vendor-btn' ).click( function( e ) {
	e.preventDefault();

	const u = new URL( location );
	let params = new URLSearchParams( u.search );
	u.searchParams.delete( 'search_term' );
	u.searchParams.delete( 'status' );
	u.searchParams.delete( 'enabled' );
	u.searchParams.delete( 'from_date' );
	u.searchParams.delete( 'to_date' );
	u.searchParams.delete( 'vendor' );

	window.location.replace( u );
} );


function changeProductStatusFuncAdmin( id ) {
	var form;

	var toggle_status_page_link = elex_mv_admin_ajaxurl_object.ajax_url;
	toggle_status_page_link = toggle_status_page_link + '?action=elex_mv_toggle_page_link';

	if ( jQuery( '#elex_mv_check_enable_' + id ).is( ':checked' ) ) {
		form = {
			id: id,
			visibility: 'yes',
			nonce: elex_mv_coupon_ajaxurl_object.nonce
		};
	} else {
		form = {
			id: id,
			visibility: 'no',
			nonce: elex_mv_coupon_ajaxurl_object.nonce
		};
	}
	jQuery.ajax( {
		url: toggle_status_page_link,
		type: 'POST',
		data: form,
		enctype: 'text/plain',
		success: function() {
			var additem = document.getElementById( 'elex_msging_for_bulk_action' );
			jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Status changed successfully' );
			var toast = new bootstrap.Toast( additem );
			toast.show();
			const myTimeout = setTimeout( function myStopFunction() {
				location.reload();
			}, 2000 );
		},
		error: function() {
			var additem = document.getElementById( 'elex_msging_for_bulk_action' );
			jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Status not changed' );
			var toast = new bootstrap.Toast( additem );
			toast.show();
			const myTimeout = setTimeout( function myStopFunction() {
				location.reload();
			}, 2000 );
		}
	} );
}

jQuery( '#elex-multi-vendor-for-woocommerce-admin-add-product-redirect-btn' ).on( 'click', function() {
	var redirectURL = jQuery( this ).find( 'a' ).attr( 'href' );
	window.location.href = redirectURL;
} );

jQuery( '#elex-multi-vendor-for-woocommerce-admin-add-product-popup-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-admin-add-product' ).addClass( 'active' );
	jQuery( '.elex-multi-vendor-for-woocommerce-admin-add-product-form-next-btn' ).removeClass( 'disabled' );
} );

jQuery( '#elex-multi-vendor-for-woocommerce-admin-add-product-close-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-admin-add-product' ).removeClass( 'active' );
} );

validation_for_forms_func( '#elex-admin-add-vendor-product-name', /^[A-Za-z][ A-Za-z0-9_]{0,29}$/, '#elex-admin-add-vendor-product-name-error', 'Enter Proper Name' );
validation_for_forms_func( '#elex-admin-add-vendor-product-price', /^[0-9]{0,29}$/, '#elex-admin-add-vendor-product-price-error', 'Enter Proper Name' );
validation_for_forms_func( '#elex-admin-add-vendor-product-commission', /^[0-9]{0,29}$/, '#elex-admin-add-vendor-product-commission-error', 'Enter Proper Name' );

function validation_for_forms_func( id_for_input_element, regex_for_string, id_for_error_element, error_string ) {
	jQuery( id_for_input_element ).keyup( function() {
		const regex = regex_for_string;
		if ( ! regex.test( this.value ) ) {
			jQuery( id_for_error_element ).html( '<div class="text-danger">' + error_string + '</div>' );
		} else {
			jQuery( id_for_error_element ).html( '' );
		}
	} );
}

jQuery( document ).ready( function() {

	var popoverTriggerList = [].slice.call( document.querySelectorAll( '[data-bs-toggle="popover"]' ) );
	var popoverList = popoverTriggerList.map( function( popoverTriggerEl ) {
		return new bootstrap.Popover( popoverTriggerEl, { container: '.elex-multi-vendor-for-woocommerce-wrap' } );
	} );

	// search_term is passed via wp_localize_script as elexMvAdminProductsParams.search_term
	var searchTerm = elexMvAdminProductsParams.search_term;
	var newOption = jQuery( '<option selected=\'selected\'></option>' ).val( searchTerm ).text( searchTerm );

	jQuery( '#product-select-for-search' ).append( newOption ).trigger( 'change' );

	jQuery( '#product-select-for-search' ).select2( {
		placeholder: 'Search Product',
		minimumInputLength: 2,
		ajax: {
			url: elex_mv_coupon_ajaxurl_object.ajax_url,
			type: 'POST',
			data: function( params ) {
				var query = {
					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.name,
						};
					} ),
				};
			}
		}
	} );

	jQuery( '#elex-search-product-admin-button' ).click( function() {
		const u = new URL(location);
		const search_val = jQuery( '#product-select-for-search' ).val();
		const nonce = jQuery( '#admin_product_page_nonce' ).val();
		u.searchParams.set( 'search_term', search_val );
		u.searchParams.set( 'admin_product_page_nonce', nonce );
		window.location.replace( u );
	} );

	jQuery( '#elex-search-product-admin-button-cross' ).click( function() {
		const u = new URL( location );
		const nonce = jQuery( '#admin_product_page_nonce' ).val();
		u.searchParams.delete( 'search_term' );
		u.searchParams.set( 'admin_product_page_nonce', nonce );
		window.location.replace( u );
	} );

	jQuery( '#vendor-select-for-filter' ).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,
					action: 'elex_mv_api_endpoint_for_vendors',
					nonce: elex_mv_coupon_ajaxurl_object.nonce,
				};
				return query;
			},
			processResults: function( response ) {
				return {
					results: response.data.map( function( store ) {
						return {
							text: store.post_title,
							id: store.ID,
						};
					} ),
				};
			}
		}
	} );

	jQuery( '#apply-filter-by-vendor-btn' ).click( function() {
		jQuery( '#elex-multi-vendor-for-woocommerce-add-admin-product-filter' ).addClass( 'active' );
	} );
	jQuery( '.elex-multi-vendor-for-woocommerce-add-admin-product-filter-close-btn' ).click( function() {
		jQuery( '#elex-multi-vendor-for-woocommerce-add-admin-product-filter' ).removeClass( 'active' );
	} );

	jQuery( '#main-check-box-product-admin' ).change( function() {
		if ( jQuery( '#main-check-box-product-admin' ).is( ':checked' ) ) {
			jQuery( '.product-individ-admin' ).prop( 'checked', true );
		} else {
			jQuery( '.product-individ-admin' ).prop( 'checked', false );
		}
	} );

	jQuery( '#apply-bulk-action-product-admin-btn' ).click( function() {
		status = jQuery( '#bulk-status-vendor-product-admin' ).val();

		req_product_ids = jQuery( 'input[name="product-individ-admin[]"]:checked' )
			.map( function() {
				return jQuery( this ).val();
			} ).get();

		const params = {
			product_ids: req_product_ids,
			status: status,
			action: 'elex_mv_bulk_action_for_product_vendor_admin',
			nonce: elex_mv_coupon_ajaxurl_object.nonce
		};

		jQuery.post( ajaxurl, params, function( res ) {
			if ( res.success ) {
				var additem = document.getElementById( 'elex_msging_for_bulk_action' );
				var toast = new bootstrap.Toast( additem );
				toast.show();
				setTimeout( function() {
					location.reload();
				}, 2000 );
			} else {
				var additem = document.getElementById( 'elex_msging_for_bulk_action' );
				jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'The Status not changed' );
				var toast = new bootstrap.Toast( additem );
				toast.show();
				setTimeout( function() {
					location.reload();
				}, 2000 );
			}
		} );
	} );

	jQuery( '.elex_mv_approval_product_from_admin_side' ).click( function() {
		admin_url = elex_mv_admin_ajaxurl_object.ajax_url;
		link = admin_url + '?action=elex_mv_approval_product_from_admin_side';

		data = {
			status: jQuery( this ).data( 'status' ),
			prod_id: jQuery( this ).data( 'product-id' ),
			nonce: elex_mv_coupon_ajaxurl_object.nonce
		};

		if ( jQuery( this ).data( 'status' ) == 'reject' ) {
			var additem = document.getElementById( 'elex_popup_vendor_prod_reject_item_toast' );
			var toast = new bootstrap.Toast( additem );
			toast.show();
			jQuery( '#elex-multi-vendor-for-woocommerce-reject-vendor-prod-confirm-btn' ).data( 'status', data['status'] );
			jQuery( '#elex-multi-vendor-for-woocommerce-reject-vendor-prod-confirm-btn' ).data( 'product-id', data['prod_id'] );
		} else {
			jQuery.ajax( {
				url: link,
				type: 'POST',
				data: data,
				enctype: 'text/plain',
				success: function( result ) {
					if ( result.success ) {
						var additem = document.getElementById( 'elex_msging_for_bulk_action' );
						jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Status Changed' );
						var toast = new bootstrap.Toast( additem );
						toast.show();
						setTimeout( function() {
							location.reload();
						}, 2000 );
					} else {
						var additem = document.getElementById( 'elex_msging_for_bulk_action' );
						jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Failed' );
						var toast = new bootstrap.Toast( additem );
						toast.show();
						setTimeout( function() {
							location.reload();
						}, 2000 );
					}
				}
			} );
		}
	} );

	jQuery( '#elex-multi-vendor-for-woocommerce-reject-vendor-prod-confirm-btn' ).click( function() {
		admin_url = elex_mv_admin_ajaxurl_object.ajax_url;
		link = admin_url + '?action=elex_mv_approval_product_from_admin_side';
		message_inp = jQuery( '#elex-multi-vendor-for-woocommerce-reject-vendor-product-input' ).val();
		data = {
			status: jQuery( this ).data( 'status' ),
			prod_id: jQuery( this ).data( 'product-id' ),
			nonce: elex_mv_coupon_ajaxurl_object.nonce,
			message: message_inp
		};

		jQuery.ajax( {
			url: link,
			type: 'POST',
			data: data,
			enctype: 'text/plain',
			success: function( result ) {
				if ( result.success ) {
					var additem = document.getElementById( 'elex_msging_for_bulk_action' );
					jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Status Changed' );
					var toast = new bootstrap.Toast( additem );
					toast.show();
					setTimeout( function() {
						location.reload();
					}, 4000 );
				} else {
					var additem = document.getElementById( 'elex_msging_for_bulk_action' );
					jQuery( '#elex_msging_for_bulk_action_msg' ).html( 'Failed' );
					var toast = new bootstrap.Toast( additem );
					toast.show();
					setTimeout( function() {
						location.reload();
					}, 4000 );
				}
			}
		} );
	} );

	jQuery( '.elex-admin-add-vendor-product-vendor-name' ).select2( {
		minimumInputLength: 2,
		placeholder: 'Assign Store Name',
		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: elex_mv_coupon_ajaxurl_object.nonce,
				};
				return query;
			},
			processResults: function( response ) {
				return {
					results: response.data.map( function( store ) {
						return {
							text: store.post_title,
							id: store.ID,
						};
					} ),
				};
			}
		}
	} );

	jQuery( '#elex-admin-add-vendor-product-category' ).select2( {
		minimumInputLength: 2,
		placeholder: 'Enter Categories',
		ajax: {
			url: elex_mv_coupon_ajaxurl_object.ajax_url,
			type: 'POST',
			data: function( params ) {
				var query = {
					search_val: params.term,
					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,
						};
					} ),
				};
			}
		}
	} );

	jQuery( '#elex-multi-vendor-for-woocommerce-filetag-admin-image' ).change( function() {
		changeImage( this );
	} );

	function changeImage( input ) {
		var reader;
		if ( input.files && input.files[0] ) {
			reader = new FileReader();
			reader.onload = function( e ) {
				jQuery( '#elexPreviewImageUploadAdminImage' ).attr( 'src', e.target.result );
				jQuery( '.elex-multi-vendor-for-woocommerce-prod-img-name-div-admin' ).removeClass( 'd-none' );
				jQuery( '.elex-multi-vendor-for-woocommerce-prod-img-name-admin' ).html( input.files[0].name );
			};
			reader.readAsDataURL( input.files[0] );
		}
	}

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

	jQuery( '.elex-multi-vendor-for-woocommerce-admin-add-product-form-next-btn' ).click( function( e ) {
		e.preventDefault();
		var dis = jQuery( this );
		var link = elex_mv_admin_ajaxurl_object.ajax_url;
		link += '?action=elex_mv_add_admin_vendor_product_details';
		var formData = new FormData( document.getElementById( 'elex-multi-vendor-for-woocommerce-admin-vendor-product-form' ) );
		formData.append( 'nonce', elex_mv_coupon_ajaxurl_object.nonce );
		jQuery( this ).addClass( 'disabled' );

		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-admin-add-product-form-next-btn' ).addClass( 'disabled' );
				var additem = document.getElementById( 'elex_additem_toast_added_product' );
				jQuery( '#elex_additem_toast_added_product_msg' ).html( 'New Product Added Successfully' );
				var toast = new bootstrap.Toast( additem );
				toast.show();
				setTimeout( function() {
					location.reload();
				}, 2000 );
			},
			error: function( status, errorThrown ) {
				dis.removeClass( 'disabled' );
				add_product_error_obj = status.responseJSON.data;
				for ( const key in add_product_error_obj ) {
					function loop_for_html_error( obj ) {
						string = '';
						for ( var i = 0; i < obj.length; i++ ) {
							string += ' ' + obj[i];
						}
						return string;
					}

					if ( add_product_error_obj[key] == null ) {
						continue;
					} else if ( key == 'regular_price' ) {
						jQuery( '#elex-admin-add-vendor-product-price-error' ).html( loop_for_html_error( add_product_error_obj[key] ) );
					} else if ( key == 'sale_price' ) {
						jQuery( '#elex-admin-add-vendor-product-sale-price-error' ).html( loop_for_html_error( add_product_error_obj[key] ) );
					} else if ( key == 'product_name' ) {
						jQuery( '#elex-admin-add-vendor-product-name-error' ).html( loop_for_html_error( add_product_error_obj[key] ) );
					}
				}
			}
		} );
	} );

} );

jQuery( '#elex_additem_toast_added_btn_ok' ).click( function() {
	location.reload();
} );
