{% set isIndex = tmpl == 'index' ? true : false %}
{% set tmpl = 'list' %}

{% extends isIndex ? '@MauticCore/Default/content.html.twig' : '@MauticCore/Default/raw_output.html.twig' %}
{% block headerTitle %}{% trans %}mautic.tagmanager.tag.header.index{%  endtrans %}{% endblock %}
{% block mauticContent %}tags{% endblock %}


{% if nameGetter is not defined %}
    {% set nameGetter = 'getTag' %}
{% endif %}

{% set listCommand = 'mautic.tagmanager.tag.searchcommand.list'|trans %}

{% block actions %}
    {{- include(
        '@MauticCore/Helper/page_actions.html.twig',
        {
            'templateButtons': {
            'new': security.isGranted('tagManager:tagManager:create')
        },
            'routeBase': 'tagmanager',
            'langVar': 'tag.list'
        }
    ) -}}
{% endblock %}

{% block content %}
    <div class="panel panel-default bdr-t-wdh-0">
        <div class="page-list">
        {{- include('@MauticCore/Helper/list_toolbar.html.twig', {
            'searchValue' : searchValue,
            'searchHelp'  : 'mautic.lead.list.help.searchcommands',
            'action'      : currentRoute,
        }) -}}
            {% if items is defined and items is not empty %}
                <div class="table-responsive">
                    <table class="table table-hover table-striped table-bordered" id="tagsTable">
                        <thead>
                        <tr>
                            {{ include(
                                '@MauticCore/Helper/tableheader.html.twig',
                                {
                                    'checkall': 'true',
                                    'target': '#tagsTable',
                                    'langVar': 'tagmanager.tag',
                                    'routeBase': 'tagmanager',
                                    'templateButtons': {
                                        'delete': permissions['tagManager:tagManager:delete'],
                                    }
                                }) }}
                            {{ include(
                                '@MauticCore/Helper/tableheader.html.twig',
                                {
                                    'sessionVar': 'tags',
                                    'orderBy'   : 'lt.tag',
                                    'text'      : 'mautic.core.name',
                                    'class'     : 'col-tag-name',
                                }
                            ) }}

                            {{ include(
                                '@MauticCore/Helper/tableheader.html.twig',
                                {
                                    'sessionVar' : 'tags',
                                    'text'       : 'mautic.lead.list.thead.leadcount',
                                    'class'      : 'visible-md visible-lg col-tag-leadcount',
                                }
                            ) }}

                            {{ include(
                                '@MauticCore/Helper/tableheader.html.twig',
                                {
                                    'sessionVar' : 'tags',
                                    'orderBy'    : 'lt.id',
                                    'text'       : 'mautic.core.id',
                                    'class'      : 'visible-md visible-lg col-tag-id',
                                }
                            ) }}
                        </tr>
                        </thead>
                        <tbody>
                        {% for item in items %}
                            <tr>
                                <td>
                                    {{- include('@MauticCore/Helper/list_actions.html.twig', {
                                        item: item,
                                        templateButtons: {
                                            edit: permissions['tagManager:tagManager:edit'],
                                            delete: permissions['tagManager:tagManager:delete'],
                                        },
                                        routeBase: 'tagmanager',
                                        langVar: 'tagmanager.tag',
                                        nameGetter: nameGetter,
                                        custom: {
                                            0: {
                                                attr : {
                                                    'data-toggle': 'ajax',
                                                    href: '',
                                                },
                                                icon: 'fa-users',
                                                label: 'mautic.lead.list.view_contacts'
                                            }
                                        }
                                    }) -}}
                                </td>
                                <td>
                                    <div>
                                        {% if permissions['tagManager:tagManager:edit'] %}
                                            <a href="{{ path('mautic_tagmanager_action', {
                                                objectAction: 'view',
                                                objectId: item.getId()
                                            }) }}" data-toggle="ajax">
                                                {{ item.getTag() }}
                                            </a>
                                        {% else %}
                                            {{ item.getTag() }}
                                        {% endif %}
                                    </div>
                                    {% set description = item.getDescription() %}
                                    {% if description %}
                                        <div class="text-muted mt-4">
                                            <small>{{ description|purify }}</small>
                                        </div>
                                    {% endif %}
                                </td>
                                <td class="visible-md visible-lg">
                                    <a class="label label-primary" href="{{-  path('mautic_contact_index', {
                                        'search' : "#{'mautic.tagmanager.lead.searchcommand.list'|trans}:\"#{item.getTag()}\"",
                                    }) -}}" data-toggle="ajax" {{- 0 == tagsCount[item.getId()] ? 'disabled=disabled' : '' -}}>
                                    {{- 'mautic.lead.list.viewleads_count'|trans({'%count%': tagsCount[item.getId()]}) -}}
                                    </a>
                                </td>
                                <td class="visible-md visible-lg">{{ item.getId() }}</td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                    <div class="panel-footer">
                        {{- include('@MauticCore/Helper/pagination.html.twig', {
                            'totalItems' : items|length,
                            'page'       : page,
                            'limit'      : limit,
                            'baseUrl'    : path('mautic_tagmanager_index'),
                            'sessionVar' : 'tagmanager',
                        }) -}}
                    </div>
                </div>
            {% else %}
                {{ include('@MauticCore/Helper/noresults.html.twig') }}
            {% endif %}
        </div>
    </div>
{% endblock %}