{#
 # This file is part of the CRUDlex package.
 #
 # (c) Philip Lehmann-Böhm <philip@philiplb.de>
 #
 # For the full copyright and license information, please view the LICENSE
 # file that was distributed with this source code.
 #}

{%extends layout %}

{% block content %}

    {% set fields = crudData.getDefinition().getEditableFieldNames() %}
    <a id="crudBtnList" href="{{ crud.generateURL('crudList', {'entity': crudEntity}) }}" type="button" class="btn btn-default btn-success btn-crudlex">{{ 'crudlex.button.list'|trans }}</a>
    {% if mode == 'edit' %}
        <a id="crudBtnShow" href="{{ crud.generateURL('crudShow', {'entity': crudEntity, 'id': entity.get('id')}) }}" type="button" class="btn btn-default btn-success btn-crudlex">{{ 'crudlex.button.show'|trans }}</a>
        <a id="crudBtnNew" href="{{ crud.generateURL('crudCreate', {'entity': crudEntity}) }}" class="btn btn-success btn-crudlex">{{ 'crudlex.button.new'|trans }}</a>

    {% endif %}
    <form id="crudForm" class="{{ mode == 'edit' ? 'crudFormEdit' : 'crudFormCreate' }}" role="form" enctype="multipart/form-data" method="POST" action="{{ mode == 'create' ? crud.generateURL('crudCreate', {'entity': crudEntity}) : crud.generateURL('crudEdit', {'entity': crudEntity, 'id': entity.get('id')}) }}">

        {% if mode == 'edit' %}
            {% if crudData.getDefinition().hasOptimisticLocking() %}
                <input type="hidden" name="version" value="{{ entity.get('version') }}" />
            {% endif %}
            <div class="form-group">
                <strong>{{ crudData.getDefinition().getFieldLabel('id') }}: </strong> {{ entity.get('id') }}
            </div>
            <div class="form-group">
                <strong>{{ crudData.getDefinition().getFieldLabel('created_at') }}: </strong> {{ entity.get('created_at') }}
            </div>
            <div class="form-group">
                <strong>{{ crudData.getDefinition().getFieldLabel('updated_at') }}: </strong> {{ entity.get('updated_at') }}
            </div>
        {% endif %}

        {% for field in fields %}

            {% set type = crudData.getDefinition().getType(field) %}
            {% set class = type == 'boolean' ? 'checkbox' : 'form-group' %}
            <div id="crudField{{ field }}" class="{{ class }} {{ field in fieldErrors|keys ? 'has-error' : '' }}">
                {% include crud.getTemplate('template', type ~ 'Field', crudEntity) with {
                    crud: crud,
                    field: field,
                    fieldNamePrefix: '',
                    crudData: crudData,
                    value: entity.get(field),
                    annotations: true,
                    crudEntity: crudEntity
                } only %}
            </div>

        {% endfor %}

        <button type="submit" id="crudBtnSubmit" class="btn btn-default btn-success">{{ 'crudlex.button.submit'|trans }}</button>
    </form>
{% endblock %}
