{% comment %}
Copyright (C) 2019-2024  The Software Heritage developers
See the AUTHORS file at the top-level directory of this distribution
License: GNU Affero General Public License version 3, or any later version
See top-level LICENSE file for more information
{% endcomment %}

{% load static %}
{% load swh_templatetags %}

{% if snapshot_context and snapshot_context.visit_info and snapshot_context.visit_info.type|visit_type_savable %}
  <button id="swh-take-new-snashot"
          class="btn btn-secondary btn-sm"
          type="button"
          data-bs-toggle="modal"
          data-bs-target="#swh-take-new-snapshot-modal">
    <i class="mdi mdi-camera mdi-fw" aria-hidden="true"></i>
    Save again
  </button>
  <div class="modal fade"
       id="swh-take-new-snapshot-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="swh-take-new-snapshot-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="swh-take-new-snapshot-modal-label">
            Take a new snapshot of a software origin
          </h6>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>
            If the archived software origin currently browsed is not synchronized with its upstream
            version (for instance when new commits have been issued), you can explicitly request Software
            Heritage to take a new snapshot of it.
            <p></p>
            Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible.
            You can then check its processing state by visiting this
            {% if "swh.web.save_code_now" in SWH_DJANGO_APPS %}
              <a href="{% url 'origin-save' %}#requests">dedicated page</a>.
            {% else %}
              <a href="https://archive.softwareheritage.org/save#requests">dedicated page</a>.
            {% endif %}
          </p>
          <form id="swh-take-new-snapshot-form" class="mt-3">
            {% csrf_token %}
            <div class="row g-3">
              <div class="col-md-3">
                <label class="form-label" for="swh-input-visit-type">Visit type</label>
                <input id="swh-input-visit-type"
                       class="form-control"
                       value="{{ snapshot_context.visit_info.type }}"
                       disabled />
              </div>
              <div class="col-md-9">
                <label class="form-label" for="swh-input-origin-url">Origin url</label>
                <input type="text"
                       class="form-control"
                       id="swh-input-origin-url"
                       value="{{ snapshot_context.origin_info.url }}"
                       disabled />
              </div>
            </div>
            <div class="row g-3 mt-1">
              <div class="col-md-3 ms-auto me-auto">
                {% if "swh.web.save_code_now" in SWH_DJANGO_APPS %}
                  <label class="form-label" for="swh-take-new-snapshot-submit">&#8203;</label>
                  <button type="submit"
                          id="swh-take-new-snapshot-save-submit"
                          class="btn btn-secondary btn-block">Submit</button>
                {% elif SWH_MIRROR_CONFIG %}
                  <a class="btn btn-secondary btn-block"
                     href="https://archive.softwareheritage.org/save?origin_url={{ snapshot_context.origin_info.url }}"
                     target="_blank"
                     rel="noopener noreferrer">
                    Submit <i class="nav-icon mdi mdi-open-in-new"></i>
                  </a>
                {% endif %}
              </div>
            </div>
          </form>
          {% if not SWH_MIRROR_CONFIG %}
            <div class="swh-processing-save-request text-center" style="display: none">
              <img src="{% static 'img/swh-spinner.gif' %}" alt="swh spinner" />
              <p>Processing "take a new snapshot" request ...</p>
            </div>
            <div id="swh-take-new-snapshot-request-status"></div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>
  {% if not SWH_MIRROR_CONFIG %}<script>swh.save_code_now.initTakeNewSnapshot();</script>{% endif %}
{% endif %}
