jQuery( function( $ ) {
    $( document ).on( 'change', '#yith_wcpo_enable_pre_order_auto_outofstock_notification', function() {
        var $select_fields = $( '.form-table tr[data-dep-target="ywpo_specific_outofstock_products"], .form-table tr[data-dep-target="ywpo_specific_outofstock_categories"]' );
        if ( $select_fields.is( ':visible' ) && ! $( this ).is( ':checked' ) ) {
            $select_fields.hide();
        } else if ( $( this ).is( ':checked' ) && $( '#ywpo_automatic_pre_order_mode-specific' ).is( ':checked' ) ) {
            $select_fields.show();
        }
    } );
    $( '#yith_wcpo_enable_pre_order_auto_outofstock_notification' ).trigger( 'change' );

    var $cart_options = $( '#yith_wcpo_mixing, #yith_wcpo_one_pre_order_in_cart' );
    $( document ).on( 'change', $cart_options, function() {
        var $cart_options_mode = $( '#ywpo_cart_options_mode__label' ).closest( '.yith-plugin-fw__panel__option--radio' );
        if ( ! $( '#yith_wcpo_mixing' ).is( ':checked' ) && ! $( '#yith_wcpo_one_pre_order_in_cart' ).is( ':checked' ) ) {
            $cart_options_mode.hide();
        } else {
            $cart_options_mode.slideDown();
        }
    } );
    $( $cart_options ).trigger( 'change' );

    $pre_order_users_radio_buttons = $( 'input[name="ywpo_pre_order_users"]' );
    $( document ).on( 'change', $pre_order_users_radio_buttons, function() {
      if ( $( '#ywpo_pre_order_users-roles' ).is( ':checked' ) ) {
        $( '#ywpo_allowed_roles' ).attr( 'required', '' );
      } else {
        $( '#ywpo_allowed_roles' ).removeAttr( 'required' );
      }
    } );
    $pre_order_users_radio_buttons.trigger( 'change' );

  $( document ).on( 'change', 'input#ywpo_fee', function() {
    if ( $( this ).is( ':checked' ) ) {
      $( '#ywpo_fee_amount' ).attr( 'required', '' );
    } else {
      $( '#ywpo_fee_amount' ).removeAttr( 'required' );
    }
  } );
  $( 'input#ywpo_fee' ).trigger( 'change' );

  if ( ! ywpo_options.integrated_gateways ) {
    var $radio_button = $( '#ywpo_charge_type-upon_release' );
    $radio_button.attr( 'disabled', 'disabled' );
    $radio_button.addClass( 'ywpo-disabled-radio' );
  }

  $( document ).on( 'change', '#ywpo_custom_place_order_button_enabled', function() {
    var $colors = $( '.form-table tr[data-dep-target="ywpo_place_order_colors"]' );
    if ( $colors.is( ':visible' ) && ! $( this ).is( ':checked' ) ) {
      $colors.hide();
    } else if ( $( this ).is( ':checked' ) && $( '#ywpo_custom_place_order_button_colors-custom' ).is( ':checked' ) ) {
      $colors.show();
    }
  } );
  $( '#ywpo_custom_place_order_button_enabled' ).trigger( 'change' );

} );
