{% extends "core/base.jinja" %}

{% macro monthly(objects) %}
  <div class="accordion-content">
    <table>
      <thead>
        <tr>
          <td>{% trans %}Year{% endtrans %}</td>
          <td>{% trans %}Month{% endtrans %}</td>
          <td>{% trans %}Total{% endtrans %}</td>
        </tr>
      </thead>
      <tbody>
        {% for object in objects %}
          {% set link=url(
          'core:user_account_detail',
          user_id=profile.id,
          year=object['grouped_date'].year,
          month=object['grouped_date'].month
          ) %}
          <tr>
            <td><a href="{{ link }}">{{ object["grouped_date"]|date("Y") }}</a></td>
            <td><a href="{{ link }}">{{ object["grouped_date"]|date("E") }}</a></td>
            <td><a href="{{ link }}">{{ "%.2f"|format(object["total"]) }} €</a></td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% block title %}
  {% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %}
{% endblock %}

{% block content %}
  {% if customer %}
    <h3>{% trans %}User account{% endtrans %}</h3>
    <p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p>
    {% if buyings_month %}
      <details class="accordion" name="account" open>
        <summary>{% trans %}Account purchases{% endtrans %}</summary>
        {{ monthly(buyings_month) }}
      </details>
    {% endif %}
    {% if refilling_month %}
      <details class="accordion" name="account">
        <summary>{% trans %}Reloads{% endtrans %}</summary>
        {{ monthly(refilling_month) }}
      </details>
    {% endif %}
    {% if invoices_month %}
      <details class="accordion" name="account">
        <summary>{% trans %}Eboutic invoices{% endtrans %}</summary>
        {{ monthly(invoices_month) }}
      </details>
    {% endif %}
    {% if etickets %}
      <details class="accordion" name="account">
        <summary>{% trans %}Etickets{% endtrans %}</summary>
        <div class="accordion-content">
          <ul>
            {% for s in etickets %}
              <li>
                <a href="{{ url('counter:eticket_pdf', selling_id=s.id) }}">
                  {{ s.quantity }} x {{ s.product.eticket }}
                </a>
              </li>
            {% endfor %}
          </ul>
        </div>
      </details>
    {% endif %}
  {% else %}
    <p>{% trans %}User has no account{% endtrans %}</p>
  {% endif %}
{% endblock %}
