{% load main_extras %}
{% load history_extras %}
{% load transaction_extras %}
{% load i18n %}
<div class="calendar">
  <table>
    <thead>
      <tr>
        {% if not year %}
          <th scope="col">{% translate "Year" %}</th>
        {% endif %}
        {% calendar_head %}
        <th scope="col">{% translate "Total" %}</th>
      </tr>
    </thead>
    <tbody>
      {% regroup history.data by month.year as years_list %}
      {% for y, y_data in years_list reversed %}
        <tr>
          {% if not year %}
            <th class="date" scope="row">
              <a href="{% history_url year=y account=account.id category=category.id %}">{{ y }}</a>
            </th>
          {% endif %}
          {% for m in y_data %}
            {% if forloop.parentloop.last and forloop.first %}
            {% empty_calendar_cells_start m.month.month %}
            {% endif %}
            {% if m %}
              <td class="month {% if m.sum > 0 %}p{% else %}m{% endif %}"
                  style="--opacity: {% calendar_opacity m.sum history.max.sum %}"
                  title="{{ m.sum|pmrvalue }}">{% up_down_icon m.sum %}</td>
            {% else %}
              <td class="month"></td>
            {% endif %}
            {% if forloop.parentloop.first and forloop.last %}
            {% empty_calendar_cells_end m.month.month %}
            {% endif %}
          {% endfor %}
          <td class="total">{{ y_data|sum_year|pmrvalue }}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
<div class="history plot">
  <table class="full-width">
    <colgroup>
      <col class="icon">
      <col class="desc">
      <col class="value">
      <col span="2" class="bar">
      <col class="value">
    </colgroup>
    <thead>
      <tr>
        <th scope="col">{{ "expand-up-down"|remix }}</th>
        <th scope="col">{% translate "Month" %}</th>
        <th scope="col" colspan="2">{% translate "Expenses" %}</th>
        <th scope="col" colspan="2">{% translate "Income" %}</th>
      </tr>
    </thead>
    <tbody>
      {% for date in history.data reversed %}
        {% ifchanged %}
          {% if date.sum_m or date.sum_p %}
            <tr {% if not date.month.month|divisibleby:"2" %}class="even"{% endif %}>
              <td class="icon">{% up_down_icon date.sum %}</td>
              <th class="date" scope="row">
                <a href="{% history_url year=date.month.year month=date.month.month account=account.id category=category.id %}">
                  {% if year %}
                    {{ date.month|date:"F"|capfirst }}
                  {% else %}
                    {{ date.month|date:"Y-m" }}
                  {% endif %}
                </a>
              </th>
              <td class="value">{{ date.sum_m|pmrvalue }}</td>
              <td class="bar m">{% plot_bar date.sum date.sum_m history.max.pm %}</td>
              <td class="bar p">{% plot_bar date.sum date.sum_p history.max.pm %}</td>
              <td class="value">{{ date.sum_p|pmrvalue }}</td>
            </tr>
          {% else %}
            <tr class="empty">
              <td colspan="6" class="empty"></td>
            </tr>
          {% endif %}
        {% endifchanged %}
      {% endfor %}
    </tbody>
  </table>
</div>
