{% extends "fitcheck/base.html" %}

{% load i18n %}

{% block fitcheck_content %}
    <div class="fc-toolbar">
        <div class="fc-eyebrow">
            {% translate "Fitting standards live independently - assign them to doctrines from each doctrine's page." %}
        </div>
        <div class="ms-auto d-flex gap-2">
            <a class="btn btn-warning btn-sm" href="{% url 'fitcheck:stale_recheck_page' %}">
                <i class="fa-solid fa-rotate"></i> {% translate "Recheck Stale Submissions" %}
            </a>
            <a class="btn btn-primary btn-sm" href="{% url 'fitcheck:standard_import' %}">
                <i class="fa-solid fa-file-import"></i> {% translate "New Fitting (EFT Import)" %}
            </a>
        </div>
    </div>

    <div class="d-flex flex-wrap gap-1 align-items-center mb-2">
        <a class="fc-chip {% if not active_doctrine %}active{% endif %}"
           href="?{{ qs_no_doctrine }}">
            {% translate "All" %}
        </a>
        {% for d in pill_doctrines %}
            <a class="fc-chip {% if active_doctrine == d.pk|stringformat:'s' %}active{% endif %}"
               href="?{{ qs_no_doctrine }}&amp;doctrine={{ d.pk }}">
                {{ d.name }} ({{ d.fit_count }})
            </a>
        {% endfor %}
        <a class="fc-chip {% if active_doctrine == 'none' %}active{% endif %}"
           href="?{{ qs_no_doctrine }}&amp;doctrine=none">
            {% translate "Standalone" %}
        </a>
        {% if overflow_doctrines %}
            <div class="btn-group">
                <button type="button" class="fc-chip dropdown-toggle{% if overflow_active %} active{% endif %}"
                        {% if not overflow_active %}style="background:transparent"{% endif %}
                        data-bs-toggle="dropdown" aria-expanded="false">
                    {% translate "More…" %}
                </button>
                <ul class="dropdown-menu">
                    {% for d in overflow_doctrines %}
                        <li>
                            <a class="dropdown-item {% if active_doctrine == d.pk|stringformat:'s' %}active{% endif %}"
                               href="?{{ qs_no_doctrine }}&amp;doctrine={{ d.pk }}">
                                {{ d.name }} ({{ d.fit_count }})
                            </a>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        {% endif %}
    </div>

    <form method="get" class="d-flex flex-wrap gap-2 align-items-center mb-3">
        <select name="category" class="form-select form-select-sm w-auto" onchange="this.form.submit()">
            <option value="">{% translate "All categories" %}</option>
            {% for category in categories %}
                <option value="{{ category.pk }}" {% if active_category == category.pk|stringformat:'s' %}selected{% endif %}>
                    {{ category.name }}
                </option>
            {% endfor %}
        </select>
        <select name="group" class="form-select form-select-sm w-auto" onchange="this.form.submit()">
            <option value="">{% translate "All hull classes" %}</option>
            {% for group in ship_groups %}
                <option value="{{ group.id }}" {% if active_group == group.id|stringformat:'s' %}selected{% endif %}>
                    {{ group.name }}
                </option>
            {% endfor %}
        </select>
        <div class="input-group input-group-sm w-auto">
            <input type="search" name="q" class="form-control" value="{{ active_q }}"
                   placeholder="{% translate 'Search fitting names…' %}">
            <button type="submit" class="btn btn-primary">
                <i class="fa-solid fa-magnifying-glass"></i>
            </button>
        </div>
        {% if active_doctrine %}
            <input type="hidden" name="doctrine" value="{{ active_doctrine }}">
        {% endif %}
        {% if active_sort %}
            <input type="hidden" name="sort" value="{{ active_sort }}">
        {% endif %}
        {% if has_filters %}
            <a class="btn btn-sm btn-secondary" href="{% url 'fitcheck:standards_list' %}">
                {% translate "Clear filters" %}
            </a>
        {% endif %}
    </form>

    <div class="fc-finding-section fc-sec-LOW">
        <div class="fc-finding-section-head">
            <i class="fa-solid fa-screwdriver-wrench"></i>
            <span class="ms-1">{% translate "Fitting standards" %}</span>
        </div>
        <div class="card-body p-0">
            <table class="table table-hover align-middle mb-0 fc-table-fixed">
                <colgroup>
                    <col style="width: 18%"><col style="width: 12%"><col style="width: 20%"><col style="width: 12%"><col style="width: 6%"><col style="width: 9%"><col style="width: 9%"><col style="width: 5%"><col style="width: 9%">
                </colgroup>
                <thead>
                    <tr>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'name' %}-name{% else %}name{% endif %}">
                                {% translate "Fitting" %}
                                {% if active_sort == 'name' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-name' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'hull' %}-hull{% else %}hull{% endif %}">
                                {% translate "Hull" %}
                                {% if active_sort == 'hull' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-hull' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'doctrines' %}-doctrines{% else %}doctrines{% endif %}">
                                {% translate "Doctrines" %}
                                {% if active_sort == 'doctrines' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-doctrines' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>{% translate "Policy" %}</th>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'version' %}-version{% else %}version{% endif %}">
                                {% translate "Version" %}
                                {% if active_sort == 'version' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-version' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'created' %}-created{% else %}created{% endif %}">
                                {% translate "Created" %}
                                {% if active_sort == 'created' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-created' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>
                            <a class="text-body text-decoration-none" href="?{{ qs_no_sort }}&amp;sort={% if active_sort == 'updated' %}-updated{% else %}updated{% endif %}">
                                {% translate "Updated" %}
                                {% if active_sort == 'updated' %}<i class="fa-solid fa-caret-up"></i>
                                {% elif active_sort == '-updated' %}<i class="fa-solid fa-caret-down"></i>{% endif %}
                            </a>
                        </th>
                        <th>{% translate "Active" %}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for fit in page_obj %}
                        <tr>
                            <td>
                                <img class="fc-module-icon" width="28" height="28" alt=""
                                     src="https://images.evetech.net/types/{{ fit.ship_type_id }}/icon?size=32">
                                <a href="{% url 'fitcheck:fit_detail' fit.pk %}"><strong>{{ fit.name }}</strong></a>
                            </td>
                            <td class="text-muted">{{ fit.ship_type.name }}</td>
                            <td>
                                {% for doctrine in fit.doctrines.all %}
                                    <a class="fc-chip me-1" href="{% url 'fitcheck:doctrine_detail' doctrine.pk %}">{{ doctrine.name }}</a>
                                {% empty %}
                                    <span class="text-muted small">{% translate "standalone" %}</span>
                                {% endfor %}
                            </td>
                            <td class="small text-muted">
                                {% if fit.compliance_policy %}
                                    <i class="fa-solid fa-scale-balanced me-1"></i>{{ fit.compliance_policy.name }}
                                {% else %}
                                    {% translate "None applied yet" %}
                                {% endif %}
                            </td>
                            <td>{% translate "Fit" %} v{{ fit.version }}</td>
                            <td class="small text-muted text-nowrap">{{ fit.created_at|date:"Y-m-d" }}</td>
                            <td class="small text-muted text-nowrap">{{ fit.last_updated|date:"Y-m-d" }}</td>
                            <td>
                                {% if fit.is_active %}
                                    <i class="fa-solid fa-check text-success"></i>
                                {% else %}
                                    <i class="fa-solid fa-xmark text-danger"></i>
                                {% endif %}
                            </td>
                            <td class="text-end text-nowrap">
                                <a class="btn btn-sm btn-warning" title="{% translate 'Module policies and exceptions' %}"
                                   href="{% url 'fitcheck:manage_fit_items' fit.pk %}">
                                    <i class="fa-solid fa-sliders"></i>
                                </a>
                                <a class="btn btn-sm btn-warning" title="{% translate 'Settings' %}"
                                   href="{% url 'fitcheck:manage_fit_settings' fit.pk %}">
                                    <i class="fa-solid fa-gear"></i>
                                </a>
                                <form class="d-inline" method="post" action="{% url 'fitcheck:fit_delete' fit.pk %}"
                                      onsubmit="return confirm('{% translate "Delete this fitting and all its submissions?" %}')">
                                    {% csrf_token %}
                                    <button class="btn btn-sm btn-danger" type="submit" title="{% translate 'Delete' %}">
                                        <i class="fa-solid fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    {% empty %}
                        <tr>
                            <td colspan="9" class="p-4 text-center text-muted">
                                {% if has_filters %}
                                    {% translate "No fittings match these filters." %}
                                {% else %}
                                    {% translate "No fitting standards yet - import one from EFT." %}
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% include "fitcheck/partials/pagination.html" %}
{% endblock %}
