{% extends "resource-new.html" %}
{% block title %}
<title>New ticket tracker - {{project.name}}</title>
{% endblock %}
{% block content %}
<div class="row">
  <form class="col-lg-8" method="POST">
    {{csrf_token()}}
    <h3>Create a new ticket tracker</h3>
    <div class="form-group">
      <label for="{{ typename }}">Name</label>
      <input
        type="text"
        name="name"
        id="name"
        class="form-control {{ valid.cls("name") }}"
        value="{{ name or project.name }}" />
      {{ valid.summary("name") }}
      <div class="form-group">
        <label for="description">Description</label>
        <textarea
          type="text"
          name="description"
          id="description"
          class="form-control {{valid.cls("description")}}"
          rows="5"
          aria-describedby="description-help-text"
          placeholder="Give users an idea of how to submit a good ticket for this tracker. Markdown supported"
        >{{ description or "" }}</textarea>
        {{valid.summary("description")}}
      </div>
    </div>
    <div class="flex-grow-1 d-flex flex-row justify-content-end">
      <button
        type="submit"
        class="btn btn-primary align-self-end"
        name="create"
      >Create new tracker {{icon("caret-right")}}</button>
    </div>
  </form>
</div>
{% if any(trackers) or search %}
<div class="row">
  <div class="col-lg-8">
    <h3 style="margin-top: 1rem">
      Or add an existing tracker
    </h3>
    {# TODO: Pagination #}
    <form class="form-group" method="POST">
      {{csrf_token()}}
      <input
        name="search"
        type="text"
        placeholder="Search ticket trackers"
        class="form-control{% if search_error %} is-invalid{% endif %}"
        value="{{ search if search else "" }}" />
      {% if search_error %}
        <div class="invalid-feedback">{{ search_error }}</div>
      {% endif %}
    </form>
    <form class="event-list select-resource" method="POSt">
      {{csrf_token()}}
      {% for tracker in trackers %}
      <div class="event">
        <h3>
          {% if tracker["id"] in existing %}
          <button
            type="submit"
            class="pull-right btn btn-primary btn-lg"
            disabled
          >Already added&nbsp;{{ icon("caret-right") }}</button>
          {% else %}
          <button
            type="submit"
            name="existing-{{ tracker["rid"] }}"
            class="pull-right btn btn-primary btn-lg"
          >Select tracker&nbsp;{{ icon("caret-right") }}</button>
          {% endif %}
          <a
            href="{{get_origin("todo.sr.ht",
              external=True)}}/{{ tracker.owner.canonical_name }}/{{tracker.name}}"
            target="_blank"
            rel="noopener"
          >{{ tracker["name"] }}</a>
        </h3>
      </div>
      {% endfor %}
    </form>
  </div>
</div>
{% endif %}
{% endblock %}
