{#
 # 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.
 #}

{% set type = definition.getType(field) %}

{% if type == 'date' %}
    {{ entity.get(field)|crudlex_formatDate(false) }}

{% elseif type == 'datetime' and entity.get(field) %}
    {{ entity.get(field)|crudlex_formatDateTime(field in ['created_at', 'updated_at']) }}

{% elseif type == 'reference' and entity.get(field) %}
    {% set reference = entity.get(field) %}
    {% set hideId = definition.getSubTypeField(field, type, 'hideId') %}
    {% set noName = 'name' not in reference|keys %}
    {% if reference is iterable %}
        <a href="{{ crud.generateURL('crudShow', {'entity': definition.getSubTypeField(field, 'reference', 'entity'), 'id': reference['id']}) }}" type="button" class="btn btn-default btn-xs btn-success">
            {% if noName %}
                {{ reference['id'] }}
            {% elseif hideId %}
                {{ reference['name'] }}
            {% else %}
                {{ reference['id'] }} :  {{ reference['name'] }}
            {% endif %}
        </a>
    {% else %}
        <span class="text-danger glyphicon glyphicon-remove-circle" aria-hidden="true"></span> {{ reference }}
    {% endif %}

{% elseif type == 'many' %}
    {% set many = entity.get(field) %}
    {% for reference in many  %}
        {% set hideId = definition.getSubTypeField(field, type, 'hideId') %}
        {% set noName = 'name' not in reference|keys %}
        <a href="{{ crud.generateURL('crudShow', {'entity': definition.getSubTypeField(field, 'many', 'entity'), 'id': reference['id']}) }}" type="button" class="btn btn-default btn-xs btn-success">
            {% if noName %}
                {{ reference['id'] }}
            {% elseif hideId %}
                {{ reference['name'] }}
            {% else %}
                {{ reference['id'] }} :  {{ reference['name'] }}
            {% endif %}
        </a>
    {% endfor %}

{% elseif type == 'multiline' %}
    {% if mode == 'list' %}
        {% set value = entity.get(field) %}
        {% if value|length > 27 %}
            <span class="tooltip-crudlex" data-toggle="tooltip" title="{{ entity.get(field) }}">
                <u>{{ entity.get(field)|slice(0, 27)|nl2br }}...</u>
            </span>
        {% else %}
            {{ entity.get(field)|nl2br }}
        {% endif %}
    {% else %}
        {{ entity.get(field)|nl2br }}
    {% endif %}

{% elseif type == 'url' and entity.get(field) %}
    {% if mode == 'list' %}
        {% set value = entity.get(field) %}
        {% if value|length > 12 %}
            <span class="tooltip-crudlex" data-toggle="tooltip" title="{{ entity.get(field) }}">
                <u><a href="{{ entity.get(field) }}" target="_blank">{{ entity.get(field)|crudlex_basename|slice(0, 12) }}...</a></u>
            </span>
        {% else %}
            <a href="{{ entity.get(field) }}" target="_blank">{{ entity.get(field)|crudlex_basename }}</a>
        {% endif %}
    {% else %}
        <a href="{{ entity.get(field) }}" target="_blank">{{ entity.get(field) }}</a>
    {% endif %}

{% elseif type == 'file' %}
    {% if entity.get(field) %}
        {% set url = crud.generateURL('crudRenderFile', {entity: crudEntity, field: field, id: entity.get('id')}) %}
        {% if mode == 'list' %}
            {% set value = entity.get(field) %}
            {% set fieldBasename = entity.get(field)|crudlex_basename %}
            {% if value|length > 12 %}
                <span class="tooltip-crudlex" data-toggle="tooltip" title="{{ entity.get(field) }}">
                    <u><a href="{{ url }}" target="_blank">{{ fieldBasename|slice(0, 12) }}...</a></u>
                </span>
            {% else %}
                <a href="{{ url }}" target="_blank">{{ fieldBasename }}</a>
            {% endif %}
        {% else %}
            <a href="{{ url }}" target="_blank">{{ entity.get(field) }}</a>

            {% if not definition.getField(field, 'required', false) %}
                <form style="display: inline-block" onSubmit="return confirm('{{ 'crudlex.file.delete'|trans({'%file%': entity.get(field)}) }}');" method="POST" action="{{ crud.generateURL('crudDeleteFile', {entity: crudEntity, field: field, id: entity.get('id')}) }}">
                    <input type="submit" class="btn btn-default btn-xs btn-danger" value="{{ 'crudlex.button.delete'|trans }}" />
                </form>
            {% endif %}
        {% endif %}
    {% endif %}

{% elseif type == 'boolean' %}
    <span class="glyphicon glyphicon-{{ entity.get(field) ? 'ok' : 'remove' }}" aria-hidden="true"></span>

{% elseif type == 'float' %}
    {{ entity.get(field)|crudlex_float }}

{% elseif type == 'wysiwym' %}
    {{ entity.get(field)|raw }}

{% else %}
    {{ entity.get(field) }}
{% endif %}
