import { Controller } from "stimulus";
export default class extends Controller {
  static targets = ["input"];

  connect() {
    this.inputTargets.forEach((i) => {
      if (i.checked) {
        this.doSelectPaymentMethod(i.dataset.paymentmethodId);
      }
    });
  }

  selectPaymentMethod(event) {
    this.doSelectPaymentMethod(event.target.dataset.paymentmethodId);

    const stripeCardSelector =
      this.application.getControllerForElementAndIdentifier(
        document
          .getElementById(event.target.dataset.paymentmethodId)
          .querySelector('[data-controller="stripe-cards"]'),
        "stripe-cards"
      );
    stripeCardSelector?.initSelectedCard();
  }

  doSelectPaymentMethod(paymentMethodContainerId) {
    Array.from(
      document.getElementsByClassName("paymentmethod-container")
    ).forEach((e) => {
      if (e.id === paymentMethodContainerId) {
        e.style.display = "block";
        this.removeDisabledAttributeOnInput(e);
      } else {
        e.style.display = "none";
        this.addDisabledAttributeOnInput(e);
      }
    });
  }

  getFormElementsArray(container) {
    return Array.from(container.querySelectorAll("input, select, textarea"));
  }

  addDisabledAttributeOnInput(container) {
    this.getFormElementsArray(container).forEach((i) => {
      i.disabled = true;
    });
  }

  removeDisabledAttributeOnInput(container) {
    this.getFormElementsArray(container).forEach((i) => {
      i.disabled = false;
    });
  }
}

import { Controller } from "stimulus";
export default class extends Controller {
  static targets = ["input"];

  connect() {
    this.inputTargets.forEach((input) => {
      if (input.checked) {
        this.setPaymentMethod(input.dataset.paymentmethodId);
      }
    });
  }

  selectPaymentMethod(event) {
    this.setPaymentMethod(event.target.dataset.paymentmethodId);

    const stripeCardSelector =
      this.application.getControllerForElementAndIdentifier(
        document
          .getElementById(event.target.dataset.paymentmethodId)
          .querySelector('[data-controller="stripe-cards"]'),
        "stripe-cards"
      );
    stripeCardSelector?.initSelectedCard();
  }

  setPaymentMethod(paymentMethodContainerId) {
    Array.from(
      document.getElementsByClassName("paymentmethod-container")
    ).forEach((container) => {
      const enabled = container.id === paymentMethodContainerId

      if (enabled) {
        container.style.display = "block";
        this.toggleFieldsEnabled(container, enabled);
      } else {
        container.style.display = "none";
        this.toggleFieldsEnabled(container, enabled);
      }
    });
  }

  toggleFieldsEnabled(container, enabled) {
    this.subFormElements(container).forEach((field) => {
      field.disabled = !enabled;
    });
  }

  subFormElements(container) {
    return Array.from(container.querySelectorAll("input, select, textarea"));
  }
}

Simplify payment method controller