{% extends "base.html" %}
{% block introduction %}
{% if msgs.lede %}
  <div class="hatnote">
    🔗 <a href="/id/{{abif_id}}">{{abif_id}}</a> &nbsp; &nbsp;
    (tags: { {% for t in msgs.taglist %}
    <a href="/tag/{{t}}">{{t}}</a>
    {% if not loop.last %}, {% endif %}
    {% endfor %}
    })
  </div>
<p>{{ msgs.lede | safe }}</p>
{% else %}
<p>Your .abif has been processed.  See <a href="#results">results below</a></p>
{% endif %}
{% endblock introduction %}
{% block content %}
{# Route-aware detection to avoid masking backend over-computation
 # Only treat as "all methods" when the route intends it (/id/<id> or /id/<id>/all) #}
{% set is_all_route = (resulttype is defined and (not resulttype or resulttype == 'all')) %}
{# Consider 'dot' and 'wlt' together for pairwise; treat as 'all' when each method is available #}
{% set all_results_generated = is_all_route and (
  'FPTP' in result_types and
  'IRV' in result_types and
  'STAR' in result_types and
  'approval' in result_types and
  ('dot' in result_types or 'wlt' in result_types)
) %}
{% include 'abifbox-snippet.html' %}
{# Duplicate the compact methods nav just under the ABIF box #}
{% include 'methods-nav-snippit.html' %}
<h2 id="results">{{ webenv.statusStr }}Results</h2>
{% if abif_id %}
{% set pathpart="/id/" + abif_id  %}
{% set abslink=webenv.base_url + pathpart %}
<!-- BEGIN .hatnote -->
<div class="hatnote">🔗<a href="{{abslink}}">{{abslink}}</a></div>
<!-- END .hatnote -->
{% endif %}
{% if msgs.results_name %}
<p>The {{msgs.results_name}} of {{abif_id}} are below,
and can be edited in the field above.</p>
{% elif msgs.results_lede %}
{# msgs.results_lede is DEPRECATED as of 2024-06-19 #}
<p>{{ msgs.results_lede | safe }}</p>
{% else %}
<p>Below are the results of the election represented above using various election methods with abiftool/abiflib.  {% if msgs.ballot_type %} The detected ballot type from the ABIF above is "{{ msgs.ballot_type }}"{% endif %}.  Some methods may transform these ballots for analysis; see method notices for details.  Resubmit the ABIF with "Transform ballots" turned off to minimize the transformations.
{% endif %}

  <!-- Election overview with summary table and preview image -->
  <div class="election-overview">
    <div class="summary-section">
      <!-- Summary table with winner information -->
    <table style="max-width: 60em; border-collapse: collapse; margin: 1em 0; font-size: 0.9em;">
      <thead>
        <tr style="background-color: #f5f5f5;">
          <th style="padding: 8px; border: 1px solid #ddd; text-align: left;">Method</th>
          <th style="padding: 8px; border: 1px solid #ddd; text-align: left;">Winner</th>
        </tr>
      </thead>
      <tbody>
        {% for restype in result_types %}
          {% if (restype == 'dot' or (restype == 'wlt' and not ('dot' in result_types))) and ('dot' in result_types or 'wlt' in result_types) %}
            <tr>
              <td style="padding: 8px; border: 1px solid #ddd;"><a href="#pairwise" class="method-link">Condorcet/Copeland</a></td>
              <td style="padding: 8px; border: 1px solid #ddd;">
                {% set _cope = copewinners if copewinners else resblob.get('copewinners', []) %}
                {% if _cope and FPTP_candnames %}
                  {%- for tok in _cope -%}
                    <span class="color-box" style="background-color: {{ colordict[tok] | default('#ccc') }};"></span>{{ FPTP_candnames[tok] if tok in FPTP_candnames else tok }}{% if not loop.last %}, {% endif %}
                  {%- endfor -%}
                {% elif copewinnerstring or resblob.get('copewinnerstring') %}
                  {{ copewinnerstring or resblob.get('copewinnerstring') }}
                {% else %}
                  N/A
                {% endif %}
              </td>
            </tr>
          {% elif restype == 'FPTP' %}
            <tr>
              <td style="padding: 8px; border: 1px solid #ddd;"><a href="#FPTP" class="method-link">FPTP</a></td>
              <td style="padding: 8px; border: 1px solid #ddd;">
                {% if FPTP_candnames and resblob and resblob['FPTP_result'] and resblob['FPTP_result']['winners'] %}
                  {% set winner_token = resblob['FPTP_result']['winners'][0] %}
                  <span class="color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{{ FPTP_candnames[winner_token] if winner_token in FPTP_candnames else winner_token }}
                {% elif resblob and resblob['FPTP_result'] and resblob['FPTP_result']['winners'] %}
                  {{ resblob['FPTP_result']['winners'][0] }}
                {% else %}
                  N/A
                {% endif %}
              </td>
            </tr>
          {% elif restype == 'IRV' %}
            <tr>
              <td style="padding: 8px; border: 1px solid #ddd;"><a href="#IRV" class="method-link">IRV/RCV</a></td>
              <td style="padding: 8px; border: 1px solid #ddd;">
                {% if IRV_dict and IRV_dict['winner'] and IRV_candnames %}
                  {%- for winner_token in IRV_dict['winner'] -%}
                    <span class="color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{{ IRV_candnames[winner_token] if winner_token in IRV_candnames else winner_token }}{% if not loop.last %}, {% endif %}
                  {%- endfor -%}
                {% elif IRV_dict and IRV_dict['winnerstr'] %}
                  {{ IRV_dict['winnerstr'] }}
                {% else %}
                  N/A
                {% endif %}
              </td>
            </tr>
          {% elif restype == 'STAR' %}
            <tr>
              <td style="padding: 8px; border: 1px solid #ddd;"><a href="#STAR" class="method-link">STAR</a></td>
              <td style="padding: 8px; border: 1px solid #ddd;">
                {% if scorestardict and scorestardict['scoremodel']['winner'] %}
                  {% set winner_name = scorestardict['scoremodel']['winner'] %}
                  {% if FPTP_candnames %}
                    {# Handle both single winners and ties in STAR results #}
                    {% if "tie " in winner_name %}
                      {# Handle STAR ties: extract candidate names from tie string #}
                      {%- for token, name in FPTP_candnames.items() -%}
                        {%- if name in winner_name -%}
                          <span class="color-box" style="background-color: {{ colordict[token] | default('#ccc') }};"></span>{{ name }}{% if not loop.last and loop.index < FPTP_candnames|length %}, {% endif %}
                        {%- endif -%}
                      {%- endfor -%}
                    {% else %}
                      {# Handle single winner - find matching candidate name #}
                      {%- for token, name in FPTP_candnames.items() -%}
                        {%- if winner_name == name -%}
                          <span class="color-box" style="background-color: {{ colordict[token] | default('#ccc') }};"></span>{{ winner_name }}
                        {%- endif -%}
                      {%- endfor -%}
                    {% endif %}
                  {% else %}
                    {# No FPTP_candnames available, show raw winner #}
                    {{ winner_name }}
                  {% endif %}
                {% else %}
                  N/A
                {% endif %}
              </td>
            </tr>
          {% elif restype == 'approval' %}
            <tr>
              <td style="padding: 8px; border: 1px solid #ddd;"><a href="#approval" class="method-link">Approval</a></td>
              <td style="padding: 8px; border: 1px solid #ddd;">
                {% if approval_result and approval_result['winners'] and FPTP_candnames %}
                  {%- for winner in approval_result['winners'] -%}
                    <span class="color-box" style="background-color: {{ colordict[winner] | default('#ccc') }};"></span>{{ FPTP_candnames[winner] if winner in FPTP_candnames else winner }}{% if not loop.last %}, {% endif %}
                  {%- endfor -%}
                {% elif approval_result and approval_result['winners'] %}
                  {% for winner in approval_result['winners'] %}<span class="color-box" style="background-color: {{ colordict[winner] | default('#ccc') }};"></span>{{ winner }}{% if not loop.last %}, {% endif %}{% endfor %}
                {% else %}
                  N/A
                {% endif %}
              </td>
            </tr>
          {% endif %}
        {% endfor %}
      </tbody>
    </table>


    </div>

    <!-- Preview image sidebar -->
    <div class="preview-sidebar">
      {% if abif_id %}
      <figure class="election-preview">
        <picture>
          <source srcset="/preview-img/id/{{ abif_id }}.svg" type="image/svg+xml">
          <img src="/preview-img/id/{{ abif_id }}.png"
               alt="Election results summary showing voting method winners"
               class="preview-image"
               onclick="openImageModal(this.currentSrc || this.src, '{{ msgs.og_description | replace('\'', '\\\'') }}')"
               style="cursor: pointer;">
        </picture>
        <!-- Preload PNG so the modal's PNG switch is instant -->
        <img src="/preview-img/id/{{ abif_id }}.png" alt="" aria-hidden="true" loading="eager" style="position:absolute; left:-9999px; width:1px; height:1px; opacity:0;">
        <figcaption class="preview-caption">
          <em>click/tap to expand</em>
        </figcaption>
      </figure>
      {% endif %}
    </div>
  </div>

  <!-- Toggle positioned above tabs, aligned right -->
  <div class="tab-controls">
    <!-- Hidden checkbox for CSS selectors -->
    <input type="checkbox" id="tabbed-mode" class="view-mode-input"{% if not resulttype or resulttype == 'all' %} checked{% endif %}>
    <!-- View mode toggle positioned above tabs -->
    <div class="view-mode-toggle-tabs">
      <label class="toggle-switch" for="tabbed-mode">
        <span class="toggle-slider">
          <span class="toggle-text off">OFF</span>
          <span class="toggle-text on">ON</span>
        </span>
      </label>
      <span class="toggle-label">tabbed view</span>
    </div>
  </div>

  <!-- Tabs for various methods -->
  <nav class="method-tabs" role="tablist" aria-label="Voting method results navigation">
    {% for restype in result_types %}
      {% if (restype == 'dot' or (restype == 'wlt' and not ('dot' in result_types))) and ('dot' in result_types or 'wlt' in result_types) %}
        <a href="#pairwise" class="method-tab" role="tab" aria-controls="condorcet-section">
          {% set _cope = copewinners if copewinners else resblob.get('copewinners', []) %}
          {% if _cope and FPTP_candnames %}
            {%- for tok in _cope -%}
              <span class="tab-color-box" style="background-color: {{ colordict[tok] | default('#ccc') }};"></span>{% if not loop.last %}{% endif %}
            {%- endfor -%}
          {% endif %}
          Condorcet/Copeland
        </a>
      {% elif restype == 'FPTP' %}
        <a href="#FPTP" class="method-tab" role="tab" aria-controls="fptp-section">
          {% if FPTP_candnames and resblob and resblob['FPTP_result'] and resblob['FPTP_result']['winners'] %}
            {% set winner_token = resblob['FPTP_result']['winners'][0] %}
            <span class="tab-color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>
          {% endif %}
          FPTP
        </a>
      {% elif restype == 'IRV' %}
        <a href="#IRV" class="method-tab" role="tab" aria-controls="irv-section">
          {% if IRV_dict and IRV_dict['winner'] and IRV_candnames %}
            {%- for winner_token in IRV_dict['winner'] -%}
              <span class="tab-color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{% if not loop.last %}{% endif %}
            {%- endfor -%}
          {% endif %}
          IRV/RCV
        </a>
      {% elif restype == 'STAR' %}
        <a href="#STAR" class="method-tab" role="tab" aria-controls="star-section">
          {% if scorestardict and scorestardict['scoremodel']['winner'] and FPTP_candnames %}
            {% set winner_name = scorestardict['scoremodel']['winner'] %}
            {# Handle both single winners and ties in STAR results #}
            {% for token, name in FPTP_candnames.items() %}
              {% if winner_name == name or name in winner_name %}
                <span class="tab-color-box" style="background-color: {{ colordict[token] | default('#ccc') }};"></span>
              {% endif %}
            {% endfor %}
          {% endif %}
          STAR
        </a>
      {% elif restype == 'approval' %}
        <a href="#approval" class="method-tab" role="tab" aria-controls="approval-section">
          {% if approval_result and approval_result['winners'] and FPTP_candnames %}
            {%- for winner_token in approval_result['winners'] -%}
              <span class="tab-color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{% if not loop.last %}{% endif %}
            {%- endfor -%}
          {% endif %}
          Approval
        </a>
      {% endif %}
    {% endfor %}
    {% if resulttype and resulttype != 'all' %}
      <a href="/id/{{ abif_id }}" class="view-all-methods">View All Methods...</a>
    {% endif %}
  </nav>
</p>
<!-- BEGIN .resultbox -->
<div class="resultbox results-container">
<!-- BEGIN resultbox -->
{% for restype in result_types %}
  {% if restype == 'dot' or (restype == 'wlt' and not ('dot' in result_types)) %}
    <div class="method-section" id="condorcet-section" data-method="condorcet">
    {############ condorcet/pairwise ############}
    {% if 'dot' in result_types or 'wlt' in result_types %}
    <h3><a name="pairwise"></a>Condorcet/Copeland results</h3>
    {% if abif_id %}
    {% set pairwise_pathpart="/id/" + abif_id + "/pairwise#pairwise" %}
    {% set pairwise_abslink=webenv.base_url + pairwise_pathpart %}
    <div class="hatnote">
      {{abif_id}} Condorcet/Copeland results permalink:<br>
      🔗<a href="{{pairwise_abslink}}">{{pairwise_abslink}}</a>
    </div>
    {% endif %}

    <!-- BEGIN pairwise notices -->
    {% set notices = resblob.notices.pairwise %}
    {% include 'notice-snippet.html' %}
    <!-- END pairwise notices -->
    {# Transformed ballots accordion for Condorcet/Copeland when available (directly under notices) #}
    {% set _tx = resblob.get('transforms', {}).get('pairwise') if resblob else None %}
    {% if _tx and _tx.get('abif') %}
      {% set transformed_abif = _tx.get('abif') %}
      {% set conversion_meta = _tx.get('meta') or {} %}
      {% set method_label = 'Condorcet/Copeland' %}
      {% set target_type = _tx.get('target_type') %}
      {% include 'transform-accordion-snippet.html' %}
    {% endif %}

    <!-- Include detailed pairwise summary -->
    {% if pairwise_summary_html %}
      {{ pairwise_summary_html | safe }}
    {% endif %}

    <!-- Include pairwise results -->
    {% if 'wlt' in result_types and pairwise_html %}
      <h4><a name="wlt"></a>Win-loss-tie (Condorcet/Copeland) table</h4>
      {% if abif_id %}
      {% set wlt_pathpart="/id/" + abif_id + "/pairwise#wlt" %}
      {% set wlt_abslink=webenv.base_url + wlt_pathpart %}
      <div class="hatnote">
        {{abif_id}} w-l-t table permalink:<br>
        🔗<a href="{{wlt_abslink}}">{{wlt_abslink}}</a>
      </div>
      {% endif %}
      {{ pairwise_html | safe }}
    {% endif %}

    {# (accordion already rendered directly under notices above) #}

    {% if 'dot' in result_types and dotsvg_html %}
      <h4><a name="dot"></a>Pairwise tournament (Copeland ordered)</h4>
      {% if abif_id %}
      {% set dot_pathpart="/id/" + abif_id + "/pairwise#dot" %}
      {% set dot_abslink=webenv.base_url + dot_pathpart %}
      <div class="hatnote">
        {{abif_id}} pairwise diagram permalink:<br>
        🔗<a href="{{dot_abslink}}">{{dot_abslink}}</a>
      </div>
      {% endif %}
      {% if abif_id %}
      <p><a href="/id/{{abif_id}}/dot/svg">{{ dotsvg_html | safe }}</a></p>
      {% else %}
      <p>{{ dotsvg_html | safe }}</p>
      {% endif %}
    {% endif %}
    {% endif %}
    </div> <!-- End condorcet section -->
  {% elif restype == 'wlt' %}
    {# wlt section now handled by dot section above - this maintains backward compatibility #}
  {% elif restype == 'FPTP' %}
    <div class="method-section" id="FPTP-section" data-method="FPTP">
    {############ FPTP ############}
    {% set rellink=restype + "#" + restype %}
    {% if 'FPTP' in result_types %}

    <h3><a name="{{restype}}"></a>FPTP result</h3>
    {% if abif_id %}
    {% set pathpart="/id/" + abif_id + "/" + rellink %}
    {% set abslink=webenv.base_url + pathpart %}
    <!-- BEGIN .hatnote -->
    <div class="hatnote">
      "FPTP" is "First-past-the-post", also known as "plurality" or "choose-one"<br>
      {{abif_id}} FPTP results permalink:<br>
      🔗<a href="{{abslink}}">{{abslink}}</a><br>
    </div>
    <!-- END .hatnote -->
    {% endif %}

    <!-- BEGIN fptp notices -->
    {% set notices = resblob.notices.fptp %}
    {% include 'notice-snippet.html' %}
    <!-- END fptp notices -->

    <p>{% include 'fptp-snippet.html' %}</p>
    {% endif %}
    <!-- END FPTP section -->
    </div> <!-- End FPTP section -->
  {% elif restype == 'IRV' %}
    <div class="method-section" id="IRV-section" data-method="IRV">
    {############ IRV ############}
    {% set rellink=restype + "#" + restype %}
    {% if 'IRV' in result_types and IRV_dict %}
    <h3><a name="{{restype}}"></a>IRV/RCV results</h3>
    {% if abif_id %}
    {% set pathpart="/id/" + abif_id + "/" + rellink %}
    {% set abslink=webenv.base_url + pathpart %}
    <div class="hatnote">
      {{abif_id}} IRV/RCV result permalink:<br>
      🔗<a href="{{abslink}}">{{abslink}}</a>
    </div>
    {% endif %}
    <!-- BEGIN irv notices -->
    {% set notices = resblob.notices.irv %}
    {% include 'notice-snippet.html' %}
    <!-- END irv notices -->
    {# Transformed ballots accordion for IRV when available (directly under notices) #}
    {% set _tx = resblob.get('transforms', {}).get('IRV') if resblob else None %}
    {% if _tx and _tx.get('abif') %}
      {% set transformed_abif = _tx.get('abif') %}
      {% set conversion_meta = _tx.get('meta') or {} %}
      {% set method_label = 'IRV/RCV' %}
      {% set target_type = _tx.get('target_type') %}
      {% include 'transform-accordion-snippet.html' %}
    {% endif %}
    <!-- BEGIN results-container -->
    <div id="results-container" data-candidate-colors='{{ colordict | tojson | safe }}'>
        <p>{% include 'irv-snippet.html' %}</p>
    </div>
    <!-- END results-container -->
    <script type="module" src="{{ url_for('static', filename='js/main.js') }}"></script>
    {% endif %}
    <!-- END IRV section -->
    </div> <!-- End IRV section -->
  {% elif restype == 'STAR' %}
    <div class="method-section" id="STAR-section" data-method="STAR">
    {############ STAR ############}
    {% set rellink=restype + "#" + restype %}
    {% if 'STAR' in result_types and STAR_html %}
    <h3><a name="{{restype}}"></a>STAR results</h3>
    {% if abif_id %}
    {% set pathpart="/id/" + abif_id + "/" + rellink %}
    {% set abslink=webenv.base_url + pathpart %}
    <!-- BEGIN .hatnote -->
    <div class="hatnote">
      {{abif_id}} STAR results permalink:<br>
      🔗<a href="{{abslink}}">{{abslink}}</a>
    </div>
    <!-- END .hatnote -->
    {% endif %}
    {% if scorestardict and scorestardict['star_lede'] %}
    <p class="description">
       {{ scorestardict['star_lede'] }}
    </p>
    {% endif %}

    <!-- BEGIN star notices -->
    {% set notices = resblob.notices.star %}
    {% include 'notice-snippet.html' %}
    <!-- END star notices -->

    <p>{% include 'star-snippet.html' %}</p>
    {% if scorestardict and scorestardict['star_foot'] %}
    <p class="description">
      {{ scorestardict['star_foot'] }}
    </p>
    {% endif %}
    {% endif %}
    <!-- END STAR section -->
    </div> <!-- End STAR section -->
  {% elif restype == 'approval' %}
    <div class="method-section" id="approval-section" data-method="approval">
    {############ Approval Voting ############}
    {% set rellink=restype + "#" + restype %}
    {% if 'approval' in result_types and approval_text %}
    <h3><a name="{{restype}}"></a>Approval voting results</h3>
    {% if abif_id %}
    {% set pathpart="/id/" + abif_id + "/" + rellink %}
    {% set abslink=webenv.base_url + pathpart %}
    <!-- BEGIN .hatnote -->
    <div class="hatnote">
      {{abif_id}} approval voting results permalink:<br>
      🔗<a href="{{abslink}}">{{abslink}}</a>
    </div>
    <!-- END .hatnote -->
    {% endif %}

    <!-- BEGIN approval notices -->
    {% set notices = resblob.notices.approval %}
    {% include 'notice-snippet.html' %}
    <!-- END approval notices -->
    {# Transformed ballots accordion for Approval when available (directly under notices) #}
    {% set _tx = resblob.get('transforms', {}).get('approval') if resblob else None %}
    {% if _tx and _tx.get('abif') %}
      {% set transformed_abif = _tx.get('abif') %}
      {% set conversion_meta = _tx.get('meta') or {} %}
      {% set method_label = 'Approval' %}
      {% set target_type = _tx.get('target_type') %}
      {% include 'transform-accordion-snippet.html' %}
    {% endif %}

    {% include 'approval-snippet.html' %}
    {% endif %}
    <!-- END Approval Voting section -->
    </div> <!-- End approval section -->
  {% endif %}
{% endfor %}
<!-- END resultbox -->
</div>
<!-- END .resultbox -->

{% include 'methods-nav-snippit.html' %}

<p>(<a href="/">homepage</a>)</p>
{% endblock content %}
