{% extends 'tags-layout.html' %}

{% from 'macros.html' import pagination %}

{% macro visualize_sorting(sort_field, sort_direction, field) %}
    {% if sort_field == field %}
        {% if sort_direction == 'asc' %}
        {% set other_direction = 'desc' %}
        {% else %}
        {% set other_direction = 'asc' %}
        {% endif %}
        <a class="text-reset text-decoration-none" href="{{ url_for('prepare.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, sort=field, direction=other_direction) }}">
            {{ caller() }}
            {% if sort_direction == 'desc' %}
            <svg class="bi" width="16" height="16" fill="currentColor">
                <use xlink:href="/static/dist/bootstrap-icons.svg#caret-down-fill"/>
            </svg>
            {% else %}
            <svg class="bi" width="16" height="16" fill="currentColor">
                <use xlink:href="/static/dist/bootstrap-icons.svg#caret-up-fill"/>
            </svg>
            {% endif %}
        </a>
    {% else %}
        <a class="text-reset text-decoration-none" href="{{ url_for('prepare.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, sort=field, direction=sort_direction) }}">
            {{ caller() }}
            {% if sort_direction == 'desc' %}
            <svg class="bi text-secondary" width="16" height="16" fill="currentColor">
                <use xlink:href="/static/dist/bootstrap-icons.svg#caret-down"/>
            </svg>
            {% else %}
            <svg class="bi text-secondary" width="16" height="16" fill="currentColor">
                <use xlink:href="/static/dist/bootstrap-icons.svg#caret-up"/>
            </svg>
            {% endif %}
        </a>
    {% endif %}
{% endmacro %}

{% block menu %}
{{ prepare_menu('Event frames') }}
{% endblock %}

{% block tags_title %}
<h2 class="h4">{{ event_frame_type }}</h2>
{% endblock %}

{% block tags_action %}
<button type="submit"
        class="btn btn-primary"
        form="eventframeselection"
        formmethod="POST"
        formaction="{{ url_for('.export_event_frames') }}">
    Export to XLSX
</button>
{% endblock %}

{% block tags_content %}
<form class="mb-3" id="eventframeselection">
    <input type="hidden" name="seriesid" value="{{ series.series_id }}">
    <input type="hidden" name="sort" value="{{ sort_field }}">
    <input type="hidden" name="direction" value="{{ sort_direction }}">
    <input type="hidden" name="type" value="{{ event_frame_type }}">
    <div class="row align-items-end gx-2">
        <div class="col-lg">
            <label for="startDateInput" class="col-form-label">Start date</label>
            <input class="form-control bg-white" id="startDateInput" name="startDate" value="{{ start_date }}">
        </div>
        <div class="col-lg">
            <label for="endDateInput" class="col-form-label">End date</label>
            <input class="form-control bg-white" id="endDateInput" name="endDate" value="{{ end_date }}">
        </div>
        <div class="col-lg-auto mt-lg-0 mt-2">
            <button type="submit" class="btn btn-secondary">List</button>
        </div>
    </div>
</form>


<script>
    flatpickr("#startDateInput", flatpickrOptions);
    flatpickr("#endDateInput", flatpickrOptions);
</script>

{% if event_frames %}
{{ pagination(
    paging,
    url_for('.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, page=paging.page-1, sort=sort_field, direction=sort_direction),
    url_for('.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, page=paging.page+1, sort=sort_field, direction=sort_direction)
) }}
<table class="ts-table table table-borderless">
    <thead class="thead-secondary">
        <tr class="border-top border-bottom">
            <th scope="col">
                {% call visualize_sorting(sort_field, sort_direction, 'start_date') %}
                    Start date
                {% endcall %}
            </th>
            <th scope="col">
                {% call visualize_sorting(sort_field, sort_direction, 'end_date') %}
                    End date
                {% endcall %}
            </th>
            <th scope="col">
                {% call visualize_sorting(sort_field, sort_direction, 'duration') %}
                    Duration
                {% endcall %}
            </th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
        {% for frame in event_frames %}
        <tr class="border-bottom">
            <td>{{ frame.start_date|datetimeformat }}</td>
            {% if frame.end_date is not none %}
            <td>{{ frame.end_date|datetimeformat }}</td>
            <td>{{ (frame.end_date - frame.start_date)|timedeltaformat }}</td>
            {% else %}
            <td>-</td>
            <td>-</td>
            {% endif %}
            <td>
            <form action="{{ url_for('.chart') }}">
                <input type="hidden" name="seriesid" value="{{ series.series_id }}">
                <input type="hidden" name="startDate" value="{{ frame.start_date }}">
                {% if frame.end_date is not none %}
                <input type="hidden" name="endDate" value="{{ frame.end_date }}">
                {% endif %}
                <input type="hidden" name="context" value="10">
                <input type="submit" class="btn btn-secondary btn-sm" value="Visualize">
            </form>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{{ pagination(
    paging,
    url_for('.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, page=paging.page-1, sort=sort_field, direction=sort_direction),
    url_for('.event_frames', seriesid=series.series_id, type=event_frame_type, startDate=start_date, endDate=end_date, page=paging.page+1, sort=sort_field, direction=sort_direction)
) }}
{% endif %}

{% endblock %}}
