{% extends "admin/base_site.html" %}
{% load i18n %}

{% block content %}
  <div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
    &rsaquo; <a href="{% url 'admin:nginx_siteconfiguration_changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
    &rsaquo; {% trans "Preview" %}
  </div>

  <h1>{% trans "Preview nginx configurations" %}</h1>

  {% if config_previews %}
    {% if missing_certificates %}
      <ul class="messagelist">
        <li class="warning">
          {% blocktrans count missing=missing_certificates|length %}
            {{ missing }} configuration requires a certificate before HTTPS can be applied.
          {% plural %}
            {{ missing }} configurations require certificates before HTTPS can be applied.
          {% endblocktrans %}
        </li>
      </ul>
    {% endif %}

    {% if can_apply %}
      <form method="post" class="submit-row">
        {% csrf_token %}
        <input type="hidden" name="ids" value="{{ ids_param }}" />
        <input type="submit" value="{% trans 'Apply configurations' %}" class="default" />
        <p class="help">{% trans 'Apply the selected configurations and reload nginx.' %}</p>
      </form>

      <form method="post" class="submit-row">
        {% csrf_token %}
        <input type="hidden" name="ids" value="{{ ids_param }}" />
        <div class="field-box">
          {{ subdomain_form.managed_subdomains.label_tag }}
          {{ subdomain_form.managed_subdomains }}
          {% if subdomain_form.managed_subdomains.errors %}
            <div class="errors">{{ subdomain_form.managed_subdomains.errors }}</div>
          {% endif %}
        </div>
        {% if subdomain_mixed %}
          <p class="help">{% trans "Selected configurations currently have different subdomain prefixes; updating will overwrite them." %}</p>
        {% endif %}
        <input type="submit" name="update_subdomains" value="{% trans 'Update subdomains' %}" />
        <p class="help">{{ subdomain_form.managed_subdomains.help_text }}</p>
      </form>

      <form method="post" class="submit-row" action="{{ generate_certificates_url }}">
        {% csrf_token %}
        <input type="hidden" name="ids" value="{{ ids_param }}" />
        <label for="certificate_type">{% trans "Certificate type" %}</label>
        <select name="certificate_type" id="certificate_type">
          {% for value, label in certificate_type_choices %}
            <option value="{{ value }}"{% if value == default_certificate_type %} selected{% endif %}>{{ label }}</option>
          {% endfor %}
        </select>
        <input type="submit" value="{% trans 'Generate certificates' %}" />
        <p class="help">{% trans 'Provision certificates for the selected configurations before applying HTTPS.' %}</p>
      </form>
    {% endif %}

    {% for preview in config_previews %}
      <div class="module">
        <h2>{{ preview.config }}</h2>
        {% for file in preview.files %}
          <div class="inline-group">
            <div class="tabular inline-related">
              <h3>{{ file.label }} <small><code>{{ file.path }}</code></small></h3>
              <p class="help">{{ file.status }}</p>
              {% if file.content %}
                <pre style="white-space: pre-wrap; max-height: 50vh; overflow: auto;">{{ file.content }}</pre>
              {% else %}
                <p>{% trans "No content available." %}</p>
              {% endif %}
            </div>
          </div>
        {% endfor %}
      </div>
    {% endfor %}
  {% else %}
    <p>{% trans "No configurations were selected for preview." %}</p>
  {% endif %}

  <p><a href="{% url 'admin:nginx_siteconfiguration_changelist' %}">&larr; {% trans "Back to site configurations" %}</a></p>
{% endblock %}
