{% extends 'base/layout.html' %}
{% load form_helpers %}
{% load helpers %}

{% comment %}
Blocks:
  form: Content within the <form> element
  buttons: Form submission buttons

Context:
  object: Python instance of the object being edited
  form: The edit form
  return_url: The URL to which the user is redirected after submitting the form
{% endcomment %}

{% block title %}
  {% if object.pk %}Editing {{ object|meta:"verbose_name" }} {{ object }}{% else %}Add a new {{ object|meta:"verbose_name" }}{% endif %}
{% endblock title %}

{% block tabs %}
  <ul class="nav nav-tabs px-3">
    <li class="nav-item" role="presentation">
      <button class="nav-link active" id="edit-form-tab" data-bs-toggle="tab" data-bs-target="#edit-form" type="button" role="tab" aria-controls="edit-form" aria-selected="true">
        {% if object.pk %}Edit{% else %}Create{% endif %}
      </button>
    </li>
  </ul>
{% endblock tabs %}

{% block content-wrapper %}
  <div class="tab-content">
    <div class="tab-pane show active" id="edit-form" role="tabpanel" aria-labelledby="object-list-tab">

      {# Warn about missing prerequisite objects #}
      {% if prerequisite_model %}
        {% include 'inc/missing_prerequisites.html' %}
      {% endif %}

      {# Link to model documentation #}
      {% if settings.DOCS_ROOT and object.docs_url %}
        <div class="float-end">
          <a href="{{ object.docs_url }}" target="_blank" class="btn btn-sm btn-outline-secondary" title="View model documentation">
            <i class="mdi mdi-help-circle"></i> Help
          </a>
        </div>
      {% endif %}

      <form action="" method="post" enctype="multipart/form-data" class="form-object-edit mt-5">
        {% csrf_token %}

        {% block form %}
          {% if form.fieldsets %}

            {# Render hidden fields #}
            {% for field in form.hidden_fields %}
              {{ field }}
            {% endfor %}

            {# Render grouped fields according to Form #}
            {% for group, fields in form.fieldsets %}
              <div class="field-group mb-5">
                {% if group %}
                  <div class="row mb-2">
                    <h5 class="offset-sm-3">{{ group }}</h5>
                  </div>
                {% endif %}
                {% for name in fields %}
                  {% with field=form|getfield:name %}
                    {% if not field.field.widget.is_hidden %}
                      {% render_field field %}
                    {% endif %}
                  {% endwith %}
                {% endfor %}
              </div>
            {% endfor %}
            {% render_field form.cluster_type %}
            {% render_field form.cluster %}
            {% render_field form.virtual_machine %}

            {# Template or custom #}
            {# Device/VM selection #}
            <div class="row mb-2">
              <div class="offset-sm-3">
                <ul class="nav nav-pills" role="tablist">
                  <li role="presentation" class="nav-item">
                    <button role="tab" type="button" id="lvm_tab" data-bs-toggle="tab" aria-controls="lvm" data-bs-target="#lvm" class="nav-link {% if not form.initial.virtual_machine %}active{% endif %}">
                      LVM
                    </button>
                  </li>
                  <li role="presentation" class="nav-item">
                    <button role="tab" type="button" id="linux_volume_tab" data-bs-toggle="tab" aria-controls="linux_volume" data-bs-target="#linux_volume" class="nav-link {% if form.initial.virtual_machine %}active{% endif %}">
                      Linux Volume
                    </button>
                  </li>
                  <li role="presentation" class="nav-item">
                    <button role="tab" type="button" id="windows_volume_tab" data-bs-toggle="tab" aria-controls="windows_volume" data-bs-target="#windows_volume" class="nav-link {% if form.initial.virtual_machine %}active{% endif %}">
                      Windows Volume
                    </button>
                  </li>
                </ul>
              </div>
            </div>
            <div class="tab-content p-0 border-0">
              <div class="tab-pane {% if not form.initial.virtual_machine %}active{% endif %}" id="lvm" role="tabpanel" aria-labeled-by="lvm_tab">
                {% render_field form.size %}
                {% render_field form.lv_name %}
                {% render_field form.vg_name %}
                {% render_field form.path %}
              </div>
              <div class="tab-pane {% if form.initial.virtual_machine %}active{% endif %}" id="linux_volume" role="tabpanel" aria-labeled-by="linux_volume_tab">
                {% render_field form.size %}
              </div>
              <div class="tab-pane {% if form.initial.virtual_machine %}active{% endif %}" id="windows_volume" role="tabpanel" aria-labeled-by="windows_volume_tab">
                {% render_field form.size %}
              </div>
            </div>

          {% else %}
            {# Render all fields in a single group #}
            <div class="field-group mb-5">
              {% render_form form %}
            </div>
          {% endif %}

        {% endblock form %}

        <div class="text-end my-3">
          {% block buttons %}
            {% if object.pk %}
              <button type="submit" name="_update" class="btn btn-primary">
                Save
              </button>
            {% else %}
              <button type="submit" name="_create" class="btn btn-primary">
                Create
              </button>
              <button type="submit" name="_addanother" class="btn btn-outline-primary">
                Create & Add Another
              </button>
            {% endif %}
            <a class="btn btn-outline-danger" href="{{ return_url }}">Cancel</a>
          {% endblock buttons %}
        </div>
      </form>
    </div>
  </div>

{% endblock content-wrapper %}
