{% extends 'mastery/base.html' %}
{% load i18n %}
{% load humanize %}
{% load evelinks %}
{% load l10n %}

{% block details %}
<div class="container mt-4">
    <div class="d-flex flex-wrap justify-content-between align-items-start gap-2 mb-3">
        <div class="d-flex align-items-center gap-2">
            <img src="{{ summary.doctrine.icon_url }}" class="rounded" width="40" height="40" alt="{{ summary.doctrine.name }}">
            <div>
                <h2 class="mb-0 d-flex align-items-center gap-2 flex-wrap">
                    <span>{{ summary.doctrine.name }}</span>
                    {% include 'mastery/partials/_priority_badge.html' with priority=summary.priority %}
                </h2>
                <div class="text-muted">{% blocktranslate with flyable=summary.kpis.flyable_now_users total=summary.kpis.users_total %}{{ flyable }}/{{ total }} players can fly at least one fit now{% endblocktranslate %}</div>
            </div>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-sm btn-outline-secondary" href="{% url 'mastery:summary_list' %}?group_id={{ selected_group.id }}&activity_days={{ activity_days }}{% if include_inactive %}&include_inactive=1{% endif %}">{% translate "Back to summary" %}</a>
            <a class="btn btn-sm btn-outline-primary" href="{% url 'fittings:view_doctrine' summary.doctrine.id %}">{% translate "View doctrine" %}</a>
            {% if perms.mastery.manage_summary_groups %}
            <a class="btn btn-sm btn-outline-secondary" href="{% url 'mastery:summary_settings' %}?group_id={{ selected_group.id }}">{% translate "Summary settings" %}</a>
            {% endif %}
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-3">
        <div class="card-body d-flex flex-wrap justify-content-between gap-2 align-items-center">
            <div class="small text-muted">{% translate "Audience group:" %} <strong>{{ selected_group.name }}</strong></div>
            <form method="get" class="d-flex flex-wrap align-items-center gap-2">
                {% if include_inactive %}<input type="hidden" name="include_inactive" value="1">{% endif %}
                <label class="small text-muted" for="summaryGroupSwitchDoctrine">{% translate "Switch group" %}</label>
                <select id="summaryGroupSwitchDoctrine" name="group_id" class="form-select form-select-sm" style="width: 240px;">
                    {% for group in summary_groups %}
                    <option value="{{ group.id }}" {% if selected_group.id == group.id %}selected{% endif %}>{{ group.name }}</option>
                    {% endfor %}
                </select>
                <label class="small text-muted" for="summaryDoctrineActivityDays">{% translate "Activity days" %}</label>
                <input id="summaryDoctrineActivityDays" class="form-control form-control-sm" type="number" min="1" max="90" name="activity_days" value="{{ activity_days }}" style="width: 96px;">
                <button class="btn btn-sm btn-outline-primary" type="submit">{% translate "Apply" %}</button>
            </form>
        </div>
    </div>

    {% for fit in summary.fittings %}
    <div class="card border-0 shadow-sm mb-3">
        <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">
            <div class="d-flex align-items-center gap-2">
                <img src="{{ fit.fitting.ship_type_type_id|type_render_url:32 }}" class="rounded" width="32" height="32" alt="{{ fit.fitting.ship_type.name }}">
                <div>
                    {% if fit.configured %}
                    <div class="d-flex align-items-center gap-2 flex-wrap">
                        <a class="fw-semibold text-body text-decoration-none mastery-fitting-link" href="{% url 'mastery:summary_fitting_detail' fit.fitting.id %}?group_id={{ selected_group.id }}&activity_days={{ activity_days }}{% if include_inactive %}&include_inactive=1{% endif %}">{{ fit.fitting.name }}</a>
                        {% include 'mastery/partials/_priority_badge.html' with priority=fit.priority %}
                    </div>
                    {% else %}
                    <div class="fw-semibold d-flex align-items-center gap-2 flex-wrap">
                        <span>{{ fit.fitting.name }}</span>
                        {% include 'mastery/partials/_priority_badge.html' with priority=fit.priority %}
                    </div>
                    {% endif %}
                    <div class="small text-muted">{{ fit.fitting.ship_type.name }}</div>
                </div>
                {% if fit.configured %}
                <div class="ms-3 ps-3 border-start">
                    <div class="small text-muted">{% translate "Avg recommended" %}</div>
                    <div class="small fw-semibold">{{ fit.kpis.recommended_avg_pct }}%</div>
                    <div class="progress mt-1" style="height: 4px; width: 80px;">
                        <div class="progress-bar bg-primary" role="progressbar" style="width: {{ fit.kpis.recommended_avg_pct|unlocalize }}%"></div>
                    </div>
                </div>
                {% endif %}
            </div>
            <div class="d-flex gap-2 align-items-center">
                {% include 'mastery/partials/_summary_fit_header_actions.html' with fit=fit selected_group=selected_group activity_days=activity_days include_inactive=include_inactive %}
            </div>
        </div>
        {% if fit.configured %}
        <div class="card-body">
            {% include 'mastery/partials/_summary_status_kpi_nav_cards.html' with fit=fit selected_group=selected_group activity_days=activity_days include_inactive=include_inactive %}
        </div>
        {% endif %}
    </div>
    {% endfor %}
</div>

{% include 'mastery/partials/_fitting_preview_modal.html' %}
{% endblock %}

<style>
    .mastery-fitting-link:hover {
        text-decoration: underline !important;
    }

    .kpi-nav-btn {
        border: 1px solid var(--bs-border-color-translucent, rgba(0, 0, 0, 0.14));
    }

    .kpi-nav-btn:hover {
        border-color: var(--bs-primary, #0d6efd);
        background-color: rgba(13, 110, 253, 0.08);
    }
</style>


