
selectedAttributes = [];

var attribute_number = 0;
jQuery( document ).ready( function() {

	jQuery( '.expand_all_attribute' ).click( function() {
		jQuery( '.elex_mv_mini_max_sec' ).each( function(
			index,
			element
		) {
			var select = jQuery( element );
			select.slideDown( 'slow' );
		} );
	} );
	jQuery( '.close_all_attribute' ).click( function() {
		jQuery( '.elex_mv_mini_max_sec' ).each( function(
			index,
			element
		) {
			var select = jQuery( element );
			select.slideUp( 'slow' );
		} );
	} );

	jQuery( document ).on( 'click', '.elex_mv_mini_max_btn', function() {

		var section = jQuery( this ).closest( '.elex_mv_mini_max_sec_parent_template' ).find( '.elex_mv_mini_max_sec' );

		if ( section.is( ':visible' ) ) {
			section.slideUp( 'slow' );
		} else {
			section.slideDown( 'slow' );
		}

	} );

	jQuery( '.elex-add-new-attribute-button' ).click( function() {

		attribute_number = jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index' ) ? jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index' ) : 0;

		jQuery( '.elex_mv_add_attribute_parent' ).prepend( jQuery( '#template-for-attributes' ).html() );
		jQuery( '.elex_mv_mini_max_sec_parent_template' ).removeClass( 'd-none' );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-attribute-values-textarea' ).prop( 'name', 'attribute_values[' + attribute_number + ']' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-name-of-attribute-input' ).prop( 'name', 'attribute_names[' + attribute_number + ']' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-visibile-product' ).prop( 'name', 'attribute_visibility[' + attribute_number + ']' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).prop( 'name', 'attribute_variation[' + attribute_number + ']' );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-visibile-product' ).attr( 'checked', 'checked' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).attr( 'checked', 'checked' );

		if ( jQuery( '#elex_multivendor_new_product_type' ).val() == 'simple' ) {
			parent = jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).parent();
			parent.addClass( 'd-none' );
		}

		attribute_number++;
		jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index', attribute_number );
		jQuery( '.elex_mv_attribute_expand_close_div' ).removeClass( 'd-none' );
	} );

	jQuery( document.body ).on( 'click', '.elex-multi-vendor-for-woocommerce-remove-button-attribute-template', function() {
		jQuery( this ).parent().parent().parent().remove();
		selectedAttributes.pop( jQuery( this ).data( 'attribute_name' ) );
	} );

	jQuery( '.elex_mv_select_attributes_woocommerce' ).select2( {
		placeholder: {
			id:   '-1',
			text: 'Select an option',
		},
		closeOnSelect: false,
		width:         'resolve',
	} );

	jQuery( '.elex_mv_select_attributes_woocommerce' ).on( 'select2:select', function( e ) {

		if ( -1 !== jQuery.inArray( e.params.data.text, selectedAttributes ) ) {
			return;
		}

		selectedAttributes.push( e.params.data.text );

		const attributeId = e && e.params && e.params.data && e.params.data.id;

		attribute_number = jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index' ) ? jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index' ) : 0;

		value = jQuery( '.elex_mv_add_attribute_parent' ).prepend( jQuery( '#template-for-attributes' ).html() );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-attribute-title' ).html( e.params.data.text );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-remove-button-attribute-template' ).data( 'attribute_name', e.params.data.text );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-name-of-attribute' ).html( 'Name:' + e.params.data.text );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-attribute-values-textarea' ).remove();

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-attribute-values-label' ).after().html( '<select id="elex-attribute-' + attribute_number + '" name="attribute_values[' + attribute_number + '][]" class="elex-attribute-values-select" style="width:200px;" class=\'attribute-select\' ></select>' );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex_mv_select_all_attribute_values' ).removeClass( 'd-none' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex_mv_select_all_attribute_values' ).attr( 'id', 'elex_mv_select_all_attribute_values_id-' + attribute_number );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex_mv_select_create_attribute_values' ).removeClass( 'd-none' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex_mv_select_create_attribute_values' ).attr( 'id', 'elex_mv_select_create_attribute_values_id-' + attribute_number );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().data( 'attribute-id', e.params.data.id );

		jQuery( document ).on( 'click', '#elex_mv_select_create_attribute_values_id-' + attribute_number, function() {

			var attribute_number_edit = this.id.split( '-' ).pop();

			var attribute = 'pa_' + ( e.params.data.text ).toLowerCase().replace( ' ', '-' );

			var new_attribute_name = window.prompt(
				'Enter a name for the new attribute term:'
			);

			if ( new_attribute_name ) {
				var data = {
					action:   'elex_mv_add_new_attributes',
					taxonomy: attribute,
					term:     new_attribute_name,
					nonce:    elex_mv_coupon_ajaxurl_object.nonce,
				};

				jQuery.post(
					elex_mv_coupon_ajaxurl_object.ajax_url,
					data,
					function( response ) {
						if ( response.error ) {
							window.alert( response.error );
						} else if ( response.slug ) {
							jQuery( '#elex-attribute-' + attribute_number_edit ).append(
								'<option value="' +
								response.term_id +
								'" selected="selected">' +
								response.name +
								'</option>'
							);
							jQuery( '#elex-attribute-' + attribute_number_edit ).trigger( 'change' );
						}
					}
				);
			}
		} );

		jQuery( document ).on( 'click', '#elex_mv_select_all_attribute_values_id-' + attribute_number, function() {

			var attribute_number_edit = this.id.split( '-' ).pop();

			var attribute = 'pa_' + ( e.params.data.text ).toLowerCase().replace( ' ', '-' );

			var data = {
				action:   'elex_mv_json_search_taxonomy_terms',
				taxonomy: attribute,
				nonce:    elex_mv_coupon_ajaxurl_object.nonce,
			};

			jQuery.get(
				elex_mv_coupon_ajaxurl_object.ajax_url,
				data,
				function( response ) {
					if ( response.errors ) {
						// Error.
						// window.alert( response.errors );
					} else if ( response && response.length > 0 ) {
						response.forEach( function( term ) {
							const currentItem = jQuery( '#elex-attribute-' + attribute_number_edit ).find(
								'option[value="' +
								term.term_id +
								'"]'
							);
							if ( currentItem && currentItem.length > 0 ) {
								currentItem.prop( 'selected', 'selected' );
							} else {
								jQuery( '#elex-attribute-' + attribute_number_edit )
									.append(
										'<option value="' +
										term.term_id +
										'" selected="selected">' +
										term.name +
										'</option>'
									);
							}
						} );
						jQuery( '#elex-attribute-' + attribute_number_edit ).trigger( 'change' );
					}
				}
			);
		} );

		jQuery( '#elex-attribute-' + attribute_number ).select2( {

			allowClear:    true,
			placeholder:   '',
			dataType:      'json',
			multiple:      true,
			closeOnSelect: true,

			ajax: {
				url:      elex_mv_coupon_ajaxurl_object.ajax_url,
				dataType: 'json',
				delay:    250,
				data:     function( params ) {
					return {
						taxonomy: 'pa_' + ( e.params.data.text ).toLowerCase().replace( ' ', '-' ),
						limit:    '50',
						orderby:  'menu_order',
						term:     params.term,
						action:   'elex_mv_json_search_taxonomy_terms',
						nonce:    elex_mv_coupon_ajaxurl_object.nonce,
					};
				},
				processResults: function( data ) {
					var terms = [];
					if ( data ) {
						jQuery.each( data, function( id, term ) {
							terms.push( {
								id:   term.term_id,
								text: term.name,
							} );
						} );
					}
					return {
						results: terms,
					};
				},
				cache: true,
			},
		} );

		var data = {
			action:   'elex_mv_json_search_taxonomy_terms',
			taxonomy: 'pa_' + ( e.params.data.text ).toLowerCase().replace( ' ', '-' ),
			nonce:    elex_mv_coupon_ajaxurl_object.nonce,
		};

		jQuery.get(
			elex_mv_coupon_ajaxurl_object.ajax_url,
			data,
			function( response ) {
				if ( response.errors ) {
					// Error.
					// window.alert( response.errors );
				} else if ( response && response.length > 0 ) {
					response.forEach( function( term ) {
						jQuery( '#elex-attribute-' + attribute_number )
							.append(
								'<option value="' +
								term.term_id +
								'">' +
								term.name +
								'</option>'
							);
					} );
					jQuery( '#elex-attribute-' + attribute_number ).trigger( 'change' );
				}
			}
		);

		vlaue2 = jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-name-of-attribute-input' ).addClass( 'd-none' );
		vlaue2 = jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-name-of-attribute-input' ).val( 'pa_' + ( e.params.data.text ).toLowerCase().replace( ' ', '-' ) );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-name-of-attribute-input' ).prop( 'name', 'attribute_names[' + attribute_number + ']' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-visibile-product' ).prop( 'name', 'attribute_visibility[' + attribute_number + ']' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).prop( 'name', 'attribute_variation[' + attribute_number + ']' );

		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-visibile-product' ).attr( 'checked', 'checked' );
		jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).attr( 'checked', 'checked' );

		if ( jQuery( '#elex_multivendor_new_product_type' ).val() == 'simple' ) {
			parent = jQuery( '.elex_mv_add_attribute_parent' ).children().first().find( '.elex-multi-vendor-for-woocommerce-use-for-variations' ).parent();
			parent.addClass( 'd-none' );
		}
		jQuery( '.elex_mv_mini_max_sec_parent_template' ).removeClass( 'd-none' );

		jQuery( this ).val( null );
		jQuery( this ).trigger( 'change' );
		attribute_number++;
		jQuery( '.elex_mv_add_attribute_parent' ).attr( 'attribute_curr_index', attribute_number );
		jQuery( '.elex_mv_attribute_expand_close_div' ).removeClass( 'd-none' );

		return false;
	} );

	jQuery( document.body ).on( 'click', '.elex-save-attributes', function() {

		dis = this;
		var original_data = jQuery( '.elex_mv_add_attribute_parent' ).find(
			'input, select, textarea'
		);

		var data = {
			post_id:      jQuery( '.elex-save-attributes' ).attr( 'elex_mv_id_prod' ),
			product_type: jQuery( '#elex_multivendor_new_product_type' ).val(),
			data:         original_data.serialize(),
			action:       'elex_mv_save_attributes',
			nonce:        elex_mv_coupon_ajaxurl_object.nonce,
		};

		jQuery.post(
			elex_mv_coupon_ajaxurl_object.ajax_url,
			data,
			function( response ) {
				if ( response.error ) {
					window.alert( response.error );
				} else if ( response.success == false ) {

					window.alert( response.data.error );

				} else {

					var additem = document.getElementById( 'wsdesk_additem_toast' );

					jQuery( '#toast_message_mv' ).html( 'Attributes Saved for Product' );

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

					if ( 'next' == jQuery( dis ).data( 'button' ) ) {
						next_page_iterate( jQuery( '.nav-tabs > .nav-item > .active' )
							.parent()
							.next( 'li' ) );
					}
				}
			}
		);
	} );

	function next_page_iterate( elem ) {

		if ( elem.hasClass( 'd-none' ) ) {
			next_page_iterate( elem.next( 'li' ) );
		} else {
			elem.find( 'button' )
				.trigger( 'click' );
		}
	}

} );
