{% load static %}

<dialog
  id="new_rule_modal"
  class="modal"
  x-data="{
    ...modalForm(),
    smirksVizHtml: '',
    updateSmirksViz() {
      const smirks = document.getElementById('rule-smirks').value;
      if (!smirks) {
        this.smirksVizHtml = '';
        return;
      }

      const img = new Image();
      img.src = '{% url 'depict' %}?is_query_smirks=true&smirks=' + encodeURIComponent(smirks);
      img.style.width = '100%';
      img.style.height = '100%';
      img.style.objectFit = 'cover';

      img.onload = () => {
        this.smirksVizHtml = img.outerHTML;
      };

      img.onerror = () => {
        this.smirksVizHtml = `
          <div class='alert alert-error' role='alert'>
            <h4 class='alert-heading'>Could not render SMIRKS!</h4>
            <p>Could not render SMIRKS - Have you entered a valid SMIRKS?</p>
          </div>`;
      };
    }
  }"
  @close="reset(); smirksVizHtml = ''"
>
  <div class="modal-box max-w-3xl">
    <!-- Header -->
    <h3 class="font-bold text-lg">Create a new Rule</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-rule-modal-form"
        accept-charset="UTF-8"
        action="{% url 'package rule list' meta.current_package.uuid %}"
        method="post"
      >
        {% csrf_token %}

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

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

        <div class="form-control mb-3">
          <label class="label" for="rule-smirks">
            <span class="label-text">SMIRKS</span>
          </label>
          <input
            id="rule-smirks"
            class="input input-bordered w-full"
            name="rule-smirks"
            placeholder="SMIRKS"
            @input="updateSmirksViz()"
          />
        </div>

        <div id="rule-smirks-viz" class="mb-3" x-html="smirksVizHtml"></div>

        <input
          type="hidden"
          name="rule-type"
          id="rule-type"
          value="SimpleAmbitRule"
        />
      </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-rule-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>
