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

{% block content %}
<h2>{% trans "All Deliveries" %}</h2>

<div class="card mt-3">
<div class="card-header d-flex justify-content-between align-items-center">
<strong>{% trans "Items" %}</strong>
<form method="get" class="d-flex gap-2 mb-0">
<input type="text" name="q" value="{{ q }}" class="form-control form-control-sm" placeholder="{% trans 'Search item...' %}">
      <button class="btn btn-primary btn-sm" type="submit">{% trans "Search" %}</button>
      </form>
</div>
<div class="card-body p-0">
<div class="table-responsive table-scroll-1">
<table class="table table-striped table-scroll-1">
  <thead>
    <tr>
      <th>{% trans "User" %}</th>
      <th>{% trans "Item" %}</th>
      <th>{% trans "Delivered" %}</th>
      <th>{% trans "Declared" %}</th>
      <th>{% trans "Status" %}</th>
      <th>{% trans "Actions" %}</th>
    </tr>
  </thead>
  <tbody>
    {% for d in item_deliveries %}
    <tr class="{% if d.is_finished %}table-success{% else %}table-info{% endif %}">
      <td>{{ d.user.profile.main_character.character_name|default:d.user.username }}</td>
      <td><img src="https://images.evetech.net/types/{{ d.item.id }}/icon?size=32"> {{ d.item.name }}</td>
      <td>{{ d.delivered_quantity }}</td>
      <td>{{ d.declared_quantity }}</td>
      <td>{{ d.get_status_display }}</td>
      <td>
        <a href="{% url 'markettracker:delete_delivery' d.pk %}" class="btn btn-danger btn-sm">{% trans "Delete" %}</a>
        {% if d.status == "PENDING" %}
        <a href="{% url 'markettracker:finish_delivery' d.pk %}" class="btn btn-success btn-sm">{% trans "Finish" %}</a>
        {% endif %}
      </td>
    </tr>
    {% empty %}
    <tr><td colspan="6" class="text-muted">{% trans "No item deliveries." %}</td></tr>
    {% endfor %}
  </tbody>
</table>
</div>
</div>
</div>
<div class="card mt-3">
<div class="card-header d-flex justify-content-between align-items-center">
<strong>{% trans "Contracts" %}</strong>
<form method="get" class="d-flex gap-2 mb-0">
<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-scroll-1">
<table class="table table-striped table-scroll-1">
  <thead>
    <tr>
      <th>{% trans "User" %}</th>
      <th>{% trans "Tracked" %}</th>
      <th>{% trans "Delivered" %}</th>
      <th>{% trans "Declared" %}</th>
      <th>{% trans "Status" %}</th>
      <th>{% trans "Actions" %}</th>
    </tr>
  </thead>
  <tbody>
    {% for d in contract_deliveries %}
    <tr class="{% if d.is_finished %}table-success{% else %}table-info{% endif %}">
      <td>{{ d.user.profile.main_character.character_name|default:d.user.username }}</td>
      <td>
        {% if d.tracked_contract.mode == "doctrine" and d.tracked_contract.fitting %}
          {{ d.tracked_contract.fitting.name }}
        {% else %}
          {{ d.tracked_contract.title_filter|default:"—" }}
        {% endif %}
      </td>
      <td>{{ d.delivered_quantity }}</td>
      <td>{{ d.declared_quantity }}</td>
      <td>{{ d.get_status_display }}</td>
      <td>
        <a href="{% url 'markettracker:delete_contract_delivery' d.pk %}"
          class="btn btn-danger btn-sm">{% trans "Delete" %}</a>
        {% if d.status == "PENDING" %}
          <a href="{% url 'markettracker:finish_contract_delivery' d.pk %}"
             class="btn btn-success btn-sm">{% trans "Finish" %}</a>
        {% endif %}
      </td>
    </tr>
    {% empty %}
    <tr><td colspan="6" class="text-muted">{% trans "No contract deliveries." %}</td></tr>
    {% endfor %}
  </tbody>
</table>
</div>
</div>
</div>
{% endblock %}
