{% set ignoreStandardColumns = ignoreStandardColumns is defined ? ignoreStandardColumns : [] %}
{% set isIndex = tmpl == 'index' ? true : false %}
{% set tmpl = 'list' %}

{% extends isIndex ? '@MauticCore/Default/content.html.twig' : '@MauticCore/Default/raw_output.html.twig' %}

{% block mauticContent %}
    {% if mauticContent is defined %}
        {{ mauticContent }}
    {% endif %}
{% endblock %}

{% if headerTitle is not defined %}
    {% set headerTitle = 'Mautic' %}
{% endif %}
{% set headerTitle = headerTitle|trans %}

{% block headerTitle %}{{ headerTitle }}{% endblock %}

{% block content %}
    {% if isIndex %}
        <div id="page-list-wrapper" class="panel panel-default">
            {{- include('@MauticCore/Helper/list_toolbar.html.twig', {
                'searchValue': searchValue,
                'action': currentRoute,
                'actionRoute': actionRoute,
                'indexRoute': indexRoute,
                'translationBase': translationBase,
                'preCustomButtons': toolBarButtons is defined ? toolBarButtons : null,
                'templateButtons': {
                    'delete': permissions[permissionBase ~ ':delete'],
                },
                'filters': filters is defined ? filters : [],
                'page_actions': {
                    'templateButtons': {
                        'new': permissions[permissionBase ~ ':create'],
                    },
                    'actionRoute': actionRoute,
                    'indexRoute': indexRoute,
                    'translationBase': translationBase,
                }
            }) -}}

            <div class="page-list">
                {{ customContent('content.above', _context) }}
                {{ block('mainContent') }}
                {{ customContent('content.below', _context) }}
            </div>
        </div>
    {% else %}
        {{ block('mainContent') }}
    {% endif %}
{% endblock %}

{% block mainContent %}
    {% if templateVariables is not defined %}
        {% set templateVariables = [] %}
    {% endif %}

    {% if sessionVar is not defined %}
        {% set sessionVar = 'entity' %}
    {% endif %}

    {% if nameAction is not defined %}
        {% set nameAction = 'view' %}
    {% endif %}

    {% if items|length %}
        {% if items is instanceof('\Doctrine\ORM\Tools\Pagination\Paginator') %}
            {% set item = items.getIterator().getArrayCopy() %}
        {% endif %}

        {% set firstItem = items|first %}
        <div class="table-responsive">
            <table class="table table-hover {{ sessionVar }}-list">
                <thead>
                <tr>
                    {% if ignoreStandardColumns is empty %}
                        {{- include('@MauticCore/Helper/tableheader.html.twig',
                            {
                                'checkall'        : 'true',
                                'actionRoute'     : actionRoute,
                                'indexRoute'      : indexRoute,
                                'templateButtons' : {
                                    'delete' : permissions[permissionBase ~ ':deleteown'] is not empty or permissions[permissionBase ~ ':deleteown'] is not empty or permissions[permissionBase ~ ':delete'] is not empty,
                                },
                        }) -}}
                        {{- include(
                            '@MauticCore/Helper/tableheader.html.twig',
                            {
                                'sessionVar' : sessionVar,
                                'orderBy'    : tablePrefix ~ '.name',
                                'text'       : 'mautic.core.name',
                                'class'      : 'col-name',
                                'default'    : true,
                            }
                        ) -}}

                        {% if attribute(firstItem, 'getCategory') is defined %}
                            {{ include('@MauticCore/Helper/tableheader.html.twig',
                                {
                                    'sessionVar' : sessionVar,
                                    'orderBy'    : (categoryTablePrefix is defined ? categoryTablePrefix : 'cat') ~ '.title',
                                    'text'       : 'mautic.core.category',
                                    'class'      : 'visible-md visible-lg col-focus-category',
                            }) }}
                        {% endif %}
                    {% endif %}

                        {% if listHeaders is defined %}
                            {% for header in listHeaders %}
                                {% if header.sessionVar is not defined %}
                                    {% set header = header|merge({'sessionVar': sessionVar}) %}
                                {% endif %}
                            {{- include('@MauticCore/Helper/tableheader.html.twig', header) -}}
                            {% endfor %}
                        {% endif %}

                        {% if ignoreStandardColumns is empty %}
                        {{- include('@MauticCore/Helper/tableheader.html.twig',
                            {
                                'sessionVar' : sessionVar,
                                'orderBy'    : tablePrefix ~ '.id',
                                'text'       : 'mautic.core.id',
                                'class'      : 'visible-md visible-lg col-id',
                        }) -}}
                        {% endif %}

                        {{ customContent('list.headers', _context) }}
                </tr>
                </thead>
                <tbody>
                {% for item in items %}
                    <tr>
                        {% if ignoreStandardColumns is empty %}
                            <td>
                            {{- include('@MauticCore/Helper/list_actions.html.twig',
                                    {
                                        'item'            : item,
                                        'templateButtons' : {
                                            'edit' : attribute(item, 'getCreatedBy') is defined
                                                ?
                                                securityHasEntityAccess(
                                                    permissions[permissionBase ~ ':editown'],
                                                    permissions[permissionBase ~ ':editother'],
                                                    item.getCreatedBy()
                                                )
                                                :
                                                permissions[permissionBase ~ ':edit'],
                                            'clone'  : enableCloneButton is defined ? permissions[permissionBase ~ ':create'] : false,
                                            'delete' : attribute(item, 'getCreatedBy') is defined
                                                ?
                                                securityHasEntityAccess(
                                                    permissions[permissionBase ~ ':deleteown'],
                                                    permissions[permissionBase ~ ':deleteother'],
                                                    item.getCreatedBy()
                                                )
                                                :
                                                permissions[permissionBase ~ ':delete'],
                                            'abtest' : enableAbTestButton is defined ? permissions[permissionBase ~ ':create'] : false,
                                        },
                                        'actionRoute'     : actionRoute,
                                        'indexRoute'      : indexRoute,
                                        'translationBase' : translationBase,
                                        'customButtons'   : customButtons is defined ? customButtons : [],
                            }) -}}
                            </td>
                            <td>
                                <div>
                                    {% if attribute(item, 'isPublished') is defined %}
                                    {{ include(
                                            '@MauticCore/Helper/publishstatus_icon.html.twig',
                                            {'item' : item, 'model' : modelName}
                                        ) }}
                                    {% endif %}
                                    <a data-toggle="ajax" href="{{ path(
                                        actionRoute,
                                        {'objectId' : item.getId(), 'objectAction' : nameAction}
                                    ) }}">
                                        {{ item.getName() }}
                                        {{ customContent('list.name', _context) }}
                                    </a>
                                </div>
                                {% if attribute(item, 'getDescription') is defined and item.getDescription() %}
                                    <div class="text-secondary mt-4">
                                        <small>{{ item.getDescription()|purify }}</small>
                                    </div>
                                {% endif %}
                            </td>
                            {% if attribute(item, 'getCategory') is defined %}
                                <td class="visible-md visible-lg">
                                    {% set category = item.getCategory() %}
                                    {% set catName, color = category
                                        ? category.getTitle()
                                        : 'mautic.core.form.uncategorized'|trans, category ? '#' ~ category.getColor() : '' %}
                                    <div class="d-flex ai-center gap-xs"><span class="label label-gray label-category" style="background: {{ color }};"> </span> {{ catName }}</div>
                                </td>
                            {% endif %}
                        {% endif %}
                        {% if listItemTemplate is defined %}
                            {% set templateVariables = templateVariables|merge({'item': item}) %}
                            {{- include(listItemTemplate, templateVariables) -}}
                        {% endif %}
                        {% if ignoreStandardColumns is empty %}
                            <td class="visible-md visible-lg">{{ item.getId() }}</td>
                        {% endif %}

                        {{ customContent('list.columns', _context) }}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="panel-footer">
            {{- include('@MauticCore/Helper/pagination.html.twig',
                {
                    'totalItems' : totalItems,
                    'page'       : page,
                    'limit'      : limit,
                    'baseUrl'    : path(indexRoute),
                    'sessionVar' : sessionVar,
            }) -}}
        </div>
    {% else %}
        {{- include('@MauticCore/Helper/noresults.html.twig') -}}
    {% endif %}

{% endblock %}