{% extends "base.html" %}

{% block main %}
{% include "breadcrumbs.html" %}
<main class="mt-4">
  <div class="container">
    <div class="row">
      <div class="col-12 col-lg-8">
        <section class="mb-3">
          <h1 class="mb-4">Projects</h1>
        </section>
        <section class="mb-3">
          <div class="alert alert-primary" role="alert">
            <p>
              Projects are created automatically when referenced in a client
              request. Make your first request:
            </p>
            <p>
              <code>
                https://callout.dev{{ base_path }}/v0/say?project=my-first-project&amp;key=***&amp;message=Hello,%20World
              </code>
            </p>
            <p>
              <code>
                https://callout.dev{{ base_path }}/v0/watchdog?project=my-first-project&amp;key=***&amp;seconds=300
              </code>
            </p>
          </div>
        </section>
        <section class="mb-3">
          <table class="table">
            <thead>
              <tr>
                <th>Project Name</th>
              </tr>
            </thead>
            <tbody>
              {% for project in projects %}
                <tr>
                  <td>
                    <a href="{{ base_path }}/teams/{{ nav_state.team_id.unwrap().simple() }}/projects/{{ project.id.simple() }}">
                      {{ project.name }}
                    </a>
                  </td>
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </section>
      </div>
      <div class="col">
        <section class="mb-3">
          <h1 class="mb-4">API Keys</h1>
        </section>
        <section class="mb-3">
          <form method="post" action="{{ base_path }}/teams/{{ nav_state.team_id.unwrap().simple() }}/new-api-key">
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
            <button class="btn btn-primary" type="submit">Generate Key</button>
          </form>
        </section>
        <section class="mb-3">
          <table class="table">
            <thead>
              <tr>
                <th>
                  API Key
                </th>
                <th title="Last Used (UTC)">
                  Last Used
                </th>
                <th>
                  Actions
                </th>
              </tr>
            </thead>
            <tbody>
              {% for key in keys %}
                <tr>
                  <td>
                    <code>
                      ********{{ key.id.simple().to_string()[key.id.simple().to_string().char_indices().nth_back(3).unwrap().0..] }}
                    </code>
                  </td>
                  <td>
                    {% if let Some(last_used_at) = key.last_used_at %}
                      {{ last_used_at.format("%Y-%m-%d") }}
                    {% else %}
                      Never
                    {% endif %}
                  </td>
                  <td>
                    <div class="btn-group btn-group-sm" role="group" aria-label="API key actions">
                      <button
                        class="btn btn-outline-light"
                        type="button"
                        name="api-key-copy-button"
                        data-copy="{{ key.id.simple() }}"
                      >
                        Copy
                      </button>
                      <button class="btn btn-outline-light" type="button">Delete</button>
                    </div>
                  </td>
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </section>
      </div>
    </div>
  </div>
</main>
<script>
document.addEventListener("DOMContentLoaded", function() {
  document.getElementsByName("api-key-copy-button")
    .forEach(function (btn) {
      btn.addEventListener("click", function (ev) {
        var content = ev.currentTarget.getAttribute("data-copy");
        navigator.clipboard.writeText(content);
      });
    });
});
</script>
{% endblock %}
