{% extends "wagtailadmin/base.html" %}
{% load i18n %}

{% block titletag %}{{ view.page_title }} {{ view.get_page_subtitle }}{% endblock %}

{% block content %}
    {% include "wagtailadmin/shared/header.html" with title=view.page_title subtitle=view.get_page_subtitle icon=view.header_icon %}

    <div class="nice-padding">
        {% if locale and not locale.language_code_is_valid %}
            <p class="help-block help-warning">
                {% trans "This locale's current language code is not supported. Please choose a new language or delete this locale." %}
            </p>
        {% endif %}

        <form method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
            {% csrf_token %}

            {% block hidden_fields %}
                {% for field in form.hidden_fields %}{{ field }}{% endfor %}

                {% for component, component_instance, component_form in components %}
                    {% for field in component_form.hidden_fields %}
                        {{ field }}
                    {% endfor %}
                {% endfor %}
            {% endblock %}

            <ul class="fields">
                {% block visible_fields %}
                    {% for field in form.visible_fields %}
                        {% include "wagtailadmin/shared/field_as_li.html" %}
                    {% endfor %}
                {% endblock %}
            </ul>

            {% for component, component_instance, component_form in components %}
                <section class="component-form{% if component.required %} component-form--required{% endif %} {% if not component.required and not component_form.enabled.value %} component-form--disabled{% endif %}">
                    <h3>{{ component.heading }}</h3>
                    <p>{{ component.help_text }}</p>

                    <ul class="fields component-form__fields">
                        {% for field in component_form.visible_fields %}
                            {% include "wagtailadmin/shared/field_as_li.html" with li_classes="component-form__fieldname-"|add:field.name %}
                        {% endfor %}
                    </ul>

                    {% if not component.required %}
                        <button type="button" class="button button-small component-form__enable-button">{% trans "Enable "%}</button>
                        <button type="button" class="button button-small no component-form__disable-button">{% trans "Disable "%}</button>
                    {% endif %}
                </section>
            {% endfor %}

            <ul class="fields">
                <li>
                    {% block actions %}
                        <input type="submit" value="{% trans 'Save' %}" class="button" />
                        {% if can_delete %}
                            <a href="{{ view.get_delete_url }}" class="button button-secondary no">{{ view.delete_item_label }}</a>
                        {% endif %}
                    {% endblock %}
                </li>
            </ul>
        </form>
    </div>
{% endblock %}

{% block extra_js %}
    {{ block.super }}
    {{ form.media.js }}

    {% for component_model, component_instance, component_form in components %}
        {{ component_form.media.js }}
    {% endfor %}

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Component enable buttons
            document.querySelectorAll('.component-form__enable-button').forEach(function (enableButton) {
                enableButton.addEventListener('click', function () {
                    var componentForm = enableButton.closest('.component-form');
                    componentForm.classList.remove('component-form--disabled');
                    componentForm.querySelector('.component-form__fieldname-enabled input').checked = true;
                });
            });

            // Component disable buttons
            document.querySelectorAll('.component-form__disable-button').forEach(function (disableButton) {
                disableButton.addEventListener('click', function () {
                    var componentForm = disableButton.closest('.component-form');
                    componentForm.classList.add('component-form--disabled');
                    componentForm.querySelector('.component-form__fieldname-enabled input').checked = false;
                });
            });
        });
    </script>
{% endblock %}

{% block extra_css %}
    {{ block.super }}
    {{ form.media.css }}

    {% for component_model, component_instance, component_form in components %}
        {{ component_form.media.css }}
    {% endfor %}

    <style>
        /* Put a box around component forms */
        .component-form {
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            margin-top: 10px;
            margin-bottom: 20px;
        }

        /* Align component form buttons to the right */
        .component-form button {
            float: right;
        }
        .component-form::after {
            content: ' ';
            display: table;
            clear: both;
        }

        /* Hide the enabled checkbox */
        .component-form__fieldname-enabled {
            display: none;
        }

        /* Hide fields if the form is disabled */
        .component-form--disabled .component-form__fields {
            display: none;
        }

        /* Show enabled button if the form is disabled */
        .component-form__enable-button {
            display: none;
        }

        .component-form--disabled .component-form__enable-button {
            display: block;
        }

        /* Hide disabled button if the form is disabled */
        .component-form--disabled .component-form__disable-button {
            display: none;
        }
    </style>
{% endblock %}
