
jQuery( document ).ready( function() {

	var input_phone_number = window.intlTelInput( document.querySelector( '#elex-vendor-onboard-number' ), {
		autoPlaceholder: 'polite',
	} );

	jQuery( '#elex-vendor-onboard-dial_code' ).val( input_phone_number.getSelectedCountryData().dialCode );
	jQuery( '#elex-vendor-onboard-country_iso' ).val( input_phone_number.getSelectedCountryData().iso2 );

	document.querySelector( '#elex-vendor-onboard-number' ).addEventListener( 'countrychange', function() {
		jQuery( '#elex-vendor-onboard-dial_code' ).val( input_phone_number.getSelectedCountryData().dialCode );
		jQuery( '#elex-vendor-onboard-country_iso' ).val( input_phone_number.getSelectedCountryData().iso2 );
	} );

	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list > li > button[data-bs-toggle="pill"]' ).on( 'shown.bs.tab', function( e ) {
		// update progress
		var step    = jQuery( e.target ).data( 'step' );
		var percent = ( parseInt( step ) / 2 ) * 100;
		jQuery( '.elex-multi-vendor-for-woocommerce-onboard-inactive-progress-bar' ).css( { width: ( 100 - percent ) + '%' } );
		jQuery( '.elex-multi-vendor-for-woocommerce-onboard-active-progress-bar' ).css( { width: percent + '%' } );
		// e.relatedTarget // previous tab

		if ( step != 0 ) {
			jQuery( '.elex-multi-vendor-for-woocommerce-onboard-back-btn' ).show();
		} else {
			jQuery( '.elex-multi-vendor-for-woocommerce-onboard-back-btn' ).hide();
		}
	} );

	let page_num = localStorage.getItem( 'setup-page' ) || 0;
	page_num++;

	for ( let i = 0; i < page_num; i++ ) {
		jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list button' ).eq( i ).prop( 'disabled', false ).addClass( 'elex-multi-vendor-for-woocommerce-on' );
	}
	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list button' ).eq( --page_num ).click();

} );

jQuery( '#vendor-success-onboarding-btn' ).click( function() {

	const u = new URL( location );
	let params = new URLSearchParams( u.search );
	u.searchParams.set( 'menu_tab', 'dashboard' );
	window.location.replace( u );
} );

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

	var formData;
	var link = elexMvOnboardUiParams.ajax_url;
	link    += '?action=elex-multi-vendor-for-woocommerce-vendor-installation-setup';

	if (
		jQuery( '#elex-vendor-onboard-store-full-addr' ).val() == null ||
		jQuery( '#elex-vendor-onboard-store-url' ).val() == null ||
		jQuery( '#elex-vendor-onboard-city' ).val() == null ||
		jQuery( '#elex-vendor-onboard-postal' ).val() == null ||
		jQuery( '#elex-vendor-onboard-country' ).val() == null
	) {
		return;
	}

	localStorage.setItem( 'setup-page', jQuery( this ).data( 'id' ) );

	var formData = new FormData( document.getElementById( 'elex-multi-vendor-for-woocommerce-vendor-installation-setup' ) );

	if ( jQuery( this ).data( 'id' ) == 2 ) {
		formData.append( 'is_setup_finished', 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-onboard-list.nav > li > .nav-link.active' ).parent().next( 'li' ).find( 'button' ).trigger( 'click' );
			jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active' ).addClass( 'elex-multi-vendor-for-woocommerce-on' );
			var additem = document.getElementById( 'elex_vendor_onboard_toast' );
			var toast   = new bootstrap.Toast( additem );
			toast.show();
		},
		error: function( result ) {
			console.log( result );
		},
	} );

} );

jQuery( '.elex-multi-vendor-for-woocommerce-onboard-back-btn' ).hide();

jQuery( '.elex-multi-vendor-for-woocommerce-confirm-admin-setup-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active' ).parent().next( 'li' ).find( 'button' ).trigger( 'click' );
	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active' ).addClass( 'elex-multi-vendor-for-woocommerce-on' );
} );

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

	var formData;
	var link = elexMvOnboardUiParams.ajax_url;
	link    += '?action=elex-multi-vendor-for-woocommerce-vendor-installation-setup';

	localStorage.setItem( 'setup-page', jQuery( this ).data( 'id' ) );

	formData = new FormData( document.getElementById( 'elex-multi-vendor-for-woocommerce-vendor-installation-setup' ) );

	if ( jQuery( this ).data( 'id' ) == 2 ) {
		formData.append( 'is_setup_finished', 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-onboard-list.nav > li > .nav-link.active' ).parent().next( 'li' ).find( 'button' ).trigger( 'click' );
			jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active' ).addClass( 'elex-multi-vendor-for-woocommerce-on' );
			var additem = document.getElementById( 'elex_vendor_onboard_toast' );
			var toast   = new bootstrap.Toast( additem );
			toast.show();
		},
		error: function( result ) {
			console.log( result );
		},
	} );

} );

jQuery( '.elex-multi-vendor-for-woocommerce-onboard-back-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active' ).parent().find( 'button' ).removeClass( 'elex-multi-vendor-for-woocommerce-on' );
	const btn = jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list.nav > li > .nav-link.active ' ).parent().prev( 'li' ).find( 'button' ).trigger( 'click' );
	// elex-vendor-onboard-number (preserved from original)

} );

jQuery( document ).ready( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-onboard-list button' ).click( function() {
		jQuery( this ).tab( 'show' );
	} );
} );

validation_for_forms_func( '#elex-vendor-onboard-store-url', /^([ \u00c0-\u01ffa-zA-Z0-9'\-])+$/, '#elex-vendor-onboard-store-url-error', 'Enter valid Name ' );
// validation_for_forms_func('#elex-vendor-onboard-store-full-addr', /[A-Za-z0-9'\.\-\s\,]/, '#elex-vendor-onboard-store-full-addr-error', 'Enter proper address')
validation_for_forms_func( '#elex-vendor-onboard-city', /^([ \u00c0-\u01ffa-zA-Z'\-])+$/, '#elex-vendor-onboard-city-error', 'Enter valid city' );
validation_for_forms_func( '#elex-vendor-onboard-postal', /(^[a-z0-9][a-z0-9\- ]){0,10}[a-z0-9]$/, '#elex-vendor-onboard-postal-error', 'Enter valid postal num in integers' );
validation_for_forms_func( '#elex-vendor-onboard-state', /^([ \u00c0-\u01ffa-zA-Z'\-])+$/, '#elex-vendor-onboard-state-error', 'Enter valid state in alphabets' );
validation_for_forms_func( '#elex-vendor-onboard-country', /^([ \u00c0-\u01ffa-zA-Z'\-])+$/, '#elex-vendor-onboard-country-error', 'Enter valid country' );
validation_for_forms_func( '#elex-vendor-onboard-paypal-email', /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, '#elex-vendor-onboard-paypal-email-error', 'Enter valid email address' );
validation_for_forms_func( '#elex-vendor-onboard-number', /^[0-9]*$/, '#elex-vendor-onboard-number-error', 'Enter valid number' );
validation_for_forms_func( '#elex-vendor-onboard-email', /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, '#elex-vendor-onboard-paypal-email-error', 'Enter valid email address' );

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;

		jQuery( id_for_input_element ).next( '.text-danger' ).remove();

		if ( ! regex.test( this.value ) ) {
			jQuery( id_for_input_element ).after( `<div class="text-danger" id="${id_for_input_element}-error" >${error_string}</div>` );
		} else {
			jQuery( id_for_input_element ).next( '.text-danger' ).remove();
		}
	} );
}
