{% extends "rattingtax/base.html" %}
{% load i18n humanize %}

{% block details %}
<div class="card mt-3">
  <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">

    <div>
      <strong>{% trans "Ratting Tax Summary" %}</strong>

      <div class="text-muted small mt-1">
        {% trans "Default tax rate" %}: <strong>{{ default_tax_rate }}%</strong>
        &nbsp;|&nbsp;
        {% trans "Flat tax reduction" %}: <strong>{{ flat_tax_reduction }} ISK</strong>
        &nbsp;|&nbsp;
        {% if show_all %}
          <strong>{% trans "Viewing: all corporations" %}</strong>
        {% else %}
          <strong>{% trans "Viewing: my corporations" %}</strong>
        {% endif %}
      </div>
    </div>

    <div class="d-flex gap-2 align-items-center flex-wrap">

      <!-- FILTER -->
      <form method="get" class="d-flex gap-2 mb-0">

        <select name="month" class="form-select form-select-sm text-nowrap">
          {% for opt in month_options %}
            <option value="{{ opt.value }}" {% if selected_month == opt.value %}selected{% endif %}>
              {{ opt.label }}
            </option>
          {% endfor %}
        </select>

        <select name="year" class="form-select form-select-sm text-nowrap">
          {% for y in years %}
            <option value="{{ y }}" {% if selected_year == y %}selected{% endif %}>{{ y }}</option>
          {% endfor %}
        </select>

        {% if can_view_all and show_all %}
          <input type="hidden" name="show_all" value="1">
        {% endif %}

        <button class="btn btn-primary btn-sm text-nowrap" type="submit">
          {% trans "Apply" %}
        </button>
      </form>

      <!-- TOGGLE -->
      {% if can_view_all %}
        {% if show_all %}
          <a href="{% url 'rattingtax:dashboard' %}?month={{ selected_month }}&year={{ selected_year }}"
             class="btn btn-secondary btn-sm text-nowrap">
            {% trans "Show my corporations" %}
          </a>
        {% else %}
          <a href="{% url 'rattingtax:dashboard' %}?month={{ selected_month }}&year={{ selected_year }}&show_all=1"
             class="btn btn-warning btn-sm text-nowrap">
            {% trans "Show all corporations" %}
          </a>
        {% endif %}
      {% endif %}

    </div>
  </div>

  <div class="card-body">
    {% if rows %}
      <div class="table-responsive">
        <table class="table table-sm align-middle">
          <thead>
            <tr>
              <th>{% trans "Corporation" %}</th>
              <th class="text-end">{% trans "Month total" %}</th>
              <th class="text-end">{% trans "Configured tax rate" %}</th>
              <th class="text-end">{% trans "Tax reduction" %}</th>
              <th class="text-end">{% trans "Final tax due" %}</th>
            </tr>
          </thead>
          <tbody>
            {% for r in rows %}
              <tr>
                <td class="text-nowrap">
                  {% if r.corp.logo_url %}
                    <img src="{{ r.corp.logo_url }}" style="height:20px;width:20px;margin-right:6px;">
                  {% endif %}

                  {{ r.corp.name }}{% if r.corp.ticker %} [{{ r.corp.ticker }}]{% endif %}

                  {% if r.uses_override %}
                    <span class="badge bg-info text-dark ms-1">override</span>
                  {% endif %}

                  {% if not r.has_stat %}
                    <span class="badge bg-secondary ms-1">no data</span>
                  {% endif %}
                </td>

                <td class="text-end">{{ r.month_total }} ISK</td>
                <td class="text-end">{{ r.effective_rate }}%</td>
                <td class="text-end">{{ r.flat_tax_reduction }} ISK</td>
                <td class="text-end"><strong>{{ r.final_tax }} ISK</strong></td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    {% else %}
      <p class="text-muted">{% trans "No data available." %}</p>
    {% endif %}
  </div>
</div>
{% endblock %}