jQuery( document ).ready( function() {

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

	jQuery( '#elex-multi-vendor-for-woocommerce-store-setting-filetag' ).on( 'change', function() {
		const reader = new FileReader();

		reader.onload = function( e ) {
			elexImageVendorUpload.setAttribute( 'src', e.target.result );
			jQuery( '.elex-multi-vendor-for-woocommerce-vend-img-name-div-admin' ).removeClass( 'd-none' );
			var fileInput = document.getElementById( 'elex-multi-vendor-for-woocommerce-store-setting-filetag' );
			var fileName = fileInput.files[0].name;
			jQuery( '.elex-multi-vendor-for-woocommerce-vend-img-name-admin' ).html( fileName );
		};
		reader.readAsDataURL( this.files[0] );

		jQuery( '#elex_mv_store_edit_deleted_image' ).attr( 'value', '' );
	} );

	jQuery( '.elex-multi-vendor-for-woocommerce-store-setting-filetag-delete-btn' ).click( function( e ) {
		jQuery( '#elex_mv_store_edit_deleted_image' ).attr( 'value', 'true' );
		jQuery( '.elex-multi-vendor-for-woocommerce-vend-img-name-div-admin' ).addClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-store-setting-filetag' ).attr( 'value', '' );
		elexImageVendorUpload.setAttribute( 'src', elex_mv_coupon_ajaxurl_object.plugin_path + 'assets/images/default-store-icon.svg' );
	} );

	jQuery( '.store-setting-save-changes' ).click( function() {
		var link = elex_mv_admin_ajaxurl_object.ajax_url;
		link += '?action=elex_mv_store_settings_details';
		var formData = new FormData( document.getElementById( 'vendor-settings-details-form' ) );

		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 ) {
					var saved = document.getElementById( 'vendor_store_setting_saved_success_toast' );
					jQuery( '#vendor_store_setting_saved_success_toast #toast_message_mv' ).html( 'Changes Saved Successfully' );
					jQuery( saved ).find( '.elex-toast-icon-success' ).removeClass( 'd-none' );
					jQuery( saved ).find( '.elex-toast-icon-error' ).addClass( 'd-none' );
					var toast = new bootstrap.Toast( saved );
					toast.show();
					setTimeout( function() {
						window.location.reload();
					}, 1000 );
				} else {
					var saved = document.getElementById( 'vendor_store_setting_saved_success_toast' );
					var errorMsg = window.elexMvFormatResponseError( result );
					jQuery( '#vendor_store_setting_saved_success_toast #toast_message_mv' ).html( errorMsg );
					jQuery( saved ).find( '.elex-toast-icon-success' ).addClass( 'd-none' );
					jQuery( saved ).find( '.elex-toast-icon-error' ).removeClass( 'd-none' );
					var toast = new bootstrap.Toast( saved );
					toast.show();
				}
			},
			error: function( xhr ) {
				var saved = document.getElementById( 'vendor_store_setting_saved_success_toast' );
				var errorMsg = window.elexMvFormatResponseError( xhr );
				jQuery( '#vendor_store_setting_saved_success_toast #toast_message_mv' ).html( errorMsg );
				jQuery( saved ).find( '.elex-toast-icon-success' ).addClass( 'd-none' );
				jQuery( saved ).find( '.elex-toast-icon-error' ).removeClass( 'd-none' );
				var toast = new bootstrap.Toast( saved );
				toast.show();
			}
		} );
	} );

} );
