jQuery( '.services_show_in_table_button' ).click( function() {
	var carrier = jQuery( this ).html();
	jQuery( "[class^='li-for-active-carrier-']" ).removeClass( 'active' );
	jQuery( '.li-for-active-carrier-' + carrier ).addClass( 'active' );
	jQuery( "[id^='service-table-values-']" ).hide();
	jQuery( '#service-table-values-' + carrier ).show();
} );

jQuery( '.enable_shipping_method_for_vendor' ).click( function() {
	var carrier_slug = jQuery( this ).data( 'carrier-slug' );
	var store_id     = jQuery( this ).data( 'store-id' );
	var action       = 'elex_mv_enabling_shipping_method_plugin';
	var enable       = this.checked;

	var data = {
		store_id:    store_id,
		action:      action,
		enable:      enable,
		carrier_slug: carrier_slug,
		nonce:       elex_mv_coupon_ajaxurl_object.nonce,
	};

	jQuery.ajax( {
		url: elex_mv_coupon_ajaxurl_object.ajax_url,
		method: 'POST',
		data: data,
		success: function() {
			jQuery( '.toast_message_mv' ).html( 'Updated Succesfully' );
			var update = document.getElementById( 'wsdesk_update_toast' );
			var toast = new bootstrap.Toast( update );
			toast.show();
			setTimeout( function() {
				window.location.reload();
			}, 2000 );
		},
	} );
} );

jQuery( '.enable_woocommerce_shipping_method_for_vendor' ).click( function() {
	var shipping_method    = jQuery( this ).data( 'shipping-method' );
	var shipping_method_id = jQuery( this ).data( 'shipping-method-id' );
	var store_id           = jQuery( this ).data( 'store-id' );
	var action             = 'elex_mv_enabling_woocommerce_shipping_method_plugin';
	var enable             = this.checked;

	var data = {
		action:            action,
		enable:            enable,
		shipping_method_id: shipping_method_id,
		shipping_method:   shipping_method,
		store_id:          store_id,
		nonce:             elex_mv_coupon_ajaxurl_object.nonce,
	};

	jQuery.ajax( {
		url: elex_mv_coupon_ajaxurl_object.ajax_url,
		method: 'POST',
		data: data,
		success: function() {
			jQuery( '.toast_message_mv' ).html( 'Updated Succesfully' );
			var update = document.getElementById( 'wsdesk_update_toast' );
			var toast = new bootstrap.Toast( update );
			toast.show();
			setTimeout( function() {
				window.location.reload();
			}, 2000 );
		},
	} );
} );

jQuery( '.override-credential-check' ).on( 'change', function() {
	if ( this.checked ) {
		jQuery( this ).parent().parent().parent().parent().parent().find( '.override-credentials-form' ).show( 300 );
		jQuery( this ).parent().parent().parent().parent().parent().find( '.override-credentials-valid' ).prop( 'disabled', false );
	} else {
		jQuery( this ).parent().parent().parent().parent().parent().find( '.override-credentials-form' ).hide( 300 );
		jQuery( this ).parent().parent().parent().parent().parent().find( '.override-credentials-valid' ).prop( 'disabled', true );
	}

	var store_id        = jQuery( this ).data( 'store-id' );
	var action          = 'elex_mv_override_vendor_credential';
	var enable          = this.checked;
	var shipping_method = jQuery( this ).data( 'shipping-method' );

	var data = {
		store_id:        store_id,
		action:          action,
		enable:          enable,
		shipping_method: shipping_method,
		nonce:           elex_mv_coupon_ajaxurl_object.nonce,
	};

	jQuery.ajax( {
		url: elex_mv_coupon_ajaxurl_object.ajax_url,
		method: 'POST',
		data: data,
		success: function() {
			var update = document.getElementById( 'wsdesk_update_toast' );
			jQuery( '.toast_message_mv' ).html( 'Updated Succesfully' );
			var toast = new bootstrap.Toast( update );
			toast.show();
		},
	} );
} );

jQuery( '.override-credentials-valid' ).click( function() {
	jQuery( '.override-credendtial-indicator' ).addClass( 'active' );
} );

jQuery( '.override-credentials-btn' ).click( function() {
	jQuery( this ).parent().parent().parent().parent().find( '.override-credentials-popup' ).addClass( 'active' );
	jQuery( this ).parent().parent().parent().parent().parent().find( '.override-credentials-popup' ).attr( 'data-shipping-method', jQuery( this ).data( 'shipping-method' ) );
} );

jQuery( document ).click( function( e ) {
	if ( jQuery( e.target ).is( '.override-credentials-btn ,.elex-multi-vendor-for-woocommerce-popup-content *' ) ) {
		return;
	}
	jQuery( '.override-credentials-popup' ).removeClass( 'active' );
} );

jQuery( '.elex_ac_carrier_setting_btn' ).click( function() {
	jQuery( this ).parent().parent().find( '.elex_ac_carrier_setting' ).addClass( 'active' );
} );
jQuery( '.elex_ac_carrier_setting_close_btn' ).click( function() {
	jQuery( '.elex_ac_carrier_setting' ).removeClass( 'active' );
} );
jQuery( document ).click( function( e ) {
	if ( jQuery( e.target ).is( '.elex_ac_carrier_setting_btn ,.elex-multi-vendor-for-woocommerce-popup-content *' ) ) {
		return;
	}
	jQuery( '.elex_ac_carrier_setting' ).removeClass( 'active' );
} );

jQuery( '.elex-multi-vendor-for-woocommerce-add-your-ac-open-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-add-your-account' ).addClass( 'active' );
} );
jQuery( '.elex-multi-vendor-for-woocommerce-add-your-ac-close-btn' ).click( function() {
	jQuery( '#elex-multi-vendor-for-woocommerce-add-your-account' ).removeClass( 'active' );
} );
jQuery( document ).click( function( e ) {
	if ( jQuery( e.target ).is( '.elex-multi-vendor-for-woocommerce-add-your-ac-open-btn ,.elex-multi-vendor-for-woocommerce-popup-content *' ) ) {
		return;
	}
	jQuery( '#elex-multi-vendor-for-woocommerce-add-your-account' ).removeClass( 'active' );
} );

jQuery( '.elex-shipping-table-open-btn' ).click( function() {
	jQuery( this ).addClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-table' ).removeClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-table-close-sec' ).removeClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( "[id^='service-table-values-']" ).hide();

	var all_carriers_list    = jQuery( this ).parent().parent().parent().find( "[id^='service-table-values-']" );
	var all_carriers_li_list = jQuery( this ).parent().parent().parent().find( "[class^='li-for-active-carrier-']" );
	jQuery( all_carriers_li_list[0] ).addClass( 'active' );
	jQuery( all_carriers_list[0] ).show();
} );

jQuery( '.elex-shipping-table-close-btn' ).click( function() {
	jQuery( '.elex-shipping-table-open-btn' ).removeClass( 'd-none' );
	jQuery( '.elex-shipping-table' ).addClass( 'd-none' );
	jQuery( '.elex-shipping-table-close-sec' ).addClass( 'd-none' );
} );

jQuery( '.elex-shipping-detail-open-btn' ).click( function() {
	jQuery( this ).parent().parent().parent().find( '.elex_show_shipping_details' ).removeClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-detail-open' ).addClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-detail-close' ).removeClass( 'd-none' );
} );

jQuery( '.elex-shipping-detail-close-btn' ).click( function() {
	jQuery( this ).parent().parent().parent().find( '.elex_show_shipping_details' ).addClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-detail-open' ).removeClass( 'd-none' );
	jQuery( this ).parent().parent().parent().find( '.elex-shipping-detail-close' ).addClass( 'd-none' );
} );

jQuery( '.override-credentials-close-btn' ).click( function() {
	jQuery( '.override-credentials-popup' ).removeClass( 'active' );
} );
jQuery( document ).click( function( e ) {
	if ( jQuery( e.target ).is( '.override-credentials-btn ,.elex-multi-vendor-for-woocommerce-popup-content *' ) ) {
		return;
	}
	jQuery( '.override-credentials-popup' ).removeClass( 'active' );
} );

jQuery( '.save-changes-related-to-service' ).click( function() {
	var store_id          = jQuery( this ).data( 'store-id' );
	var method_slug       = jQuery( this ).data( 'method-slug' );
	var service_container = jQuery( this ).parent().find( '.elex-multi-vendor-for-woocommerce-service-dhl-table' );
	var carrier_data      = {};

	service_container.find( '.elex_row_shipment_services' ).each( function() {
		var row          = jQuery( this );
		var carrier_name    = row.data( 'carrier-name' );
		var carrier_enabled = row.data( 'carrier-enabled' );

		var row_data = {
			name: row.find( '.elex_service_name' ).val(),
			id:   row.find( '.elex_service_id' ).val(),
			price_adjustment: {
				value: row.find( '.elex_service_value' ).val(),
				type:  row.find( '.elex_service_type' ).val(),
			},
		};

		if ( row.find( '.elex_service_enabled' ).prop( 'checked' ) ) {
			row_data['enabled'] = true;
		} else {
			row_data['enabled'] = false;
		}

		if ( carrier_data[carrier_name] === undefined ) {
			carrier_data[carrier_name] = {};
		}
		if ( carrier_data[carrier_name]['name'] === undefined && carrier_data[carrier_name]['enabled'] === undefined && carrier_data[carrier_name]['services'] === undefined ) {
			carrier_data[carrier_name]['name']     = carrier_name;
			carrier_data[carrier_name]['enabled']  = carrier_enabled;
			carrier_data[carrier_name]['services'] = [];
		}
		carrier_data[carrier_name]['services'].push( row_data );
	} );

	jQuery.ajax( {
		url: elex_mv_coupon_ajaxurl_object.ajax_url,
		type: 'POST',
		data: {
			action:   'elex_mv_' + method_slug + '_services_changed_to_save',
			services: carrier_data,
			store_id: store_id,
		},
		enctype: 'text/plain',
		success: function() {
			// location.reload();
		},
		error: function( status ) {
			var error_obj = status.responseJSON.data;
			for ( var key in error_obj ) {
				jQuery( '.elex_service_id' ).each( function() {
					if ( jQuery( this ).val() === key ) {
						for ( var keey in error_obj[key] ) {
							if ( keey === 'name' ) {
								jQuery( this ).parent().parent().find( '.elex_service_name' ).after( '<div style="color:red;" >' + error_obj[key]['name'] + '</div>' );
							}
							if ( keey === 'price_adjustment' ) {
								jQuery( this ).parent().parent().find( '.elex_service_value' ).after( '<div style="color:red;" >' + error_obj[key]['price_adjustment'] + '</div>' );
							}
						}
					}
				} );
			}
		},
	} );
} );

jQuery( '.elex-multi-vendor-for-woocommerce-override-cred-function' ).click( function() {
	var frm = jQuery( this ).parent().find( '.mv-override-credentials-form' ).serialize() + '&action=elex_mv_shipping_save_vendor_credentials' + '&nonce=' + elex_mv_coupon_ajaxurl_object.nonce;

	jQuery.ajax( {
		url: elex_mv_coupon_ajaxurl_object.ajax_url,
		method: 'POST',
		data: frm,
		success: function() {
			var update = document.getElementById( 'wsdesk_update_toast' );
			jQuery( '.toast_message_mv' ).html( 'Credentials Updated Succesfully' );
			var toast = new bootstrap.Toast( update );
			toast.show();
			jQuery( '.override-credentials-popup' ).removeClass( 'active' );
		},
		error: function( xhr ) {
			var respdata   = xhr.responseJSON.data;
			var update     = document.getElementById( 'wsdesk_update_toast' );
			var danger_svg = '<i class="fa-solid fa-circle-exclamation" style="color: #fd0d0d;font-size:24px"></i>';
			jQuery( '.wsdesk_update_toast_svg' ).html( danger_svg );
			jQuery( '.toast_message_mv' ).html( respdata.validation );
			var toast = new bootstrap.Toast( update );
			toast.show();
		},
	} );
} );
