{% extends "core/base.jinja" %}
{% from 'core/macros.jinja' import user_profile_link %}

{# This page uses a custom macro instead of the core `paginate_jinja` and `paginate_alpine`
because it works with a somewhat dynamic form,
but was written before Alpine was introduced in the project.
TODO : rewrite the pagination used in this template an Alpine one
#}
{% macro paginate(page_obj, paginator) %}
  {% set js = "formPagination(this)" %}
  {% if page_obj.has_previous() or page_obj.has_next() %}
    {% if page_obj.has_previous() %}
      <a type="submit" onclick="{{ js }}" href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a>
    {% else %}
      <span class="disabled">{% trans %}Previous{% endtrans %}</span>
    {% endif %}
    {% for i in paginator.page_range %}
      {% if page_obj.number == i %}
        <span class="active">{{ i }} <span class="sr-only">({% trans %}current{% endtrans %})</span></span>
      {% else %}
        <a type="submit" onclick="{{ js }}" href="?page={{ i }}">{{ i }}</a>
      {% endif %}
    {% endfor %}
    {% if page_obj.has_next() %}
      <a type="submit" onclick="{{ js }}" href="?page={{ page_obj.next_page_number() }}">{% trans %}Next{% endtrans %}</a>
    {% else %}
      <span class="disabled">{% trans %}Next{% endtrans %}</span>
    {% endif %}
  {% endif %}
{% endmacro %}

{% block content %}
  <h3>{% trans %}Sales{% endtrans %}</h3>
  <form id="form" action="?page=1" method="post">
    {% csrf_token %}
    {{ form }}
    <p><input type="submit" value="{% trans %}Show{% endtrans %}" /></p>
    <p><input type="submit" value="{% trans %}Download as CSV{% endtrans %}" formaction="{{ url('club:sellings_csv', club_id=object.id) }}"/></p>
  </form>
  <p>
    {% trans %}Quantity: {% endtrans %}{{ total_quantity }} {% trans %}units{% endtrans %}<br/>
    {% trans %}Total: {% endtrans %}{{ total }} €<br/>
    {% trans %}Benefit: {% endtrans %}{{ benefit }} €
  </p>
  <table>
    <thead>
      <tr>
        <td>{% trans %}Date{% endtrans %}</td>
        <td>{% trans %}Counter{% endtrans %}</td>
        <td>{% trans %}Barman{% endtrans %}</td>
        <td>{% trans %}Customer{% endtrans %}</td>
        <td>{% trans %}Label{% endtrans %}</td>
        <td>{% trans %}Quantity{% endtrans %}</td>
        <td>{% trans %}Total{% endtrans %}</td>
        <td>{% trans %}Payment method{% endtrans %}</td>
      </tr>
    </thead>
    <tbody>
      {% for s in paginated_result %}
        <tr>
          <td>{{ s.date|localtime|date(DATETIME_FORMAT) }} {{ s.date|localtime|time(DATETIME_FORMAT) }}</td>
          <td>{{ s.counter }}</td>
          {% if s.seller %}
            <td><a href="{{ s.seller.get_absolute_url() }}">{{ s.seller.get_display_name() }}</a></td>
          {% else %}
            <td></td>
          {% endif %}
          {% if s.customer %}
            <td><a href="{{ s.customer.user.get_absolute_url() }}">{{ s.customer.user.get_display_name() }}</a></td>
          {% else %}
            <td></td>
          {% endif %}
          <td>{{ s.label }}</td>
          <td>{{ s.quantity }}</td>
          <td>{{ s.quantity * s.unit_price }} €</td>
          <td>{{ s.get_payment_method_display() }}</td>
          {% if s.is_owned_by(user) %}
            <td><a href="{{ url('counter:selling_delete', selling_id=s.id) }}">{% trans %}Delete{% endtrans %}</a></td>
          {% endif %}
        </tr>
      {% endfor %}
    </tbody>
  </table>
  {{ paginate(paginated_result, paginator) }}
{% endblock %}

{% block script %}
  <script type="text/javascript">
    function formPagination(link){
      const form = document.getElementById("form")
      form.action = link.href;
      link.href = "javascript:void(0)"; // block link action
      form.submit();
    }
  </script>
{% endblock %}



