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

{% block content %}
<h2>{% trans "Tracked Contracts" %}</h2>

<div class="card mt-3">
  <div class="card-header d-flex justify-content-between align-items-center">
    <strong>{% trans "Tracked Contracts" %}</strong>
    <form method="get" class="d-flex gap-2 mb-0">
      {% if status == "red" %}
        <button type="button" class="btn btn-primary btn-sm text-nowrap" onclick="location.href='?{% if tc %}tc={{ tc|urlencode }}{% endif %}'">
          {% trans "Show All" %}
        </button>
      {% else %}
        <button type="button" class="btn btn-danger btn-sm text-nowrap" onclick="location.href='?status=red{% if tc %}&tc={{ tc|urlencode }}{% endif %}'">
          {% trans "RED" %}
        </button>
      {% endif %}
      {% if status == "yellow" %}
        <button type="button" class="btn btn-primary btn-sm text-nowrap" onclick="location.href='?{% if tc %}tc={{ tc|urlencode }}{% endif %}'" >
          {% trans "Show All" %}
        </button>
      {% else %}
        <button type="button" class="btn btn-warning btn-sm text-nowrap" onclick="location.href='?status=yellow{% if tc %}&tc={{ tc|urlencode }}{% endif %}'">
          {% trans "YELLOW" %}
        </button>
      {% endif %}
      <input type="text" name="tc" value="{{ tc }}" class="form-control form-control-sm" placeholder="{% trans 'Search by ship/fit/title…' %}">
      <button class="btn btn-primary btn-sm text-nowrap" type="submit">
        {% trans "Search" %}
      </button>
    </form>
  </div>
  <div class="card-body p-0">
    <div class="table-responsive">
<table class="table table-striped">
  <thead>
    <tr>
      <th>{% trans "Icon" %}</th>
      <th>{% trans "Name" %}</th>
      <th>{% trans "Quantity (current / desired)" %}</th>
      <th>{% trans "Minimum Price" %}</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {% for r in rows %}
      <tr class="{% if r.status == 'RED' %}table-danger{% elif r.status == 'YELLOW' %}table-warning{% endif %}">
        <td>
          {% if r.icon_type_id %}
            <img src="https://images.evetech.net/types/{{ r.icon_type_id }}/icon?size=32" alt="">
          {% else %}
            <span class="text-muted">—</span>
          {% endif %}
        </td>
        <td>{{ r.name }}</td>
        <td>{{ r.current_qty }} / {{ r.desired_qty|default:"—" }}</td>
        <td>
          {% if r.min_price %}
            {{ r.min_price|floatformat:2|intcomma }} ISK
          {% else %}
            <span class="text-muted">{% trans "No data" %}</span>
          {% endif %}
        </td>
        <td class="text-end">
          <!--
          <form method="get"
            action="{% url 'markettracker:create_contract_delivery' r.tc.id %}"
            class="d-inline">
          <button type="submit" class="btn btn-sm btn-success">
            {% trans "Deliver" %}
          </button>
          </form>
          -->
        </td>
      </tr>
    {% empty %}
      <tr>
        <td colspan="5" class="text-center text-muted">{% trans "No tracked contracts." %}</td>
      </tr>
    {% endfor %}
  </tbody>
      </table>
    </div>
  </div>
</div>
{% endblock %}
