<% content_for "title" do %>ToV: <%= cap.kind.to_verb %> <%= ballot.title %><% end %>
<% content_for "og_desc" do %>
  <%- if cap.kind == CapKind::BallotAdmin -%>
    You were supposed to keep this link private, s m h
  <%- elsif cap.kind == CapKind::BallotVote -%>
    Cast your vote with this ballot on Tug of Vote
  <%- else -%>
    View this ballot on Tug of Vote
  <%- end -%>
<% end %>

<section class="ballot-results">
  <h2><%= ballot.title %></h2>
  <p><%= content_to_html(ballot.description) %></p>
  <hr>
  <p>
    Current results, calculated according to <a href="https://en.wikipedia.org/wiki/Schulze_Method" rel="noreferrer">Schulze</a>
    with <%= pluralize(votes.size, "vote") %>:
  </p>

  <%- unless ballot.hide_names -%>
  <div class="names">
    <%- votes.each do |vote| -%>
    <address><%= vote.username %></address>
    <%- end -%>
  </div>
  <%- end -%>

  <ol>
    <%- cached_result.each do |candidates| -%>
      <li><span><%= candidates.sort.join(" <em>and</em><br>") %></span></li>
    <%- end -%>
  </ol>

  <p class="object-metadata">
    Created at <%= ballot.created_at %>.
    <%- if ballot.duration -%>
    <span data-js-countdown-to="<%= close_timestamp %>">
      Voting closes automatically after <%= pluralize(ballot.duration.not_nil!, "hour") %>.
    </span>
    <%- end -%>
    <%- if cap.kind == CapKind::BallotVoteDisabled -%>
    <noscript>
    Voting has ended. You can only view the results now.
    </noscript>
    <%- end -%>
    <br>
    <%=
      lower_caps
      .sort_by{|cap| cap.kind }
      .reverse
      .map {|cap| "<a href=\"/cap/#{cap.cap_slug}\">#{cap.kind}</a>" }
      .join(" Â· ")
    %>
    <br>
    Only share these links with people you trust to use the respective permission.
  </p>
</section>

<section class="object-actions">
  <%- if cap.kind == CapKind::BallotVote || cap.kind == CapKind::BallotAdmin -%>
    <div class="vote">
      <h2>Vote</h2>
      <p class="notes">
        <em>Notes:</em> Order the candidates in descending order of preference, i.e. with <em>1</em> as your favorite candidate
        and <em><%= ballot.candidates.size %></em> as your least-favorite one. Ties between candidates are allowed.
        You can choose to leave candidates unordered, they'll count as if they were all set to <em><%= ballot.candidates.size + 1%></em>.
        To delete your vote entirely, fill in blanks everywhere.<br>
        <%- if ballot.hide_names -%>
        Your order of preference (and your username) will not be displayed publicly, but is visible to the site admin
        and anyone threateningly waving a gun at them.
        <%- else -%>
        <em class="danger">
        Your username will be publicly visible, which may make it possible to deduce your order of preference.
        </em>
        <%- end -%>
      </p>
      <form action="/cap/<%= cap.cap_slug %>/ballot/vote" method="post">
        <div class="iam">
          <label for="name">Your name</label>
          <input id="name" placeholder="Moros" name="name" required="" maxlength="42">
          <label for="password">PIN</label>
          <input id="password" name="password" required="" maxlength="70">
        </div>
        <ul>
          <%- ballot.candidates.sort.each_with_index do |candidate, index| -%>
          <li>
            <input type="number" min="1" max="<%= ballot.candidates.size %>" name="<%= index %>"> <%= candidate %>
          </li>
          <%- end -%>
        </ul>
        <button>Save</button>
      </form>
    </div>
  <%- end -%>
  <%- if cap.kind == CapKind::BallotAdmin -%>
    <div class="admin">
      <h2>Admin</h2>

      <p>
        <a
          href="/cap/<%= cap.cap_slug %>/ballot/end_voting"
          class="danger"
          data-js-confirm="Are you sure you want to irreversibly close voting?"
        >
          End voting
        </a>
      </p>

      <form action="/cap/<%= cap.cap_slug %>/ballot/update" method="post">
        <label for="edit-ballot-title">Edit title</label>
        <input id="edit-ballot-title" name="title" value="<%= ballot.title %>" required maxlength="200">
        <label for="edit-ballot-textarea">Edit description</label>
        <textarea id="edit-ballot-textarea" name="description" required maxlength="20000"><%= ballot.description %></textarea>
        <button>Update ballot</button>
      </form>
    </div>
  <%- end -%>
</section>
