{% extends "wagtailadmin/base.html" %}
{% load i18n %}
{% load l10n %}
{% load wagtailadmin_tags %}
{% block titletag %}{% trans "Add multiple documents" %}{% endblock %}
{% block extra_css %}
    {{ block.super }}

    {{ form_media.css }}

    <link rel="stylesheet" href="{% versioned_static 'wagtaildocs/css/add-multiple.css' %}" type="text/css" />
{% endblock %}

{% block content %}
    {% trans "Add documents" as add_str %}
    {% include "wagtailadmin/shared/header.html" with title=add_str icon="doc-full-inverse" %}

    <div class="nice-padding">
        <div class="drop-zone">
            <p>{% trans "Drag and drop documents into this area to upload immediately." %}</p>
            <p>{{ help_text }}</p>

            <form action="{% url 'wagtaildocs:add_multiple' %}" method="POST" enctype="multipart/form-data">
                <div class="replace-file-input">
                    <button class="button bicolor button--icon">{% icon name="plus" wrapped=1 %}{% trans "Or choose from your computer" %}</button>
                    <input id="fileupload" type="file" name="files[]" data-url="{% url 'wagtaildocs:add_multiple' %}" multiple>
                </div>
                {% csrf_token %}
                {% if collections %}
                    <div class="field choice_field select">
                        <label for="id_adddocument_collection">{% trans "Add to collection:" %}</label>
                        <div class="field-content">
                            <select id="id_adddocument_collection" name="collection">
                                {% minimum_collection_depth collections as min_depth %}
                                {% for collection in collections %}
                                    <option value="{{ collection.id|unlocalize }}">
                                        {% if request.user.is_superuser %}
                                            {# Superuser may see all collections. #}
                                            {% format_collection collection %}
                                        {% else %}
                                            {# Pass the minimum depth of the permitted collections, since user isn't a superuser #}
                                            {% format_collection collection min_depth %}
                                        {% endif %}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                {% endif %}
            </form>
        </div>

        <div id="overall-progress" class="progress progress-secondary">
            <div class="bar" style="width: 0%;">0%</div>
        </div>

        <ul id="upload-list" class="upload-list multiple"></ul>
    </div>

    <script id="upload-list-item" type="text/template">
        <li class="row">
            <div class="left col3">
                <div class="preview">
                    <div class="progress">
                        <div class="bar" style="width: 0%;"></div>
                    </div>
                </div>
            </div>
            <div class="right col9">
                <p class="status-msg success">{% trans "Upload successful. Please update this document with a more appropriate title, if necessary. You may also delete the document completely if the upload wasn't required." %}</p>
                <p class="status-msg failure">{% trans "Sorry, upload failed." %}</p>
                <p class="status-msg update-success">{% trans "Document updated." %}</p>
                <p class="status-msg failure error_messages"></p>
            </div>
        </li>
    </script>
{% endblock %}

{% block extra_js %}
    {{ block.super }}

    {{ form_media.js }}

    <!-- this exact order of plugins is vital -->
    <script src="{% versioned_static 'wagtailadmin/js/vendor/jquery.iframe-transport.js' %}"></script>
    <script src="{% versioned_static 'wagtailadmin/js/vendor/jquery.fileupload.js' %}"></script>
    <script src="{% versioned_static 'wagtailadmin/js/vendor/jquery.fileupload-process.js' %}"></script>
    <script src="{% versioned_static 'wagtailadmin/js/vendor/tag-it.js' %}"></script>

    <!-- Main script -->
    <script src="{% versioned_static 'wagtaildocs/js/add-multiple.js' %}"></script>

    {% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
    <script>
        window.fileupload_opts = {
            max_title_length: {{ max_title_length|stringformat:"s"|default:"null" }}, //numeric format
            simple_upload_url: "{% url 'wagtaildocs:add' %}"
        }
        window.tagit_opts = {
            autocomplete: {source: "{{ autocomplete_url|addslashes }}"}
        };
    </script>
{% endblock %}
