{% include "header.html" %}

{% set show_prev_link = false %}
{% set show_next_link = false %}
{%  if 'items?' in url %}
  {% set urlq = url + '&' %}
  {% else %}
  {% set urlq = url + '?' %}
{% endif %}


<nav aria-label="breadcrumb">
  <ol class="breadcrumb bg-light">
    {% for crumb in crumbs %}
      {% if not loop.last %}
    <li class="breadcrumb-item"><a href="{{ crumb.url }}/">{{ crumb.part }}</a></li>
      {% else %}<li class="breadcrumb-item active" aria-current="page">{{ crumb.part }}</li>
      {% endif %}
    {% endfor %}

    <li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=geojson">GeoJSON</a></li>
  </ol>
</nav>

<h1>Collection Items: {{ response.title or response.id }}</h1>

<div id="map" class="rounded" style="width:100%; height:400px;">Loading...</div>

<p>
  <b>Number of matching items:</b> {{ response.numberMatched }}<br/>
  <b>Number of returned items:</b> {{ response.numberReturned }}<br/>
  <b>Page:</b> <span id="current_page"></span> of <span id="total_pages"></span><br/>
</p>

<div class="form-row" style="margin-bottom:10px;" id="controls">
  {% for link in response.links %}
    {% if link.rel == 'prev' %}
      <div class="col-auto"><a class="btn btn-secondary" title="previous page" href="{{ link.href }}">prev</a></div>
    {% endif %}
  {% endfor %}
  <div class="col-auto">
    <select class="form-control" id="limit"> <!-- TODO: dynamically populate the values based on oga_max_limit -->
      <option value="10">page size</option>
      <option>10</option>
      <option>100</option>
      <option>1000</option>
      <option>10000</option>
    </select>
  </div>
  {% for link in response.links %}
    {% if link.rel == 'next' %}
      <div class="col-auto"><a class="btn btn-secondary" title="next page" href="{{ link.href }}">next</a></div>
    {% endif %}
  {% endfor %}
</div>
<div class="table-responsive">
{% if response.features is defined and response.features|length > 0 %}
<table class="table">
  <thead class="thead-light">
    <th>ID</th>
{% for key, value in response.features.0.properties.items() %}
    <th>{{ key }}</th>
{% endfor %}
  </thead>
  <tbody>
{% for feature in response.features %}
    <tr>
      <td><a target="_blank" href="{{ template.api_root }}/collections/{{ response.id }}/items/{{ feature.id }}">{{ feature.id }}</a></td>
  {% for key, value in feature.properties.items() %}
      <td>{{ value }}</td>
  {% endfor %}
    </tr>
{% endfor %}
  </tbody>
</table>
{% endif %}
</div>

<script>
function changePageSize() {
  var url = "{{ template.api_root }}/collections/{{ response.id }}/items?";
  url += "limit=" + $("#limit").val();
  window.location.href = url;
}
$(function() {

  //
  // mapping
  //
  var geojson = {{ response|tojson }};
  var map = L.map('map').setView([0, 0], 1);
  map.addLayer(new L.TileLayer(
    'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 18,
      attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
    }
  ));
  var features = L.geoJSON(geojson).addTo(map);
  map.fitBounds(features.getBounds());

  //
  // paging
  //
  var offset = 0; // defaults
  var limit = 10;

  {% if "offset" in template.params %}
  offset = {{ template.params.offset }};
  {% endif %}
  {% if "limit" in template.params %}
  limit = {{ template.params.limit }};
  {% endif %}

  var current_page = (offset + limit)/limit;
  $("#current_page").html(current_page);
  var total_pages = Math.ceil({{ response.numberMatched }}/limit);
  $("#total_pages").html(total_pages);

  //
  // event handling
  //
  $("#limit").on("change", function() {
    changePageSize();
  });
});
</script>

{% include "footer.html" %}
