{% extends "base.html" %}
{% load translations %}
{% load icons %}
{% load crispy_forms_tags %}
{% load i18n %}

{% block breadcrumbs %}
<li><a href="{% url 'profile' %}">{% trans "Your profile" %}</a></li>
<li><a href="{% url 'billing' %}">{% trans "Billing" %}</a></li>
{% endblock %}

{% block content %}

<div class="row">

<div class="col-lg-6" id="billing-{{ billing.pk }}">
<div class="panel {% if not billing.is_active %}panel-default{% elif not billing.paid or not billing.in_limits %}panel-danger{% else %}panel-default{% endif %}">
  <div class="panel-heading">
  <h4 class="panel-title">
    <a class="pull-right flip doc-link" href="https://weblate.org/hosting/">{% icon "info.svg" %}</a>
    {% trans "Billing plan" %}
    {% if billing.payment.recurring %}
    <span class="badge">{% trans "Recurring payment" %}</span>
    {% endif %}
    {% if not billing.is_active %}
    <span class="badge">{% trans "Expired" %}</span>
    {% elif not billing.paid %}
    <span class="badge">{% trans "Past due date" %}</span>
    {% elif not billing.in_limits %}
    <span class="badge">{% trans "Exceeds limits" %}</span>
    {% endif %}
  </h4>
  </div>
  {% include "billing/status.html" %}
{% if billing.is_active %}
  <div class="panel-footer">
  <form method="post">
  {% csrf_token %}
  {% if payment_enabled and billing.can_be_paid %}
    <a href="{% url 'create-billing' %}?billing={{ billing.pk }}&amp;upgrade=1" class="btn btn-primary">
    {% trans "Change plan or payment method" %}
    </a>
  {% endif %}
  {% if billing.payment.recurring %}
  <button type="submit" name="recurring" value="{{ billing.pk }}" class="btn btn-warning">{% trans "Terminate recurring payment" %}</button>
  {% endif %}
  <button type="submit" name="terminate" value="{{ billing.pk }}" class="btn btn-danger"
  {% if billing.all_projects %}
  title="{% trans "To terminate billing you have to remove all projects first." %}"
  disabled
  {% endif %}
  >{% trans "Terminate billing plan" %}</button>
  </form>
  </div>
{% endif %}
</div>
</div>

<div class="col-lg-6">
{% with invoices=billing.invoice_set.order %}
{% if invoices %}
<div class="panel panel-default">
  <div class="panel-heading"><h4 class="panel-title">{% trans "Invoices" %}</h4></div>
<table class="table table-striped">
<thead>
<tr>
<th>{% trans "Invoice period" %}</th>
<th>{% trans "Invoice amount" %}</th>
<th>{% trans "Download invoice" %}</th>
</tr>
</thead>
<tbody>
{% for invoice in billing.invoice_set.order %}
<tr>
<td>{{ invoice.start|date:"SHORT_DATE_FORMAT" }} - {{ invoice.end|date:"SHORT_DATE_FORMAT" }}</td>
<td>{{ invoice.amount }} {{ invoice.get_currency_display }}</td>
<td>
{% if invoice.ref and invoice.filename_valid %}
<a href="{% url 'invoice-download' pk=invoice.pk %}">{{ invoice.filename }}</a>
{% elif invoice.ref %}
{{ invoice.filename }}
{% else %}
{% trans "Not available" %}
{% endif %}
</td>
</tr>
{% empty %}
<tr><td colspan="3">{% trans "No invoices found." %}</td/></tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
{% endwith %}

{% if offer_hosting and billing.plan.price == 0 %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        {% trans "Libre hosting" %}
        {% if billing.is_libre_trial and billing.payment.libre_request %}
          <span class="badge">{% trans "Pending approval" %}</span>
        {% endif %}
      </h4>
    </div>
    <div class="panel-body">
      {% include "snippets/libre-checklist.html" %}
    </div>
    {% if billing.is_libre_trial %}
      <div class="panel-footer">
        <form method="POST">
          {% csrf_token %}
          {% if billing.valid_libre %}
            {% if user.is_superuser or not billing.payment.libre_request %}
              <p>{% trans "Please check that following conditions are met:" %}<p>
              <ul>
                {% include "snippets/libre-basic.html" %}
              </ul>
              {% if user.is_superuser %}
                <input type="submit" class="btn btn-primary" name="approve" value="{% trans "Approve as libre" %}" />
              {% else %}
                {{ hosting_form|crispy }}
                <input type="submit" class="btn btn-primary" name="request" value="{% trans "Request approval for libre hosting" %}" />
              {% endif %}
            {% endif %}
          {% else %}
            <p>{% trans "This subscription is not eligible to the libre plan, please fix the errors above or purchase a subscription." %}</p>
          {% endif %}
        </form>
      </div>
    {% endif %}
  </div>
{% endif %}

</div>

</div>
{% endblock %}
