{% extends "holdfast/den/base.html" %}
{% load i18n %}
{% load humanize %}

{% block holdfast_content %}

    {% if sov_impacted %}
        <div class="holdfast-section-title">{% translate "Costing us sovereignty upgrades" %}</div>
        <div class="alert alert-danger py-2">
            {% blocktranslate %}A siphon only really matters once it knocks something out. In these systems a den is taking workforce <em>and</em> a sovereignty upgrade is unpowered at the same time.{% endblocktranslate %}
        </div>
        <div class="table-responsive mb-3">
            <table class="table table-sm align-middle">
                <thead>
                    <tr>
                        <th>{% translate "Planet" %}</th>
                        <th>{% translate "System" %}</th>
                        <th class="holdfast-num">{% translate "Workforce taken" %}</th>
                        <th>{% translate "Upgrades down" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in sov_impacted %}
                        <tr class="holdfast-row--critical">
                            <td>{{ row.skyhook.planet_name }}</td>
                            <td><a href="https://evemaps.dotlan.net/system/{{ row.skyhook.system_name }}" target="_blank" rel="noopener">{{ row.skyhook.system_name }}</a></td>
                            {% with estimate=row.skyhook.siphon_estimate %}
                                <td class="holdfast-num text-danger fw-bold">
                                    &minus;{{ estimate.1|intcomma }}
                                    ({{ estimate.0|floatformat:0 }}%)
                                    <div class="holdfast-sub fw-normal">{{ estimate.2 }}</div>
                                </td>
                            {% endwith %}
                            <td class="holdfast-sub">
                                {% for upgrade in row.upgrades %}
                                    <span class="badge text-bg-danger">{{ upgrade.type_name }}</span>
                                {% endfor %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}

    {% if pending_claims %}
        <div class="holdfast-section-title">{% translate "Claims awaiting a decision" %}</div>
        <div class="table-responsive">
            <table class="table table-sm align-middle" data-filterable
                   data-filter-placeholder="{% translate 'Search claims…' %}">
                <thead>
                    <tr>
                        <th data-filter="{% translate 'Planet' %}">{% translate "Planet" %}</th>
                        <th data-filter="{% translate 'Applicant' %}">{% translate "Applicant" %}</th>
                        <th>{% translate "Character" %}</th>
                        <th>{% translate "Requested" %}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for claim in pending_claims %}
                        <tr>
                            <td>{{ claim.slot.planet_name }}</td>
                            <td>{{ claim.user.username }}</td>
                            <td>{{ claim.character.character_name }}</td>
                            <td class="holdfast-sub">{{ claim.created_at|date:"Y-m-d H:i" }} UTC</td>
                            <td class="text-end text-nowrap">
                                {% if can_manage %}
                                    <form method="post" class="d-inline" action="{% url 'holdfast:den_decide_claim' claim.pk 'approve' %}">
                                        {% csrf_token %}
                                        <button class="btn btn-sm btn-success">{% translate "Approve" %}</button>
                                    </form>
                                    <form method="post" class="d-inline" action="{% url 'holdfast:den_decide_claim' claim.pk 'reject' %}">
                                        {% csrf_token %}
                                        <button class="btn btn-sm btn-outline-danger">{% translate "Reject" %}</button>
                                    </form>
                                {% else %}
                                    <span class="holdfast-sub">{% translate "a den manager decides" %}</span>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        <p class="holdfast-sub">{% translate "Approving one applicant automatically rejects everyone else queued on that site." %}</p>
    {% endif %}

    <div class="holdfast-section-title">{% translate "Every site, with holders" %}</div>
    <div class="table-responsive">
        <table class="table table-sm table-hover align-middle" data-filterable
               data-filter-placeholder="{% translate 'Search planet, system, holder…' %}">
            <thead>
                <tr>
                    <th data-filter="{% translate 'Planet' %}">{% translate "Planet" %}</th>
                    <th data-filter="{% translate 'System' %}">{% translate "System" %}</th>
                    <th data-filter="{% translate 'Status' %}">{% translate "Status" %}</th>
                    <th>{% translate "Held by" %}</th>
                    <th data-filter="{% translate 'Corporation' %}">{% translate "Corporation" %}</th>
                    <th>{% translate "Impact" %}</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for slot in slots %}
                    <tr class="{% if slot.skyhook.is_siphon_suspected %}holdfast-row--danger{% endif %}">
                        <td>{{ slot.planet_name }}</td>
                        <td><a href="https://evemaps.dotlan.net/system/{{ slot.system_name }}" target="_blank" rel="noopener">{{ slot.system_name }}</a></td>
                        <td data-filter-value="{{ slot.status_label }}">
                            <span class="holdfast-status holdfast-status--{{ slot.status }}">{{ slot.status_label }}</span>
                            {% if slot.is_overdue %}<div class="holdfast-sub text-warning">{% translate "approved, nothing anchored" %}</div>{% endif %}
                            {# Revoking cannot unanchor anything; only the operator can. #}
                            {% if slot.awaiting_removal %}<div class="holdfast-sub text-warning">{% translate "revoked, den still up" %}</div>{% endif %}
                        </td>
                        <td>
                            {{ slot.holder_name|default:"&mdash;" }}
                            {% if slot.holder_source %}<div class="holdfast-sub">{{ slot.holder_source }}</div>{% endif %}
                        </td>
                        <td class="holdfast-sub">{{ slot.holder_corporation|default:"&mdash;" }}</td>
                        <td class="holdfast-sub">
                            {% with estimate=slot.skyhook.siphon_estimate %}
                                {% if estimate.0 %}
                                    <span class="text-danger fw-bold">&minus;{{ estimate.0|floatformat:0 }}% ({{ estimate.1|intcomma }})</span>
                                    {# measured is arithmetic; inferred rests on the peak having been clean #}
                                    <div class="holdfast-sub">{{ estimate.2 }}</div>
                                {% else %}&mdash;{% endif %}
                            {% endwith %}
                        </td>
                        <td class="text-end">
                            {% if can_manage %}
                                {% with granted=slot.approved_claim %}
                                    {% if granted %}
                                        {# Taking the ground back. The operator has to unanchor it themselves. #}
                                        <form method="post" class="d-inline"
                                              action="{% url 'holdfast:den_decide_claim' granted.pk 'revoke' %}"
                                              onsubmit="return confirm('{% translate "Revoke this slot? The operator will be told to unanchor their den." %}');">
                                            {% csrf_token %}
                                            <button class="btn btn-sm btn-outline-warning">{% translate "Revoke" %}</button>
                                        </form>
                                    {% endif %}
                                {% endwith %}
                                <button class="btn btn-sm btn-outline-secondary" type="button"
                                        data-bs-toggle="collapse" data-bs-target="#hostile-{{ slot.pk }}">
                                    {% translate "Record" %}
                                </button>
                            {% endif %}
                        </td>
                    </tr>
                    {% if can_manage %}
                        <tr data-filter-follows>
                            <td colspan="7" class="p-0 border-0">
                                <div class="collapse" id="hostile-{{ slot.pk }}">
                                    <form method="post" class="d-flex flex-wrap gap-2 align-items-center p-2"
                                          action="{% url 'holdfast:den_record_den' slot.pk %}">
                                        {% csrf_token %}
                                        {% if slot.recorded_den %}
                                            <input type="hidden" name="present" value="0">
                                            <span class="holdfast-sub">
                                                {% if slot.recorded_hostile %}{% translate "Hostile" %}{% else %}{% translate "Ours" %}{% endif %} &mdash;
                                                {{ slot.recorded_owner_note|default:_("no owner noted") }}
                                                {% if slot.recorded_corporation_note %}({{ slot.recorded_corporation_note }}){% endif %}
                                                &mdash; {% translate "recorded by" %} {{ slot.recorded_by }}
                                                {{ slot.recorded_at|date:"Y-m-d" }}
                                            </span>
                                            <button class="btn btn-sm btn-outline-success">{% translate "Clear it" %}</button>
                                        {% else %}
                                            <input type="hidden" name="present" value="1">
                                            <input type="text" name="owner_note" class="form-control form-control-sm"
                                                   style="max-width: 16rem"
                                                   placeholder="{% translate 'Who is running it' %}">
                                            <input type="text" name="corporation_note" class="form-control form-control-sm"
                                                   style="max-width: 14rem"
                                                   placeholder="{% translate 'Their corporation' %}">
                                            <div class="form-check form-check-inline m-0">
                                                <input class="form-check-input" type="checkbox" name="hostile" value="1"
                                                       id="hostile-flag-{{ slot.pk }}">
                                                <label class="form-check-label holdfast-sub" for="hostile-flag-{{ slot.pk }}">
                                                    {% translate "Not one of ours" %}
                                                </label>
                                            </div>
                                            <button class="btn btn-sm btn-outline-primary">{% translate "Record a den here" %}</button>
                                        {% endif %}
                                    </form>
                                </div>
                            </td>
                        </tr>
                    {% endif %}
                {% endfor %}
            </tbody>
        </table>
    </div>
    <p class="holdfast-sub">
        {% translate "ESI only ever exposes a character's own dens. Anything not run by a registered operator -- hostile or our own -- has to be recorded here by hand, and a hand record is replaced automatically the day its operator registers a token." %}
    </p>

    {% if operators %}
        <div class="holdfast-section-title">{% translate "Operators, and how to reach them" %}</div>
        <div class="table-responsive">
            <table class="table table-sm align-middle" data-filterable
                   data-filter-placeholder="{% translate 'Search operators…' %}">
                <thead>
                    <tr>
                        <th>{% translate "Auth user" %}</th>
                        <th>{% translate "Main" %}</th>
                        <th>{% translate "Characters" %}</th>
                        <th>{% translate "Discord" %}</th>
                        <th data-filter="{% translate 'Timezone' %}">{% translate "Timezone" %}</th>
                        <th>{% translate "Their local time" %}</th>
                        <th class="holdfast-num">{% translate "Dens" %}</th>
                        <th>{% translate "Note" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for operator in operators %}
                        <tr>
                            <td>{{ operator.username }}</td>
                            <td class="holdfast-sub">{{ operator.main|default:"&mdash;" }}</td>
                            <td class="holdfast-sub">{{ operator.characters|join:", " }}</td>
                            <td class="holdfast-sub">{{ operator.discord|default:"&mdash;" }}</td>
                            <td class="holdfast-sub">{{ operator.timezone|default:"&mdash;" }}</td>
                            <td class="holdfast-sub">
                                {% if operator.local_time %}{{ operator.local_time|date:"H:i" }}{% else %}&mdash;{% endif %}
                            </td>
                            <td class="holdfast-num">{{ operator.dens|length }}</td>
                            <td class="holdfast-sub">{{ operator.note|default:"&mdash;" }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}
{% endblock %}
