{% load beam_tags %}
{% load i18n %}


{% if fields and not layout %}
    {# FIXME: shouldn't this use fields instead of form.fields? #}
    {% fields_to_layout form.fields as layout %}
{% elif not fields and not layout %}
    {% fields_to_layout form.fields as layout %}
{% endif %}


{% block layout %}
{% for row in layout %}

    {% block row %}
    <div class="row">
        {% block columns %}
        {% for col in row %}

            {% block column %}
            <div class="col">
                {% block fields %}
                {% for field in col %}

                    {% block field %}
                    {% if field.is_html %}

                        {% block html_field_container %}
                        <div class="beam-field beam-html-field beam-field-{{ field.name|default:"html" }}">
                            {% block html_field %}
                                {{ field.content|safe }}
                            {% endblock %}
                        </div>
                        {% endblock %}

                    {% else %}

                        {% get_form_field form field as form_field %}
                        {% if form_field is None %}

                            {% block detail_field_container %}
                            <div class="row">
                                <div class="col-sm-4 text-md-right beam-field-label beam-detail-field-label beam-field-label-{{ field }} ">

                                    {% block detail_field_label %}
                                        <label>
                                            {{ form.instance|field_verbose_name:field|capfirst }}
                                        </label>
                                    {% endblock %}

                                </div>
                                <div class="col-sm-8 beam-field beam-detail-field beam-field-{{ field }} ">

                                    {% block detail_field %}
                                        {% include "beam/partials/detail_field.html" with object=form.instance field=field %}
                                    {% endblock %}

                                </div>
                            </div>
                            {% endblock %}

                        {% elif not form_field.is_hidden %}

                            {% get_url_for_related form_field.field.queryset.model "create" as create_url %}
                            {% get_options form_field.field.queryset.model as related_options %}

                            {% block form_field_container %}
                            <div class="row field-group">
                                <div class="col-sm-4 text-md-right beam-field-label beam-form-field-label beam-field-label-{{ field }}">

                                    {% block form_field_label %}
                                        <label class="col-form-label" for="{{ form_field.auto_id }}">
                                            {{ form_field.label }}<span class="text-muted small">{% if form_field.field.required %}*{% else %}&nbsp;{% endif %}</span>
                                        </label>
                                    {% endblock %}

                                </div>

                                <div class="col-sm-8 beam-field beam-form-field beam-field-{{ field }}"
                                        {% if create_url %}
                                             data-create-text="{% blocktrans with verbose_name=related_options.verbose_name|capfirst %}Create {{ verbose_name }}{% endblocktrans %}"
                                             data-create-url="{{ create_url }}?_popup={{ form_field.auto_id }}"
                                        {% endif %}>

                                    {% block form_field %}
                                        {% include "beam/partials/form_field.html" %}

                                    {% endblock %}

                                </div>
                            </div>
                            {% endblock %}

                        {% endif %}

                    {% endif %}
                    {% endblock %}

                {% endfor %}
                {% endblock %}
            </div>
            {% endblock %}

        {% endfor %}
        {% endblock %}

    </div>
    {% endblock %}

{% endfor %}
{% endblock %}


{% block hidden_fields %}
    {% for hidden in form.hidden_fields %}
        {% block hidden_field %}
            {{ hidden }}
        {% endblock %}
    {% endfor %}
{% endblock %}
