
jQuery( '#elex_multivendor_edit_new_product_type' ).change( function() {
	if ( this.value == 'simple' ) {
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-prod-variations-tab' ).closest( 'li' ).addClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-regular-price-sale-price-div' ).removeClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-prod-variations' ).addClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-virtual-product-switch-div' ).removeClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-dwn-product-switch-div' ).removeClass( 'd-none' );

	} else {
		jQuery( '.elex-multi-vendor-for-woocommerce-regular-price-sale-price-div' ).addClass( 'd-none' );

		jQuery( '#elex-multi-vendor-for-woocommerce-edit-prod-variations-tab' ).closest( 'li' ).removeClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-prod-variations' ).removeClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-virtual-product-switch-div' ).addClass( 'd-none' );
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-dwn-product-switch-div' ).addClass( 'd-none' );
	}
} );

jQuery( '#elex_multivendor_edit_manage_stock_value' ).change( function() {

	if ( jQuery( this ).is( ':checked' ) ) {
		jQuery( '.elex-multi-vendor-for-woocommerce-low-stock-value-div' ).removeClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-low-stock-threshold-div' ).removeClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-backorders-div' ).removeClass( 'd-none' );

		jQuery( '.elex-multi-vendor-for-woocommerce-stock-status-div' ).addClass( 'd-none' );

	} else {
		jQuery( '.elex-multi-vendor-for-woocommerce-low-stock-value-div' ).addClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-low-stock-threshold-div' ).addClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-backorders-div' ).addClass( 'd-none' );

		jQuery( '.elex-multi-vendor-for-woocommerce-stock-status-div' ).removeClass( 'd-none' );
	}

} );

jQuery( '#elex-multi-vendor-for-woocommerce-edit-dwn-product-switch' ).change( function() {

	if ( jQuery( this ).is( ':checked' ) ) {
		jQuery( '.elex-multi-vendor-for-woocommerce-edit-download-file-div' ).removeClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-dwm-limit-expiry-div' ).removeClass( 'd-none' );
	} else {
		jQuery( '.elex-multi-vendor-for-woocommerce-edit-download-file-div' ).addClass( 'd-none' );
		jQuery( '.elex-multi-vendor-for-woocommerce-dwm-limit-expiry-div' ).addClass( 'd-none' );
	}

} );

jQuery( '#elex-multi-vendor-for-woocommerce-edit-virtual-product-switch' ).change( function() {

	if ( jQuery( this ).is( ':checked' ) ) {

		jQuery( '#elex-multi-vendor-for-woocommerce-edit-ship-tab' ).closest( 'li' ).addClass( 'd-none' );
		// jQuery('#elex-multi-vendor-for-woocommerce-add-ac-shipping-tab').attr('disabled','disabled');

		jQuery( '#elex-multi-vendor-for-woocommerce-edit-ship' ).addClass( 'd-none' );
	} else {
		jQuery( '#elex-multi-vendor-for-woocommerce-edit-ship-tab' ).closest( 'li' ).removeClass( 'd-none' );
		// jQuery('#elex-multi-vendor-for-woocommerce-add-ac-shipping-tab').attr('disabled','disabled');

		jQuery( '#elex-multi-vendor-for-woocommerce-edit-ship' ).removeClass( 'd-none' );
	}

} );

dwn_index = 0;
jQuery( '.elex-multi-vendor-for-woocommerce-add-download-file-row' ).click( function() {
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).prepend( jQuery( '#template-for-product-file-download' ).html() );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex_mv_name_file_download' ).attr( 'id', 'elex_mv_name_file_download_id-' + dwn_index );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex_mv_hash_file_download' ).attr( 'id', 'elex_mv_hash_file_download_id-' + dwn_index );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex_mv_url_file_download' ).attr( 'id', 'elex_mv_url_file_download_id-' + dwn_index );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn' ).attr( 'id', 'elex-multi-vendor-for-woocommerce-edit-upload-download-file-btn-id-' + dwn_index );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex-multi-vendor-for-woocommerce-file-download' ).attr( 'id', 'elex-multi-vendor-for-woocommerce-file-download-id-' + dwn_index );
	jQuery( '#elex_mv_edit_table_download_file' ).find( 'tbody' ).children( 'tr:first' ).find( '.elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product' ).attr( 'id', 'elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-' + dwn_index );

	dwn_index++;
} );

var downloadable_file_frame;
var file_path_field;

jQuery( document.body ).on( 'click', '[id^="elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-"]', function( event ) {

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

	// jQuery("#elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-" + variat_index ).parent().parent().remove();
	jQuery( '#elex-multi-vendor-for-woocommerce-remove-dowload-tr-simple-product-id-' + variat_index ).parent().closest( 'tr' ).remove();

} );

jQuery( document.body ).on( 'change', '.elex-multi-vendor-for-woocommerce-file-download', function( event ) {

	var reader;

	var variat_index = this.id.split( '-' ).pop();
	var formData = new FormData();

	if ( this.files && this.files[0] ) {
		reader = new FileReader();
		reader.onload = function( e ) {

			// jQuery("#elexPreviewImageUploadVariation_" + variat_index).attr("src", e.target.result);

			jQuery( '#elex-multi-vendor-for-woocommerce-remove-image-variation-' + variat_index ).removeClass( 'd-none' );
		};

		reader.readAsDataURL( this.files[0] );

		formData.append( 'file', this.files[0] );
		formData.append( 'nonce', elex_mv_coupon_ajaxurl_object.nonce );

		jQuery.ajax( {
			url:         elex_mv_coupon_ajaxurl_object.ajax_url + '?action=elex_mv_save_image_wordpress',
			method:      'post',
			encType:     'multipart/form-data',
			processData: false,
			contentType: false,
			data:        formData,
			success:     function( response ) {

				jQuery( '#elex_mv_url_file_download_id-' + variat_index ).val( response.data );
			},
		} );
	}

} );
