{% 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 content %}
    <div id="page-list-wrapper" class="{% if items|length > 0 or searchValue is not empty %}panel {% endif %}panel-default">
        {{- include('@MauticCore/Helper/list_toolbar.html.twig', {
            'searchValue': searchValue,
            'action': currentRoute,
            'page_actions': {
                'templateButtons': {
                    'new': security.isGranted('tagManager:tagManager:create')
                },
                'routeBase': 'tagmanager',
                'langVar': 'tag.list'
            },
            'bulk_actions': {
                'langVar': 'tagmanager.tag',
                'routeBase': 'tagmanager',
                'templateButtons': {
                    'delete': permissions['tagManager:tagManager:delete'],
                },
            },
        }) -}}
        <div class="page-list">
            {% if items is defined and items is not empty %}
                <div class="table-responsive">
                    <table class="table table-hover" id="tagsTable">
                        <thead>
                        <tr>
                            {{ include('@MauticCore/Helper/tableheader.html.twig', {
                                'checkall': 'true',
                                'target': '#tagsTable',
                            }) }}
                            {{ 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: 'ri-team-line',
                                                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 %}
                                        {{ customContent('tag.name', _context) }}
                                    </div>
                                    {% set description = item.getDescription() %}
                                    {% if description %}
                                        {{ include('@MauticCore/Helper/description--inline.html.twig', {
                                            'description': description
                                        }) }}
                                    {% endif %}
                                </td>
                                <td class="visible-md visible-lg">
                                    <a size="sm" class="label label-gray" 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 %}
                {% if searchValue is not empty %}
                    {{- include('@MauticCore/Helper/noresults.html.twig') -}}
                {% else %}
                    <div class="mt-80 col-md-offset-2 col-lg-offset-3 col-md-8 col-lg-5 height-auto">
                        {% set childContainer %}
                            <div class="mt-32 mb-md">
                                {% include '@MauticCore/Components/pictogram.html.twig' with {
                                    'pictogram': 'tags',
                                    'size': '80'
                                } %}
                            </div>
                        {% endset %}
            
                        {{ include('@MauticCore/Components/content-block.html.twig', {
                            heading: 'mautic.tagmanager.contentblock.heading',
                            subheading: 'mautic.tagmanager.contentblock.subheading',
                            copy: 'mautic.tagmanager.contentblock.copy',
                            childContainer: childContainer,
                        }) }}
                    </div>
                {% endif %}
            {% endif %}
        </div>
    </div>
    {{ include('@MauticCore/Modules/protip.html.twig', {
        tip: random(['mautic.protip.tags.segmentation', 'mautic.protip.tags.multivalue', 'mautic.protip.tags.creation'])
    }) }}
{% endblock %}