{% load static %}

<dialog
  id="edit_user_modal"
  class="modal"
  x-data="modalForm()"
  @close="reset()"
>
  <div class="modal-box">
    <!-- Header -->
    <h3 class="font-bold text-lg">Update User Defaults</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="edit-user-modal-form"
        accept-charset="UTF-8"
        action=""
        method="post"
      >
        {% csrf_token %}

        <div class="form-control mb-3">
          <label class="label" for="default-package">
            <span class="label-text">Default Package</span>
          </label>
          <select
            id="default-package"
            name="default-package"
            class="select select-bordered w-full"
          >
            <option disabled>Select a Package</option>
            {% for p in meta.writeable_packages %}
              <option
                value="{{ p.url }}"
                {% if p.id == meta.user.default_package.id %}selected{% endif %}
              >
                {{ p.name|safe }}
              </option>
            {% endfor %}
          </select>
        </div>

        <div class="form-control mb-3">
          <label class="label" for="default-group">
            <span class="label-text">Default Group</span>
          </label>
          <select
            id="default-group"
            name="default-group"
            class="select select-bordered w-full"
          >
            <option disabled>Select a Group</option>
            {% for g in meta.available_groups %}
              <option
                value="{{ g.url }}"
                {% if g.id == meta.user.default_group.id %}selected{% endif %}
              >
                {{ g.name|safe }}
              </option>
            {% endfor %}
          </select>
        </div>

        <div class="form-control mb-3">
          <label class="label" for="default-prediction-setting">
            <span class="label-text">Default Prediction Setting</span>
          </label>
          <select
            id="default-prediction-setting"
            name="default-prediction-setting"
            class="select select-bordered w-full"
          >
            <option disabled>Select a Setting</option>
            {% for s in meta.available_settings %}
              <option
                value="{{ s.url }}"
                {% if s.id == meta.user.default_setting.id %}selected{% endif %}
              >
                {{ s.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"
      >
        Close
      </button>
      <button
        type="button"
        class="btn btn-primary"
        @click="submit('edit-user-modal-form')"
        :disabled="isSubmitting"
      >
        <span x-show="!isSubmitting">Update</span>
        <span
          x-show="isSubmitting"
          class="loading loading-spinner loading-sm"
        ></span>
        <span x-show="isSubmitting">Updating...</span>
      </button>
    </div>
  </div>

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