{% extends "base.html" %}
{# Optionally renders an img element, otherwise standard value or link rendering #}
{% macro render_item_value(v, width) -%}
    {% set val = v | string | trim | lower %}
    {% if val|length and
      (val.endswith(".jpg") or val.endswith(".jpeg") or val.endswith(".png") or val.endswith(".gif") or val.endswith(".bmp"))
    %}
        {# Ends with image extension: render img element with link to image #}
        <a href="{{ val }}"><img src="{{ val }}" alt="" width="{{ width }}"/></a>
    {% else %}
        {# All other cases: text or link value #}
        {{ v | urlize() }}
    {% endif %}
{%- endmacro %}
{% block title %}{{ super() }} {{ data['title'] }} - {{ data['id'] }}{% endblock %}
{% block crumbs %}{{ super() }} 
/ <a href="../../../collections">Collections</a>
{% for link in data['links'] %}
  {% if link.rel == 'collection' %}
/  <a href="{{ link['href'] }}">{{ link['title'] }}</a>
  {% endif %}
{% endfor %}     
/ <a href="../items">Items</a>
/ <a href="./{{ data['id'] }}">{{ data['title'] or data['id'] }}</a>
{% endblock %}
{% block extrahead %}
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"/>
    <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
{% endblock %}

{% block body %}
    <section id="item">
      <div class="row">
        <div class="col-sm">
          <h2>Item {{ data['id'] }}</h2>
        </div>
      </div>
      <div class="row">
        <div class="col-md-6 col-sm-12">
          <div class="row">
            <div class="col-sm-12">
              <div id="items-map"></div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-12">
              {% if data['prev'] or data['next'] %}
              <div class="row">
                <div class="col-sm-12">
                  {% for link in data['links'] %}
                  {% if link['rel'] == 'prev' %}
                  <a role="button" href="./{{ data['prev'] }}">Previous</a>
                  {% elif link['rel'] == 'next' %}
                  <a role="button" href="./{{ data['next'] }}">Next</a>
                  {% endif %}
                  {% endfor %}
                </div>
              </div>
              {% endif %}
            </div>
          </div>
        </div>
        <div class="col-md-6 col-sm-12">
            <table class="striped">
              <thead>
              <tr>
                <th>Property</th>
                <th>Value</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>id</td>
                <td>{{ data.id }}</td>
              </tr>
              {% for k, v in data['properties'].items() %}
              <tr>
                <td>{{ k }}</td>
                {% if k == 'links' %}
                <td>
                    <ul>
                        {% for l in v %}
                        <li><a href="{{ l['href'] }}">{{ l['title'] }}</a></li>
                        {% endfor %}
                    </ul>
                </td>
                {% else %}
                  <td>{{ render_item_value(v, 80) }}</td>
                {% endif %}
              </tr>
              {% endfor %}
            </tbody>
            </table>
        </div>
      </div>
    </section>
{% endblock %}

{% block extrafoot %}
    <script>
    var map = L.map('items-map').setView([{{ 45 }}, {{ -75 }}], 10);
    map.addLayer(new L.TileLayer(
        '{{ config['server']['map']['url'] }}', {
            maxZoom: 18,
            attribution: '{{ config['server']['map']['attribution'] }}'
        }
    ));
    var geojson_data = {{ data |to_json }};
    var items = new L.GeoJSON(geojson_data);

    map.addLayer(items);
    map.fitBounds(items.getBounds(), {maxZoom: 10});
    </script>
{% endblock %}
