{% extends "base_list.html" %}
{% load mathfilters %}
{% load static %}
{% load naturalduration %}

{% block title %}{{object.title}}{% endblock %}

{% block head_extra %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<style>
    .cover img {
        width: 250px;
    }

    .cover {
        float: left;
        width: 252px;
        padding: 0;
    }

    .summary {
        float: left;
        width: 600px;
        margin-left: 10px;
    }

    #map {
        height: 400px;
    }
</style>
{% endblock %}

{% block lists %}

<div class="row">
    {% if trail_gpx_url %}
    <div class="mb-3">
        <div id="map"></div>
    </div>
    {% endif %}
    <div class="summary">
        {% if object.description%}
        <p>{{object.description|safe|linebreaks|truncatewords:160}}</p>
        <hr />
        {% endif %}
        <p style="float:right;">
            <a href="{{object.strava_link}}"><img src="{% static "images/strava-logo.png" %}" width=35></a>
        </p>
    </div>
</div>
<div class="row">
    <p>{{object.scrobble_set.count}} scrobbles</p>
    <p>
        <a href="{{object.start_url}}">Hike again</a>
    </p>
</div>
{% if charts %}
<div class="row">
    <div class="col-md">
        {% include "scrobbles/_chart_links.html" %}
    </div>
</div>
{% endif %}
<div class="row">
    <div class="col-md">
        <h3>Last scrobbles</h3>
        <div class="table-responsive">
            <table class="table table-striped table-sm">
                <thead>
                    <tr>
                        <th scope="col">Date</th>
                    </tr>
                </thead>
                <tbody>
                    {% for scrobble in object.scrobble_set.all|dictsortreversed:"timestamp" %}
                    <tr>
                        <td><a href={{scrobble.get_absolute_url}}>{{scrobble.local_timestamp}}</a></td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
{{ block.super }}
{{ log_form.media }}
{% if trail_gpx_url %}
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-gpx@2.2.0/gpx.min.js"></script>
<script>
var map = L.map('map');
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
    referrerPolicy: 'origin'
}).addTo(map);
var gpx = new L.GPX("{{ trail_gpx_url|escapejs }}", {
    async: true,
    polyline_options: { color: '#e74c3c' }
});
gpx.on('loaded', function(e) {
    map.fitBounds(e.target.getBounds());
});
gpx.addTo(map);
</script>
{% endif %}
{% endblock %}
