{% extends 'buybackprogram/page.html' %}
{% load i18n %}
{% load humanize %}
{% load django_bootstrap5 %}
{% load static %}

{% block body %}

<div class="card mb-3">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="fas fa-percentage me-1"></i> {% trans "Special Item Taxes" %}</span>
        {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
        <div class="d-flex gap-2">
            <a class="btn btn-sm btn-outline-primary" href="{% url 'buybackprogram:program_edit_item' program_pk=program.id %}">
                <i class="fas fa-plus me-1"></i>{% trans "Add Item" %}
            </a>
            <a class="btn btn-sm btn-outline-primary" href="{% url 'buybackprogram:program_edit_marketgroup' program_pk=program.id %}">
                <i class="fas fa-plus me-1"></i>{% trans "Add Market Group" %}
            </a>
            <a class="btn btn-sm btn-outline-danger" href="{% url 'buybackprogram:program_item_remove_all' program_pk=program.id %}">
                <i class="fas fa-trash me-1"></i>{% trans "Delete All" %}
            </a>
        </div>
        {% endif %}
    </div>
    <div class="card-body pb-1">
        <p class="text-muted small mb-2">{% trans "Special item taxes applied to this program. Values may be additional taxes or discounts." %}</p>
    </div>
    <div class="table-responsive">
        <table class="table table-striped table-hover mb-0">
            <thead>
                <tr>
                    <th class="ps-3">{% trans "Item" %}</th>
                    <th class="text-center">{% trans "Tax Adjustment" %}</th>
                    <th class="text-center">{% trans "Total Tax" %}</th>
                    <th class="text-center">{% trans "Allowed" %}</th>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <th class="text-center">{% trans "Actions" %}</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for item in program_items %}
                <tr>
                    <td class="ps-3">
                        {% if item.item_type %}
                            <span class="badge bg-info me-1">{% trans "Item" %}</span>
                            {{ item.item_type.name }}
                        {% else %}
                            <span class="badge bg-warning text-dark me-1">{% trans "Market Group" %}</span>
                            {{ item.market_group.name }}
                        {% endif %}
                    </td>
                    <td class="text-center">
                        <span class="badge {% if item.item_tax > 0 %}bg-warning text-dark{% elif item.item_tax < 0 %}bg-success{% else %}bg-secondary{% endif %}">
                            {% if item.item_tax > 0 %}+{% endif %}{{ item.item_tax }}%
                        </span>
                    </td>
                    <td class="text-center">
                        <span class="badge bg-light text-dark border">{{ item.item_tax|add:program.tax }}%</span>
                    </td>
                    <td class="text-center">
                        {% if item.disallow_item %}
                            <span class="badge bg-danger"><i class="fas fa-times me-1"></i>{% trans "No" %}</span>
                        {% else %}
                            <span class="badge bg-success"><i class="fas fa-check me-1"></i>{% trans "Yes" %}</span>
                        {% endif %}
                    </td>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <td class="text-center">
                        <a class="btn btn-sm btn-outline-danger"
                           href="{% url 'buybackprogram:program_item_remove' item_pk=item.pk program_pk=program.id %}">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                    {% endif %}
                </tr>
                {% empty %}
                <tr>
                    <td colspan="5" class="text-center text-muted py-3">{% trans "No special item taxes configured." %}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<div class="card mb-3">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="fas fa-tag me-1"></i> {% trans "Static Prices" %}</span>
        {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
        <a class="btn btn-sm btn-outline-primary" href="{% url 'buybackprogram:program_edit_static_price' program_pk=program.id %}">
            <i class="fas fa-plus me-1"></i>{% trans "Add Static Price" %}
        </a>
        {% endif %}
    </div>
    <div class="card-body pb-1">
        <p class="text-muted small mb-2">{% trans "Items with a fixed ISK buy price. These completely override market data for this program." %}</p>
    </div>
    <div class="table-responsive">
        <table class="table table-striped table-hover mb-0">
            <thead>
                <tr>
                    <th class="ps-3">{% trans "Item" %}</th>
                    <th class="text-center">{% trans "Static Price (ISK)" %}</th>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <th class="text-center">{% trans "Actions" %}</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for price_item in static_prices %}
                <tr>
                    <td class="ps-3">{{ price_item.eve_type.name }}</td>
                    <td class="text-center"><code>{{ price_item.price|intcomma }} ISK</code></td>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <td class="text-center">
                        <a class="btn btn-sm btn-outline-danger"
                           href="{% url 'buybackprogram:remove_static_price' price_pk=price_item.id program_pk=program.id %}">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                    {% endif %}
                </tr>
                {% empty %}
                <tr>
                    <td colspan="{% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}3{% else %}2{% endif %}"
                        class="text-center text-muted py-3">
                        {% trans "No static prices configured." %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<div class="card mb-3">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="fas fa-search me-1"></i> {% trans "Manual Review Watchlist" %}</span>
        {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
        <a class="btn btn-sm btn-outline-primary" href="{% url 'buybackprogram:program_edit_watchlist' program_pk=program.id %}">
            <i class="fas fa-plus me-1"></i>{% trans "Add to Watchlist" %}
        </a>
        {% endif %}
    </div>
    <div class="card-body pb-1">
        <p class="text-muted small mb-2">{% trans "Items or market groups here trigger a manual review warning during appraisal." %}</p>
    </div>
    <div class="table-responsive">
        <table class="table table-striped table-hover mb-0">
            <thead>
                <tr>
                    <th class="ps-3">{% trans "Type" %}</th>
                    <th>{% trans "Name" %}</th>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <th class="text-center">{% trans "Actions" %}</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for wl in watchlist_items %}
                <tr>
                    <td class="ps-3">
                        {% if wl.item_type %}
                            <span class="badge bg-info">{% trans "Item" %}</span>
                        {% else %}
                            <span class="badge bg-warning text-dark">{% trans "Group" %}</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if wl.item_type %}{{ wl.item_type.name }}{% else %}{{ wl.eve_group.name }}{% endif %}
                    </td>
                    {% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}
                    <td class="text-center">
                        <a class="btn btn-sm btn-outline-danger"
                           href="{% url 'buybackprogram:remove_watchlist_item' watchlist_pk=wl.id program_pk=program.id %}">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                    {% endif %}
                </tr>
                {% empty %}
                <tr>
                    <td colspan="{% if perms.buybackprogram.manage_all_programs or perms.buybackprogram.manage_programs and program.owner.user == request.user %}3{% else %}2{% endif %}"
                        class="text-center text-muted py-3">
                        {% trans "No items or groups on the watchlist." %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

{% endblock %}

{% block extra_javascript %}
<script src="{% static 'buybackprogram/js/bootstrap-autocomplete.min.js' %}"></script>
<script src="{% static 'buybackprogram/js/autocomplete.js' %}"></script>
{% endblock %}

{% block extra_css %}
{% include "buybackprogram/bundles/buybackprogram-css-light.html" %}
{% endblock %}
