{% extends "layout.html" %}
{% import "utils.html" as utils with context %}
{% block title %}
<title>Preparing patchset for {{repo.owner.canonical_name}}/{{repo.name}} - {{cfg("sr.ht", "site-name")}} git</title>
{% endblock %}
{% block body %}
<div class="header-tabbed">
  <div class="container">
    <ul class="nav nav-tabs">
      <h2>
        <a
          href="/{{ owner.canonical_name }}"
        >{{ owner.canonical_name }}</a>/<wbr>{{ repo.name }}
      </h2>
      <li class="nav-item">
        <a class="nav-link" href="{{url_for("repo.summary",
            owner=repo.owner.canonical_name, repo=repo.name)}}">
          {{icon("caret-left")}} back
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link active" href="{{url_for("mail.send_email_start",
            owner=repo.owner.canonical_name, repo=repo.name)}}">
          prepare patchset
        </a>
      </li>
    </ul>
  </div>
</div>
<form
  class="container prepare-patchset"
  method="POST"
  action="{{url_for('mail.send_email_review',
    owner=repo.owner.canonical_name, repo=repo.name)}}"
>
  {{csrf_token()}}
  <legend>Finalize the patchset</legend>
  <small class="event-list-help">
    You can prune too-recent commits now. You'll be able to review the final
    patchset before it's sent in the next step.
  </small>
  <div class="event-list commit-list reverse">
    <input type="hidden" name="start_commit" value="{{str(start.id)}}" />
    {% for c in commits %}
    <input
      type="radio"
      name="end_commit"
      id="commit-{{str(c.id)}}"
      value="{{str(c.id)}}"
      {% if loop.first %}checked{% endif %} />
    <label class="event" for="commit-{{str(c.id)}}">
      {{ utils.commit_event(repo, c, False, href="#commit-diff-" + str(c.id)) }}
    </label>
    {% endfor %}

    {{valid.summary()}}

    <details
      {% if valid.error_for("cover_letter", "cover_letter_subject") %}
      open
      {% endif %}
    >
      <summary>Add a cover letter</summary>
      <small class="text-muted">
        The cover letter is used to describe the patchset as a whole. Add any
        comments useful for the reviewers of this patch. It will be wrapped to
        72 columns.
      </small>
      <div class="form-group">
        <input
          type="text"
          name="cover_letter_subject"
          class="form-control {{valid.cls("cover_letter_subject")}}"
          placeholder="Subject..."
          value="{{cover_letter_subject or ""}}" />
        {{valid.summary("cover_letter_subject")}}
      </div>
      <textarea
        class="form-control {{valid.cls("cover_letter")}}"
        rows="8"
        name="cover_letter"
        placeholder="Details..."
      >{{cover_letter or ""}}</textarea>
      {{valid.summary("cover_letter")}}
    </details>

    <div class="form-group">
      <label for="version">
        Patchset revision
        <small>(update this if you're sending a revised version of an earlier patch)</small>
      </label>
      <input
        type="number"
        name="version"
        id="version"
        value="{{version or 1}}"
        class="form-control" />
    </div>

    <div class="form-controls">
      <button class="btn btn-primary">Continue {{icon("caret-right")}}</a>
    </div>

    {% for diff in diffs %}
    {% set c = commits[loop.index-1] %}
    <div class="commit-diff" id="commit-diff-{{str(c.id)}}">
      <h3>{{ trim_commit(c.message) }}</h3>
      <div class="event commit-event">
        {{ utils.commit_event(repo, c, full_body=True, diff=diff) }}
        <details>
          <summary>Add commentary</summary>
          <small class="text-muted">
            Add details or caveats useful for reviewing or testing this commit.
            This won't appear in the log once the patch is applied. It will be
            wrapped to 72 columns.
          </small>
          <textarea
            class="form-control"
            rows="4"
            name="commentary_{{len(diffs) - loop.index}}"
          ></textarea>
        </details>
      </div>
      {{utils.commit_diff(repo, c, diff,
        anchor=str(c.id) + "-", target_blank=True)}}
    </div>
    {% endfor %}

    <div class="form-controls last">
      <button class="btn btn-primary">Continue {{icon("caret-right")}}</a>
    </div>
  </div>
</form>
{% endblock %}

{% block repohead %}
<style>
  .commit-diff {
    display: none;
  }

  {% for c in commits %}
  {% for d in commits[loop.index-1:] %}
  #commit-{{str(c.id)}}:checked ~ #commit-diff-{{str(d.id)}}
  {%- if not loop.last %},{% endif %}
  {% endfor %}
  {
    display: block;
  }
  {% endfor %}
</style>
{% endblock %}
