{% extends "admin/base.html" %}
{% load static %}
{% block head %}
    {{ block.super }}
    <script src="{% static 'se/tags.js' %}"></script>

    {# Token used by Javscript code to make ``fetch`` requests #}
    <meta name="csrf-token" content="{{ csrf_token }}">
{% endblock %}

{% block extrahead %}
  {{ blocksuper }}
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const radioDefault = document.getElementById('id_crawl_policy_choice_0');
      const radioDomain = document.getElementById('id_crawl_policy_choice_1');
      const labelDefault = document.getElementById('tags_label_default');
      const labelDomain = document.getElementById('tags_label_domain');

      function updateLabels() {
        if (radioDefault.checked) {
          labelDefault.style.display = 'block';
          labelDomain.style.display = 'none';
        } else if (radioDomain.checked) {
          labelDefault.style.display = 'none';
          labelDomain.style.display = 'block';
        }
      }

      // Attach event listeners to radio buttons
      radioDefault.addEventListener('change', updateLabels);
      radioDomain.addEventListener('change', updateLabels);

      // Initialize the correct label visibility on page load
      updateLabels();
    });
  </script>
{% endblock %}

{% block breadcrumbs %}
    <a href="{% url 'admin:index' %}">Admin</a> &gt;
    <a href="{% url 'admin:se_crawlpolicy_changelist' %}">Crawl policies</a> &gt;
    <span class="current">Add to queue</span>
{% endblock %}

{% block title %}
    {{ block.super }} - Add to queue
{% endblock %}

{% block content_title %}
    {{ block.super }} - Add to queue
{% endblock %}}

{% block content %}
    {% include "se/components/modal.html" with id="tags" title=tags_edit_title %}
    {% if crawl_policies %}
      <form method="post" action="{% url 'admin:queue_confirm' %}">
          {% csrf_token %}
    {% else %}
      <form method="get" action="{% url 'admin:queue_confirm' %}">
    {% endif %}
        <h3>{{ form.urls.label }}</h3>
        {{ form.urls.errors }}
        {{ form.urls }}
        {% if form.urls.help_text %}
            <p class="help">{{ form.urls.help_text|safe }}</p>
        {% endif %}

        {% if crawl_policies %}
            <input type="submit" name="action" value="Refresh" style="margin-top: 5px">

            <h3>Crawl polic{{ crawl_policies|length|pluralize:"y,ies" }}</h3>
            {% if form.fields.crawl_policy_choice.choices %}
                <p>
                    {% include "admin/crawl_policy_desc.html" with add_to_queue=True label_tag="label_tag" urls=urls %}
                    <span class="label_tag">
                    📝 <a href="{% url 'admin:se_crawlpolicy_change' crawl_policy.id %}?return_url={{ return_url }}">Edit</a>
                  </span>
                </p>

                <p>
                  {{ form.crawl_policy_choice }}
                </p>

                <p>
                  <div class="form-row field-tags">
                    <label id="tags_label_default" for="tags">Tags for this URL:</label>
                    <label id="tags_label_domain" for="tags" style="display: none">Tags for all pages of https://{{ domain }}/:</label>
                    <div style="margin-top: 3px;">
                      {{ form.tags }}
                    </div>
                  </div>
                </p>

                <p>
                    <a href="{% url 'admin:se_crawlpolicy_add' %}?url_regex={{ urls|first|urlencode }}&return_url={{ return_url }}">Create a new policy</a>
                </p>
            {% elif crawl_policies|length == 1 %}
                <p id="matching_policy">
                    {{ urls|length|pluralize:"This URL,These URLs" }} will be crawled with policy <a href="{% url 'admin:se_crawlpolicy_change' crawl_policy.id %}?return_url={{ return_url }}">{{ crawl_policy }}</a>:
                </p>
                <p>
                    {% include "admin/crawl_policy_desc.html" with add_to_queue=True label_tag="label_tag" urls=urls %}
                </p>
                <p>
                    <a href="{% url 'admin:se_crawlpolicy_add' %}?url_regex={{ urls|first|urlencode }}&return_url={{ return_url }}">Create a new policy</a>
                </p>
            {% else %}
                <p>
                    These URLs will be crawled with policies:
                    <ul>
                        {% for crawl_policy in crawl_policies %}
                            <li><a href="{% url 'admin:se_crawlpolicy_change' crawl_policy.id %}&return_url={{ return_url }}">{{ crawl_policy }}</a></li>
                        {% endfor %}
                    </ul>
                </p>
            {% endif %}

            {% if crawl_policy.recursion == CrawlPolicy.CRAWL_ON_DEPTH or settings.SOSSE_BROWSABLE_HOME %}
                <h3>Parameters</h3>
            {% endif %}
            {% if crawl_policy.recursion == CrawlPolicy.CRAWL_ON_DEPTH %}
                {{ form.recursion_depth.errors }}
                {{ form.recursion_depth.label_tag }} {{ form.recursion_depth }}
                {% if form.recursion_depth.help_text %}
                    <small><p class="help">{{ form.recursion_depth.help_text|safe }}</p></small>
                {% endif %}
            {% endif %}
            {% if settings.SOSSE_BROWSABLE_HOME %}
                {{ form.show_on_homepage.errors }}
                {{ form.show_on_homepage }}
                <label class="vCheckboxLabel" for="id_show_on_homepage">Show on homepage</label>

                {% if form.show_on_homepage.help_text %}
                    <small><p class="help">{{ form.show_on_homepage.help_text|safe }}</p></small>
                {% endif %}
            {% endif %}
            <input type="hidden" name="confirmation" value="1">
            <p>
                <input type="submit" name="action" value="Confirm" autofocus>
            </p>
        {% else %}
            <input type="submit" value="Check and queue" style="margin-top: 5px">
        {% endif %}
    </form>
{% endblock %}
