{% for name, data in presets|dictsort %}
  <div class="preset-item" data-preset="{{ name }}">
    <h2 class="anchor" id="{{ name }}">Preset {{ name }}</h2>
    <div class="row">
      <div class="col-xs-12 col-md-3">
        <ul>
          <li>
            <b>General:</b>
            <ul>
              <li><b>Name:</b> {{ data['preset'].name }}</li>
              <li><b>Count:</b> {{ data['preset'].count }}</li>
              <li><b>Flavor:</b> {{ data['preset'].config['flavor'] }}</li>
              <li><b>Image:</b> {{ data['preset'].config['image'] }}</li>
            </ul>
          </li>
        </ul>
      </div>  <!-- end section general -->
      <div class="col-xs-12 col-md-3">
        <ul>
          <li>
            <b>Network:</b>
            <ul>
              <li><b>Availability zone:</b> {{ data['preset'].config['network'].get('availability_zone', '') }}</li>
              <li>
                <b>Subnet:</b>
                <ul>
                  {% for subnet in data['preset'].config['network'].get('subnet', []) %}
                    <li>{{ subnet }}</li>
                  {% endfor %}
                </ul>
              </li>  <!-- end subnet -->
              <li>
                <b>Security groups:</b>
                <ul>
                  {% for sg in data['preset'].config['network'].get('security_groups', []) %}
                    <li>{{ sg }}</li>
                  {% endfor %}
                </ul>
              </li>
            </ul>
          </li>
        </ul>
      </div>  <!-- end section network -->
      <div class="col-xs-12 col-md-3">
        <ul>
          <li>
            <b>VM customs:</b>
            <ul>
              <li><b>Meta-tags:</b>
                <ul>
                  {% for k, v in data['preset'].config.get('meta_tags', {}).items() %}
                    <li><b>{{ k }}</b>: {{ v }}</li>
                  {% endfor %}
                </ul>
              </li>
              <li>
                <a href="#modal-userdata-{{loop.index}}" role="button" class="btn btn-xs btn-default" data-toggle="modal">Show userdata</a>
                <div id="modal-userdata-{{loop.index}}" class="modal fade">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">Userdata</h4>
                      </div>
                      <div class="modal-body">
                        <textarea cols=54 rows=20 readonly>{{ data['preset'].config.get('userdata', '') }}</textarea>
                      </div>
                      <div class="modal-footer">
                        <button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
                      </div>
                    </div> <!-- end modal content -->
                  </div> <!-- end modal dialog -->
                </div> <!-- end modal wrapper -->
              </li>
            </ul>
          </li>
        </ul>
      </div>  <!-- end section vm customs -->
  </div>  <!-- end preset config -->
  <h4>Last managed</h4>
  <div class="row sub-header">
    <div class="col-xs-12">
      <ul>
        <li>last_manage_by: {{ data['runtime'].last_managed_by }}</li>
        <li>last_manage_sec_ago: {{ (data['now'] - data['runtime'].last_managed)|int }}s </li>
      </ul>
    </div>
  </div>  <!-- end last managed -->
  <div class="table-responsive sub-header">
    <div>
      <h4 class="pull-left">Virtual machines ({{ data['vms']|length }}/{{ data['preset'].count }})</h4>
    </div>
    <table class="table table-striped">
      <thead>
        <tr>
          <th>Lp</th>
          <th>Vm_id</th>
          <th>Ip</th>
          <th>State</th>
          <th>Created at</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for vm in data['vms']|sorted %}
          <tr>
            <td>{{ loop.index }}</td>
            <td>{{ vm.id }}</td>
            <td>{{ vm.ip|join(", ") }}</td>
            <td {%if data['runtime'].failed_checks.get(vm.id) %} class="error" {% elif vm.state.value in ['nearbyshutdown', 'aftertimeshutdown'] %} class="warning" {% endif %}>
              {{ vm.state.value|upper }}
              {%if data['runtime'].failed_checks.get(vm.id) %}
                Healthcheck fails for {{ data['now']|int - data['runtime'].failed_checks[vm.id]['time']|int }}s, count: {{data['runtime'].failed_checks[vm.id]['count'] }})
              {% endif %}
            </td>
            <td>{{ vm.created.strftime('%Y-%m-%d %H:%M:%S') }}</td>
            <td>
              {% for name, link in data['preset']['config'].get('panel_links', {}).items() %}
                <a href="{{ link.replace('ip', (vm.ip or ["#"]).pop()) }}">{{ name }}</a> 
              {% endfor %}
            </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>  <!-- end vm list -->
</div>
{% endfor %}
