{% extends 'series-layout.html' %}
{% from 'macros.html' import pagination, visualize_metadata_value %}

{% block menu %}
{{ series_menu('Metadata') }}
{% endblock %}

{% 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('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, startDate=start_date, endDate=end_date, sort=sort_field, direction=other_direction) }}">
            {{ caller() }}
            {% if sort_direction == 'desc' %}
            {{ bootstrap_icon('caret-down-fill') }}
            {% else %}
            {{ bootstrap_icon('caret-up-fill') }}
            {% endif %}
        </a>
    {% else %}
        <a class="text-reset text-decoration-none" href="{{ url_for('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, startDate=start_date, endDate=end_date, sort=field, direction=sort_direction) }}">
            {{ caller() }}
            {% if sort_direction == 'desc' %}
            {{ bootstrap_icon('caret-down', 'text-secondary') }}
            {% else %}
            {{ bootstrap_icon('caret-up', 'text-secondary') }}
            {% endif %}
        </a>
    {% endif %}
{% endmacro %}


{% block series_title %}
<h2 class="h4">Metadata audit trail</h2>
{% endblock %}

{% block series_content %}

<form class="mb-3">
    <input type="hidden" name="sourcename" value="{{ source_name }}">
    <input type="hidden" name="seriesid" value="{{ series.series_id }}">
    <input type="hidden" name="blockevaluationid" value="{{ block_evaluation.db_id }}">
    <div class="row align-items-end gx-2">
        <div class="col-lg">
            <label for="startDateInput" class="col-form-label">Start date</label>
            <input type="datetime-local" step="1" class="form-control" id="startDateInput" name="startDate" data-ts-datetime-value="{{ start_date }}">
        </div>
        <div class="col-lg">
            <label for="endDateInput" class="col-form-label">End date</label>
            <input type="datetime-local" step="1" class="form-control" id="endDateInput" name="endDate" data-ts-datetime-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>
{% if audit_trail|count > 0 %}
{{ pagination(
    paging,
    url_for('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, startDate=start_date, endDate=end_date, page=paging.page-1, sort=sort_field, direction=sort_direction),
    url_for('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, 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, 'field_name') %}
                Name
                {% endcall %}
            </th>
            <th scope="col">
                Old value
            </th>
            <th scope="col">
                New value
            </th>
            <th scope="col">
                {% call visualize_sorting(sort_field, sort_direction, 'modified_date') %}
                Modified date
                {% endcall %}
            </th>
        </tr>
    </thead>
    <tbody>
        {% for event in audit_trail %}
        <tr class="border-bottom">
            <td>{{ event.field_name|capitalize }}</td>
            {{ visualize_metadata_value(event.field_name, event.old_value)}}
            {{ visualize_metadata_value(event.field_name, event.new_value)}}
            <td>{{ event.modified_date|ts_datetime}}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{{ pagination(
    paging,
    url_for('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, startDate=start_date, endDate=end_date, page=paging.page-1, sort=sort_field, direction=sort_direction),
    url_for('.metadata_audit_trail', blockevaluationid=block_evaluation.db_id, seriesid=series.series_id, startDate=start_date, endDate=end_date, page=paging.page+1, sort=sort_field, direction=sort_direction)
) }}
{% endif %}

{% endblock series_content %}
