{% load static %}
<!-- Delete Edge -->
<dialog
  id="delete_pathway_edge_modal"
  class="modal"
  x-data="modalForm({ state: { selectedEdge: '', imageUrl: '' } })"
  @modal-opened.window="
   const links = d3.selectAll('path.highlighted');
   if (!links.empty()) {
        const el = links.node();
        const selectElement = document.getElementById('delete_pathway_edge_edges');
        console.log(el);
        console.log(el.__data__);
        for (let option of selectElement.options) {
            if (option.value === el.__data__.url) {
                option.selected = true;
                break;
            }
        }
        selectElement.dispatchEvent(new Event('change'));
   }
  "
  @close="reset()"
>
  <div class="modal-box">
    <!-- Header -->
    <h3 class="text-lg font-bold">Delete Edge</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">
      <p class="mb-4">
        Deletes the Edge. Nodes referenced by this edge will remain.
      </p>
      <form
        id="delete-pathway-edge-modal-form"
        accept-charset="UTF-8"
        action=""
        method="post"
      >
        {% csrf_token %}
        <div class="form-control">
          <label class="label" for="delete_pathway_edge_edges">
            <span class="label-text">Select Reaction to delete</span>
          </label>
          <select
            id="delete_pathway_edge_edges"
            name="edge-url"
            class="select select-bordered w-full"
            x-model="selectedEdge"
            @change="imageUrl = selectedEdge ? selectedEdge + '?image=svg' : ''"
            required
          >
            <option value="" disabled selected>
              Select Reaction to delete
            </option>
            {% for e in pathway.edges %}
              <option value="{{ e.url }}">{{ e.edge_label.name|safe }}</option>
            {% endfor %}
          </select>
        </div>
        <input type="hidden" id="hidden" name="hidden" value="delete" />
      </form>

      <!-- Image Preview -->
      <div class="mt-4" x-show="imageUrl" x-cloak>
        <img :src="imageUrl" class="w-full" alt="Edge preview" />
      </div>
    </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-error"
        @click="setFormAction('delete-pathway-edge-modal-form', selectedEdge); submit('delete-pathway-edge-modal-form')"
        :disabled="isSubmitting || !selectedEdge"
      >
        <span x-show="!isSubmitting">Delete</span>
        <span
          x-show="isSubmitting"
          class="loading loading-spinner loading-sm"
        ></span>
        <span x-show="isSubmitting">Deleting...</span>
      </button>
    </div>
  </div>

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