{% if not included %}
{% extends 'index.html' %}
{% endif %}

{% set included = True %}

{% block content %}

<form data-name="{{data['@OBID']}}">

  <h4>{{data['@OBID']}}</h4>

  {% for template in data.Template %}
  <section data-template="{{template.TPLID}}">
    <h4>
      <small class="text-muted">{{loop.index}}</small>
      {{template.TPLID}}
    </h4>

    <!-- {{template.doc}} -->
    <!-- {{doc}} -->

  {% for subsystem in template | sort%}

    {% if (not subsystem=="TPLID") and (not subsystem=="TPLName") %}

    <fieldset data-subsystem="{{subsystem}}",
              data-instance="{{loop.index}}">

    <legend>
      {{subsystem}}
    </legend>

    {% for param in template[subsystem] %}
    <div class="row mb-2">
      <label class="col-2 col-form-label" for="{{template.TPLID}}-{{param.name}}">{{param.name}}</label>
      <div class="col">

      <div class="input-group">

        {% if not param.options %}

        <input type="text"
               class="form-control"
               id="{{param.name}}"
               name="{{param.name}}"
               value="{{param.value}}"
               placeholder="dummy"
               {% if param.type == 'ref' %}
               disabled
               {% endif %}
               >

        {% else %}

        <select id="{{param.name}}" name="{{param.name}}" class="form-control"
               {% if param.type == 'ref' %}
               disabled
               {% endif %}
                >

               {% if param.type == 'ref' %}
               <option value="{{param.value}}">
                 {{param.value}}
               </option>
               {% else %}

               {% for o in param.options %}
               <option value="{{o}}" {% if o == param.value %} selected {% endif %}>
                 {{o}}
               </option>
               {% endfor %}

               {% endif %}


        </select>

        {% endif %}

        <button type="button" class="btn btn-outline-secondary"
                data-bs-container="body"
                data-bs-toggle="popover"
                data-bs-placement="right"
                data-bs-content="{{param.help}}">
          ?
        </button>

      </div>
    </div>


    </div>
    {% endfor %}

    {% endif %}

  </fieldset>
  {% endfor %}

    <button class="col mb-2 btn btn-secondary" type="button"
            data-control="delete-template-instance"
            data-name="{{data['@OBID']}}"
            data-instance="{{loop.index}}">Delete Template instance</button>

</section>

  {% endfor %}

</form>

<div class="row">

  <label for="tpl-list-options" class="col-2 col-form-label">Add template</label>
  <div class="col">
    <select id="tpl-list-options" class="form-select">
      <option  selected disabled hidden>
        -- Select a template --
      </option>
      {% for ob in tpl | sort(attribute='name') %}
      {{ob}}
      <option data-index="{{loop.counter}}" value="{{ob.Template.TPLID}}">
        {{ob.Template.TPLID}}
      </option>
      {% endfor %}
    </select>

  </div>

    <div class="col-auto">
      <button class="btn btn-secondary" type="submit"
              id="update-ob">Update this OB!</button>
    </div>

    <div class="col-auto">
      <button class="me-2 btn btn-secondary" type="submit"
              id="delete-ob">Delete this OB!</button>
    </div>

</div>




{% endblock content %}
