{% extends "base.html" %}

{% block content %}

<link rel="stylesheet" href="{{ url_for('static', filename='css/flags/flags.css') }}"/>

<div class="container">
<div class="panel panel-success">
<div class="panel-heading"><h3 class="panel-title">Logbook</h3></div>
<div class="panel-body">


<form>
<div class="well">
<select name="country" onchange="this.form.submit();">
	<option value="">(none)</option>
    {% for country in countries %}
    <option value="{{ country.iso2 }}"{% if sel_country == country.iso2 %} SELECTED{% endif %}>{{ country.iso2 }}</option>
    {% endfor %}
</select>
<select name="airport" onchange="this.form.submit();">
	<option value="">(none)</option>
    {% for airport in airports %}
    <option value="{{ airport.id }}"{% if sel_airport == airport.id %} SELECTED{% endif %}>{{ airport.name }}</option>
    {% endfor %}
</select>
<select name="date" onchange="this.form.submit();">
	{% if dates|length %}
	    {% for entry in dates %}
	    <option value="{{ entry.date }}"{% if sel_date|string() == entry.date|string() %} SELECTED{% endif %}>{{ entry.date }} ({{ entry.logbook_count }})</option>
	    {% endfor %}
	{% else %}
		<option value="">(none)</option>
	{% endif %}
</select>
</div>
</form>


{% if logbook is not none %}
<table class="datatable table table-striped table-bordered">
	<tr>
		<th>Nr.</th>
        <th>Aircraft</th>
        <th>Type</th>
        <th colspan="2">Takeoff</th>
        <th colspan="2">Landing</th>
        <th>Time</th>
        <th>AGL</th>
        <th>Remark</th>
	</tr>
	{% for entry in logbook %}
	<tr>
		<td>{{ loop.index }}</td>
        <td><a href="{{ url_for('main.device_detail', id=entry.device.id) }}">{% if entry.device.info is not none and entry.device.info.registration|length %}{{ entry.device.info.registration }}{% else %}[{{ entry.device.address }}]{% endif %}</a></td>
        <td>{% if entry.device.info is not none and entry.device.info.aircraft|length %}{{ entry.device.info.aircraft }}{% else %}-{% endif %}</td>
        <td>{% if entry.takeoff_timestamp is not none and entry.takeoff_airport.id == sel_airport %} {{ entry.takeoff_timestamp.strftime('%H:%M') }} {% endif %}</td>
        <td>{% if entry.takeoff_track is not none and entry.takeoff_airport.id == sel_airport %} {{ '%02d' | format(entry.takeoff_track/10) }} {% endif %}</td>
        <td>{% if entry.landing_timestamp is not none and entry.landing_airport.id == sel_airport %} {{ entry.landing_timestamp.strftime('%H:%M') }} {% endif %}</td>
        <td>{% if entry.landing_track is not none and entry.landing_airport.id == sel_airport %} {{ '%02d' | format(entry.landing_track/10) }} {% endif %}</td>
        <td>{% if entry.duration is not none %}{{ entry.duration }}{% endif %}</td>
        <td>{% if entry.max_altitude is not none %}{{ '%0.1f'|format(entry.max_altitude - entry.takeoff_airport.altitude) }} m{% endif %}</td>
        <td>
        	{% if entry.takeoff_airport is not none and entry.takeoff_airport.id != sel_airport %}Take Off: <img src="{{ url_for('static', filename='img/Transparent.gif') }}" class="flag flag-{{ entry.takeoff_airport.country_code|lower }}" alt="{{ entry.takeoff_airport.country_code }}"/> <a href="{{ url_for('main.logbook', country=entry.takeoff_airport.country_code, airport=entry.takeoff_airport.id, date=sel_date) }}">{{ entry.takeoff_airport.name }}</a>
	        {% elif entry.landing_airport is not none and entry.landing_airport.id != sel_airport %}Landing: <img src="{{ url_for('static', filename='img/Transparent.gif') }}" class="flag flag-{{ entry.landing_airport.country_code|lower }}" alt="{{ entry.landing_airport.country_code }}"/> <a href="{{ url_for('main.logbook', country=entry.takeoff_airport.country_code, airport=entry.landing_airport.id, date=sel_date) }}">{{ entry.landing_airport.name }}</a>
	        {% endif %}
	    </td>
	</tr>
	{% endfor %}
</table>
{% endif %}
</div>
</div>

{% endblock %}