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

{% block details %}
<div class="card mt-3">
  <div class="card-header">
    <strong>{% trans "Tax Management" %}</strong>
    <div class="text-muted small mt-1">
      {% trans "Manage global defaults and per-corporation tax overrides." %}
    </div>
  </div>

  <div class="card-body">
    <form method="post">
      {% csrf_token %}

      <div class="row g-3 mb-4">
        <div class="col-md-4">
          <label for="id_default_tax_rate" class="form-label">
            {% trans "Default tax rate (%)" %}
          </label>
          <input
            type="number"
            step="0.01"
            min="0"
            max="100"
            class="form-control"
            id="id_default_tax_rate"
            name="default_tax_rate_percent"
            value="{{ default_tax_rate }}"
          >
          <div class="form-text">
            {% trans "Used when a corporation does not have its own override." %}
          </div>
        </div>

        <div class="col-md-4">
          <label for="id_flat_tax_reduction" class="form-label">
            {% trans "Flat tax reduction (ISK)" %}
          </label>
          <input
            type="number"
            step="1"
            min="0"
            class="form-control"
            id="id_flat_tax_reduction"
            name="flat_tax_reduction"
            value="{{ flat_tax_reduction_raw }}"
          >
          <div class="form-text">
            {% trans "Subtracted from the calculated tax for every corporation." %}
          </div>
        </div>

        <div class="col-md-4 d-flex align-items-end">
          <button type="submit" class="btn btn-primary">
            {% trans "Save settings" %}
          </button>
        </div>
      </div>

      <div class="table-responsive">
        <table class="table table-sm align-middle">
          <thead>
            <tr>
              <th>{% trans "Corporation" %}</th>
              <th class="text-end">{% trans "Override rate (%)" %}</th>
              <th class="text-end">{% trans "Effective rate (%)" %}</th>
              <th class="text-end">{% trans "In-game corp tax (%)" %}</th>
              <th>{% trans "Mode" %}</th>
            </tr>
          </thead>
          <tbody>
            {% for r in rows %}
              <tr>
                <td class="text-nowrap">
                  {% if r.corp.logo_url %}
                    <img src="{{ r.corp.logo_url }}" alt="" style="height:20px;width:20px;border-radius:3px;margin-right:6px;">
                  {% endif %}
                  {{ r.corp.name }}{% if r.corp.ticker %} [{{ r.corp.ticker }}]{% endif %}
                </td>

                <td class="text-end" style="max-width: 160px;">
                  <input
                    type="number"
                    step="0.01"
                    min="0"
                    max="100"
                    class="form-control form-control-sm text-end"
                    name="corp_rate_{{ r.corp.id }}"
                    value="{{ r.override_rate }}"
                    placeholder="{% trans 'Use global' %}"
                  >
                </td>

                <td class="text-end">{{ r.effective_rate }}%</td>
                <td class="text-end">
                  {% if r.ingame_tax_rate %}
                    {{ r.ingame_tax_rate }}%
                  {% else %}
                    <span class="text-muted">—</span>
                  {% endif %}
                </td>
                <td>
                  {% if r.uses_override %}
                    <span class="badge bg-info text-dark">{% trans "Per-corp override" %}</span>
                  {% else %}
                    <span class="badge bg-secondary">{% trans "Global default" %}</span>
                  {% endif %}
                </td>
              </tr>
            {% empty %}
              <tr>
                <td colspan="5" class="text-center text-muted py-4">
                  {% trans "No corporations found." %}
                </td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>

      <div class="mt-3 text-muted small">
        {% trans "Leave an override field empty to use the global default tax rate for that corporation." %}
      </div>
    </form>
  </div>
</div>
{% endblock %}