{% load static %}

<dialog
  id="new_reaction_modal"
  class="modal"
  x-data="modalForm()"
  @close="reset()"
>
  <div class="modal-box max-w-3xl">
    <!-- Header -->
    <h3 class="font-bold text-lg">Create a new Reaction</h3>

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

    <!-- Body -->
    <div class="py-4">
      <form
        id="new-reaction-modal-form"
        accept-charset="UTF-8"
        action="{% url 'package reaction list' meta.current_package.uuid %}"
        method="post"
      >
        {% csrf_token %}

        <div class="form-control mb-3">
          <label class="label" for="reaction-name">
            <span class="label-text">Name</span>
          </label>
          <input
            id="reaction-name"
            class="input input-bordered w-full"
            name="reaction-name"
            placeholder="Name"
            required
          />
        </div>

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

        <div class="form-control mb-3">
          <label class="label" for="reaction-smirks">
            <span class="label-text">Reaction SMILES</span>
          </label>
          <div class="join mx-auto w-full">
            <input
              type="text"
              class="input input-bordered join-item w-full"
              name="reaction-smiles"
              placeholder="Reaction SMILES"
              id="reaction-smiles"
            />
            <button
              type="button"
              id="render-reaction-smiles"
              class="btn join-item"
            >
              Render
            </button>
          </div>
        </div>

        <div class="mb-3">
          {% with iframe_id="new_reaction_ketcher" height="624" text_input_id="reaction-smiles" %}
            {% include "components/ketcher.html" %}
          {% endwith %}
        </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('new-reaction-modal-form');"
        :disabled="isSubmitting"
      >
        <span x-show="!isSubmitting">Submit</span>
        <span
          x-show="isSubmitting"
          class="loading loading-spinner loading-sm"
        ></span>
        <span x-show="isSubmitting">Creating...</span>
      </button>
    </div>
  </div>

  <!-- Backdrop -->
  <form method="dialog" class="modal-backdrop">
    <button :disabled="isSubmitting">close</button>
  </form>
</dialog>
<script>
  document.addEventListener("DOMContentLoaded", function () {
    const renderButton = document.getElementById("render-reaction-smiles");
    renderButton.addEventListener("click", async function (e) {
      let smiles = document.getElementById("reaction-smiles").value.trim();

      if (smiles) {
        setKetcherMolecule(smiles, false);
      }
    });
  });
</script>
