{% load static %}

<dialog
  id="new_package_modal"
  class="modal"
  x-data="{
    isSubmitting: false,
    packageClassification: null,

    reset() {
      this.isSubmitting = false;
      this.packageClassification = null;
    },

    setFormData(data) {
      this.formData = data;
    },

    get isSecret() {
      return this.packageClassification === '20';
    },

    submit(formId) {
      const form = document.getElementById(formId);

      // Remove previously injected inputs
      form.querySelectorAll('.dynamic-param').forEach(el => el.remove());

      // Add values from dynamic form into the html form
      if (this.formData) {
        Object.entries(this.formData).forEach(([key, value]) => {
          const input = document.createElement('input');
          input.type = 'hidden';
          input.name = key;
          input.value = value;
          input.classList.add('dynamic-param');

          form.appendChild(input);
        });
      }

      if (form && form.checkValidity()) {
        this.isSubmitting = true;
        form.submit();
      } else if (form) {
        form.reportValidity();
      }
    }

  }"
  @close="reset()"
>
  <div class="modal-box max-w-3xl">
    <!-- Header -->
    <h3 class="text-lg font-bold">New Package</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="new_package_form"
        accept-charset="UTF-8"
        action=""
        method="post"
      >
       {% csrf_token %}

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

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

       <!-- Classification Level -->
        <div class="form-control mb-3">
          <label class="label" for="package-classification">
            <span class="label-text">Package Classification</span>
          </label>
          <select
            id="package-classification"
            name="package-classification"
            class="select select-bordered w-full"
            x-model="packageClassification"
            required
          >
            <option value="null" disabled selected>Select Classification</option>
            <option value="0">Internal</option>
            <option value="10">Restricted</option>
            <option value="20">Secret</option>
          </select>
        </div>

       <!-- Secret Groups -->
        <div class="form-control mb-3" x-show="isSecret" x-cloak>
          <label class="label" for="package-data-pool">
            <span class="label-text">Data Pool for SECRET Package</span>
          </label>
          <p>Only users with this role can be granted access to this package</p>
          <select
            id="package-data-pool"
            name="package-data-pool"
            class="select select-bordered w-full"
          >
            <option value="" disabled selected>Select Data Pool</option>
              {% for obj in meta.secret_groups %}
                <option value="{{ obj.url }}">{{ obj.name|safe }}</option>
              {% endfor %}
          </select>
        </div>

     </form>
    </div>

    <!-- Footer -->
    <div class="modal-action">
      <button
        type="button"
        class="btn"
        onclick="this.closest('dialog').close()"
        :disabled="isSubmitting"
      >
        Cancel
      </button>
      <button
        type="button"
        class="btn btn-primary"
        @click="submit('new_package_form')"
        :disabled="isSubmitting || !selectedType || loadingSchemas"
      >
        <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>