{% load static %}
<dialog
  id="add_pathway_edge_modal"
  class="modal"
  x-data="{
    isSubmitting: false,
    reactionImageUrl: '',

    reset() {
      this.isSubmitting = false;
      this.reactionImageUrl = '';
    },

    updateReactionImage() {
      const substratesSelect = document.getElementById('add_pathway_edge_substrates');
      const productsSelect = document.getElementById('add_pathway_edge_products');

      const substrates = [];
      for (const option of substratesSelect.selectedOptions) {
        substrates.push(option.dataset.smiles);
      }

      const products = [];
      for (const option of productsSelect.selectedOptions) {
        products.push(option.dataset.smiles);
      }

      if (substrates.length > 0 && products.length > 0) {
        const reaction = substrates.join('.') + '>>' + products.join('.');
        this.reactionImageUrl = '{% url "depict" %}?smirks=' + encodeURIComponent(reaction);
      } else {
        this.reactionImageUrl = '';
      }
    },

    submit() {
      this.isSubmitting = true;
      document.getElementById('add_pathway_edge_modal_form').submit();
    }
  }"
  @close="reset()"
>
  <div class="modal-box max-w-4xl">
    <!-- Header -->
    <h3 class="text-lg font-bold">Add a Reaction</h3>

    <!-- Close button (X) -->
    <form method="dialog">
      <button
        class="btn btn-sm btn-circle btn-ghost absolute top-2 right-2"
        :disabled="isSubmitting"
      >
        ✕
      </button>
    </form>

    <!-- Body -->
    <div class="py-4">
      <form
        id="add_pathway_edge_modal_form"
        accept-charset="UTF-8"
        action="{% url 'package pathway edge list' meta.current_package.uuid pathway.uuid %}"
        data-remote="true"
        method="post"
      >
        {% csrf_token %}
        <div class="form-control mb-3">
          <label class="label" for="edge-name">
            <span class="label-text">Name</span>
          </label>
          <input
            id="edge-name"
            type="text"
            class="input input-bordered w-full"
            name="edge-name"
            placeholder="Name"
          />
        </div>

        <div class="form-control mb-3">
          <label class="label" for="edge-description">
            <span class="label-text">Description</span>
          </label>
          <input
            id="edge-description"
            type="text"
            class="input input-bordered w-full"
            name="edge-description"
            placeholder="Description"
          />
        </div>

        <div class="mb-3 grid grid-cols-11 gap-2">
          <div class="col-span-5">
            <div class="form-control">
              <label class="label">
                <span class="label-text font-semibold">Substrate(s)</span>
              </label>
              <select
                id="add_pathway_edge_substrates"
                name="edge-substrates"
                class="select select-bordered h-32 w-full"
                multiple
                @change="updateReactionImage()"
              >
                {% for n in pathway.nodes %}
                  <option
                    data-smiles="{{ n.default_node_label.smiles }}"
                    value="{{ n.url }}"
                  >
                    {{ n.default_node_label.name|safe }}
                  </option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-span-1 flex items-center justify-center">
            <span class="text-2xl">→</span>
          </div>
          <div class="col-span-5">
            <div class="form-control">
              <label class="label">
                <span class="label-text font-semibold">Product(s)</span>
              </label>
              <select
                id="add_pathway_edge_products"
                name="edge-products"
                class="select select-bordered h-32 w-full"
                multiple
                @change="updateReactionImage()"
              >
                {% for n in pathway.nodes %}
                  <option
                    data-smiles="{{ n.default_node_label.smiles }}"
                    value="{{ n.url }}"
                  >
                    {{ n.default_node_label.name|safe }}
                  </option>
                {% endfor %}
              </select>
            </div>
          </div>
        </div>

        <div class="mb-3" x-show="reactionImageUrl" x-cloak>
          <img :src="reactionImageUrl" class="w-full" alt="Reaction preview" />
        </div>
      </form>
    </div>

    <!-- Footer -->
    <div class="modal-action">
      <button
        type="button"
        class="btn"
        onclick="this.closest('dialog').close()"
        :disabled="isSubmitting"
      >
        Close
      </button>
      <button
        type="button"
        class="btn btn-primary"
        @click="submit()"
        :disabled="isSubmitting"
      >
        <span x-show="!isSubmitting">Submit</span>
        <span
          x-show="isSubmitting"
          class="loading loading-spinner loading-sm"
        ></span>
        <span x-show="isSubmitting">Submitting...</span>
      </button>
    </div>
  </div>

  <!-- Backdrop -->
  <form method="dialog" class="modal-backdrop">
    <button :disabled="isSubmitting">close</button>
  </form>
</dialog>
