{% 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-3 mb-3">
        <div>
            <h2 class="mb-1">{% translate "Doctrine Coverage Summary" %}</h2>
            <div class="text-muted">{% translate "Global view for leadership and FCs" %}</div>
        </div>
        <div class="d-flex gap-2">
            <span class="badge bg-secondary">{% blocktranslate with count=member_count %}{{ count }} members{% endblocktranslate %}</span>
            <span class="badge bg-info text-dark">{% blocktranslate with count=active_character_count %}{{ count }} active characters{% endblocktranslate %}</span>
            {% if perms.mastery.manage_summary_groups %}
            <a class="btn btn-sm btn-outline-secondary" href="{% url 'mastery:summary_settings' %}">{% translate "Summary settings" %}</a>
            {% endif %}
        </div>
    </div>

    <form method="get" class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <div class="row g-3 align-items-end">
                <div class="col-12 col-lg-5">
                    <label class="form-label small text-muted" for="summarySearchInput">{% translate "Search doctrine / fitting / ship" %}</label>
                    <input id="summarySearchInput" class="form-control" type="text" name="q" value="{{ search_query }}" placeholder="Armor, Cerb, Muninn...">
                </div>
                <div class="col-12 col-sm-6 col-lg-2">
                    <label class="form-label small text-muted" for="summaryGroupInput">{% translate "Audience group" %}</label>
                    <select id="summaryGroupInput" class="form-select" name="group_id">
                        {% for group in summary_groups %}
                        <option value="{{ group.id }}" {% if selected_group and selected_group.id == group.id %}selected{% endif %}>{{ group.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-12 col-sm-6 col-lg-3">
                    <label class="form-label small text-muted" for="activityDaysInput">{% translate "Activity window (days)" %}</label>
                    <input id="activityDaysInput" class="form-control" type="number" min="1" max="90" name="activity_days" value="{{ activity_days }}">
                </div>
                <div class="col-12 col-sm-6 col-lg-2">
                    <div class="form-check mt-4">
                        <input class="form-check-input" type="checkbox" id="includeInactiveInput" name="include_inactive" value="1" {% if include_inactive %}checked{% endif %}>
                        <label class="form-check-label" for="includeInactiveInput">{% translate "Include inactive" %}</label>
                    </div>
                </div>
                <div class="col-12 col-lg-2 d-flex gap-2">
                    <button class="btn btn-primary w-100" type="submit">{% translate "Apply" %}</button>
                    <a class="btn btn-outline-secondary" href="{% url 'mastery:summary_list' %}">{% translate "Reset" %}</a>
                </div>
            </div>
        </div>
    </form>

    {% if not selected_group %}
    <div class="alert alert-warning">
        {% translate "No summary audience group is configured yet." %}
        {% if perms.mastery.manage_summary_groups %}
        <a href="{% url 'mastery:summary_settings' %}">{% translate "Create one in summary settings" %}</a>.
        {% endif %}
    </div>
    {% endif %}

    {% if selected_group %}
    <div class="card border-0 shadow-sm">
        <div class="card-header">{% translate "Doctrines" %}</div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead class="mastery-summary-thead">
                    <tr>
                        <th>{% translate "Doctrine" %}</th>
                        <th>{% translate "Flyable now (players)" %}</th>
                        {% include 'mastery/partials/_summary_status_headers.html' with include_tooltips=1 include_chars_suffix=0 %}
                        <th>{% translate "Avg recommended" %}</th>
                        <th>{% translate "Configured fits" %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for item in doctrine_summaries %}
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <img src="{{ item.doctrine.icon_url }}" class="rounded" width="32" height="32" alt="{{ item.doctrine.name }}">
                                <div>
                                    <div class="d-flex align-items-center gap-2 flex-wrap">
                                        <a class="fw-semibold text-body text-decoration-none mastery-doctrine-link" href="{% url 'mastery:summary_doctrine_detail' item.doctrine.id %}?group_id={{ selected_group.id }}&activity_days={{ activity_days }}{% if include_inactive %}&include_inactive=1{% endif %}">{{ item.doctrine.name }}</a>
                                        {% include 'mastery/partials/_priority_badge.html' with priority=item.priority %}
                                    </div>
                                    <div class="small text-muted">{% blocktranslate with count=item.active_characters_total %}{{ count }} active characters{% endblocktranslate %}</div>
                                </div>
                            </div>
                        </td>
                        <td>{{ item.kpis.flyable_now_users }}/{{ item.kpis.users_total }}</td>
                        <td class="text-success fw-semibold">{{ item.kpis.elite_characters }}</td>
                        <td class="text-primary fw-semibold">{{ item.kpis.almost_elite_characters }}</td>
                        <td class="text-info fw-semibold">{{ item.kpis.can_fly_characters }}</td>
                        <td class="text-warning fw-semibold">{{ item.kpis.almost_fit_characters }}</td>
                        <td class="text-danger fw-semibold">{{ item.kpis.needs_training_characters }}</td>
                        <td>
                            <div class="small fw-semibold mb-1">{{ item.kpis.recommended_avg_pct }}%</div>
                            <div class="progress" style="height: 8px; min-width: 120px;">
                                <div class="progress-bar bg-primary" role="progressbar" style="width: {{ item.kpis.recommended_avg_pct|unlocalize }}%"></div>
                            </div>
                        </td>
                        <td>{{ item.configured_fittings }}/{{ item.fittings_total }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="10" class="text-muted">{% translate "No doctrines available for this filter scope." %}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
</div>

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


<script>
    $(document).ready(() => {
        $('[data-bs-tooltip="aa-mastery"]').tooltip();
    });
</script>
{% endblock %}
