{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
{% load wger_extras %}

{% block title %}{% trans "Workout log calendar" %}{% endblock %}

{% block header %}
<script>
$(document).ready(function() {
    $('table.month td.filled a').click(function(e){
        e.preventDefault();
        $('.calendar-log-data').hide();
        $('#' + $(this).data('log')).toggle();
    });

    // Popup for adding logs
    $('#calendar-day-select-link').click(function(e){
        e.preventDefault();
        $('#calendar-day-select-popup').dialog({
            modal: true
        });
    });
 });
</script>
{% endblock %}

{% block content %}
{{calendar|safe}}

{% for date in logs %}
<div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">
    <h3>{{date}}</h3>

    {% regroup logs|get_item:date by exercise as grouped_logs %}

    {# TODO: what if some of the entries don't belong to a workout? #}
    {% with tmp=grouped_logs|first %}
    {% with first_log=tmp.list|first %}
    {% with session=first_log.get_workout_session %}
        <p>
            <a href="{{first_log.workout.get_absolute_url}}" class="btn">{{ first_log.workout }}</a>
            <a href="{% url 'workout-log' first_log.workout.id %}" class="btn">{% trans "Log overview" %}</a>
            {% if session %}
                <a href="{% url 'workout-session-edit' session.pk %}" class="btn modal-dialog">{% trans "Edit workout session" %}</a>
            {% else %}
                <a href="{% url 'workout-session-add' first_log.workout_id date|date:'Y' date|date:'m' date|date:'d' %}" class="btn modal-dialog">{% trans "Add workout impression" %}</a>
            {% endif %}
        </p>
        {% if session %}
        <p>
            <strong>{% trans "Time" %}:</strong>
                {% if session.time_start %}
                    {{session.time_start|time:"H:i"}} - {{session.time_end|time:"H:i"}}
                {% else %}
                    -/-
                {% endif %}<br>
            <strong>{% trans "General impression" %}: </strong> {% trans session.get_impression_display %}<br>
            <strong>{% trans "Notes" %}:</strong> {{session.notes|default:'-/-'}}
        </p>
        {% endif %}
    {% endwith %}
    {% endwith %}
    {% endwith %}
    <ul class="grouper-list">
    {% for i in grouped_logs %}
        <li>{{i.grouper}}
        <ul>
        {% for log in i.list %}
            <li>
                {{log.reps}} × {{log.weight}}
                <span class="editoptions">
                    <a href="{% url 'workout-log-delete' log.pk %}"
                       title="{% trans 'Delete' %}"
                       class="modal-dialog">
                           <img src="{% static 'images/icons/trash.svg' %}"
                                width="16"
                                height="16"
                                alt="{% trans 'Delete' %}">
                    </a>
                    <a href="{% url 'workout-log-edit' log.pk %}"
                       title="{% trans 'Edit' %}"
                       class="modal-dialog">
                           <img src="{% static 'images/icons/edit.svg' %}"
                                width="16"
                                height="16"
                                alt="{% trans 'Edit' %}">
                    </a>
                </span>
            </li>
        {% endfor %}
        </ul>
        </li>
    {% endfor %}
    </ul>
</div>
{% endfor %}
{% endblock %}



{% block sidebar %}
<div id="calendar-day-select-popup"
     style="display:none;"
     title="{% trans 'Add log to day' %}">
    <div>
        {% if current_workout %}
        <p>{% blocktrans %}Click to add weight logs to a training
day in your current workout:{% endblocktrans %} {{current_workout}}</p>

        {% for day in current_workout.day_set.all %}
            <a href="{% url 'day-log' day.pk %}" class="btn btn-block">{{day.description}}</a>
        {% endfor %}

        {% else %}
        <p><em>{% trans "No workouts found." %}</em></p>
        <p>
        <a href="{% url 'workout-add' %}" class="btn btn-block">
           <img src="{% static 'images/icons/add.svg' %}"
                width="22"
                height="22"
                alt="">
            {% trans "Add one now." %}
        </a>
        </p>
        {% endif %}
    </div>
</div>

<h3>{% trans 'Info' %}</h3>
<p>{% blocktrans %}The coloured days in the calendar have weight logs,
you can click on them to see the details.{% endblocktrans %}</p>

<h3>{% trans 'Log' %}</h3>
<p>
    <a href="{% url 'workout-calendar' %}">
        <img src="{% static 'images/icons/calendar.svg' %}"
             width="32"
             height="32"
             alt="{% trans 'Go to current month' %}">
    {% trans "Go to current month" %}</a>
</p>
<p>
    <a href="" id="calendar-day-select-link">
        <img src="{% static 'images/icons/add.svg' %}"
             width="32"
             height="32"
             alt="{% trans 'Add new log' %}">
    {% trans "Add new log" %}</a>
</p>
<div class="timeline-log">
{% for date in month_list %}
    {% ifchanged date.year %}
        <p><strong>{{ date|date:"Y" }}</strong></p>
    {% endifchanged %}
    {% if current_month = date.month and current_year = date.year %}
        <span>{{date|date:"F"}}</span><br>
    {% else %}
        <span><a href="{% url 'workout-calendar' date.year date.month %}">{{date|date:"F"}}</a></span><br>
    {% endif %}
{% endfor %}
</div>

{% endblock %}
