jQuery( document ).ready( function() {

	jQuery( '.elex-admin-add-product-apply-vendor-name' ).on( 'keyup', function() {
		jQuery( '#elex_vendor_name_list' ).empty();
		jQuery.ajax( {
			url:     elex_mv_coupon_ajaxurl_object.ajax_url,
			type:    'GET',
			enctype: 'text/plain',
			data: {
				vendor_search: jQuery( this ).val(),
				status:        'publish',
				action:        'elex_mv_api_endpoint_for_vendors',
				nonce:         elex_mv_coupon_ajaxurl_object.nonce,
			},
			success: function( response ) {
				jQuery.each( response.data, function( i, item ) {
					jQuery( '#elex_vendor_name_list' ).append(
						jQuery( '<option>', { value: item.post_title + ' ID: ' + item.ID } )
					);
				} );
			},
		} );
	} );

	jQuery( '.elex-admin-add-product-apply-vendor-name-btn' ).on( 'click', function() {
		var vendorName = jQuery( '.elex-admin-add-product-apply-vendor-name' ).val();
		vendorName     = vendorName.split( ':' )[1];

		jQuery.ajax( {
			url:     elex_mv_coupon_ajaxurl_object.ajax_url,
			type:    'POST',
			enctype: 'text/plain',
			data: {
				vendor_name: vendorName.trim(),
				product_id:  elexMvVendorNameDisplayParams.product_id,
				action:      'elex_mv_assign_vendor_name_product',
				nonce:       elex_mv_coupon_ajaxurl_object.nonce,
			},
		} );
	} );

} );
