{# Override tmpl to "list" because otherwise AJAX calls render the whole content instead of just the raw output #}
{% 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.webhook.webhooks{% endtrans %}{% endblock %}
{% block mauticContent %}mauticWebhook{% endblock %}

{% block actions %}
    {{- include(
        '@MauticCore/Helper/page_actions.html.twig', {
            templateButtons: {
                'new': permissions['webhook:webhooks:create']
            },
            routeBase: 'webhook'
    }) -}}
{% endblock %}

{% block content %}
{% if isIndex %}
<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,
        'bulk_actions': {
            'routeBase': 'webhook',
            'templateButtons': {
                'delete': (permissions['webhook:webhooks:deleteown'] or permissions['webhook:webhooks:deleteother'])
            }
        },
        'quickFilters': [
            {
                'search': 'mautic.core.searchcommand.ispublished',
                'label': 'mautic.core.form.active',
                'tooltip': 'mautic.core.searchcommand.ispublished.description',
                'icon': 'ri-check-line'
            },
            {
                'search': 'mautic.core.searchcommand.isunpublished',
                'label': 'mautic.core.form.inactive',
                'tooltip': 'mautic.core.searchcommand.isunpublished.description',
                'icon': 'ri-close-line'
            },
            {
                'search': 'mautic.core.searchcommand.ismine',
                'label': 'mautic.core.searchcommand.ismine.label',
                'tooltip': 'mautic.core.searchcommand.ismine.description',
                'icon': 'ri-user-line'
            }
        ]
    }) }}
    <div class="page-list">
        {{ block('listResults') }}
    </div>
</div>
{% else %}
    {{ block('listResults') }}
{% endif %}
{% endblock %}

{% block listResults %}
{% if items is defined and items is not empty %}
    <div class="table-responsive panel-collapse pull out webhook-list">
        <table class="table table-hover webhook-list" id="webhookTable">
            <thead>
            <tr>
                {{- include(
                    '@MauticCore/Helper/tableheader.html.twig', {
                        checkall: 'true',
                        target: '#webhookTable'
                }) -}}
                {{- include(
                    '@MauticCore/Helper/tableheader.html.twig', {
                        sessionVar: 'mautic_webhook',
                        orderBy: 'e.name',
                        text: 'mautic.core.name',
                        class: 'col-webhook-name',
                        default: true
                }) -}}
                {{- include(
                    '@MauticCore/Helper/tableheader.html.twig', {
                        sessionVar: 'mautic_webhook',
                        orderBy: 'c.title',
                        text: 'mautic.core.category',
                        class: 'visible-md visible-lg col-webhook-category',
                }) -}}
                {{- include(
                    '@MauticCore/Helper/tableheader.html.twig', {
                        sessionVar: 'mautic_webhook',
                        orderBy: 'e.webhookUrl',
                        text: 'mautic.webhook.webhook_url',
                        class: 'col-webhook-id visible-md visible-lg'
                }) -}}
                {{- include(
                    '@MauticCore/Helper/tableheader.html.twig', {
                        sessionVar: 'mautic_webhook',
                        orderBy: 'e.id',
                        text: 'mautic.core.id',
                        class: 'col-webhook-id visible-md visible-lg'
                }) -}}
            </tr>
            </thead>
            <tbody>
                {% for item in items %}
                <tr>
                    <td>
                        {{- include(
                            '@MauticCore/Helper/list_actions.html.twig', {
                                item: item,
                                templateButtons: {
                                    edit: securityHasEntityAccess(
                                        permissions['webhook:webhooks:editown'],
                                        permissions['webhook:webhooks:editother'],
                                        item.getCreatedBy()
                                    ),
                                    clone: permissions['webhook:webhooks:create'],
                                    delete: securityHasEntityAccess(
                                        permissions['webhook:webhooks:deleteown'],
                                        permissions['webhook:webhooks:deleteother'],
                                        item.getCreatedBy()
                                    )
                                },
                                routeBase: 'webhook'
                        }) -}}
                    </td>
                    <td>
                        <div>
                            {{- include(
                                '@MauticCore/Helper/publishstatus_icon.html.twig', {
                                    item: item,
                                    model: 'webhook'
                                }
                            ) }}
                            <a data-toggle="ajax" href="{{ path(
                                'mautic_webhook_action',
                                { objectId: item.getId(), objectAction: 'view' }
                            ) }}">
                                {{ item.getName() }}
                            </a>
                            {{ include('@MauticCore/Helper/description--inline.html.twig', {
                                'description': item.getDescription()
                            }) }}
                        </div>
                    </td>
                    <td class="visible-md visible-lg">
                        {{ include('@MauticCore/Modules/category--expanded.html.twig', {'category': item.getCategory()}) }}
                    </td>
                    <td class="visible-md visible-lg">{{ item.getWebhookUrl() }}</td>
                    <td class="visible-md visible-lg">{{ item.getId() }} </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="panel-footer">
        {{- include(
            '@MauticCore/Helper/pagination.html.twig', {
                totalItems: items|length,
                page: page,
                limit: limit,
                menuLinkId: 'mautic_webhook_index',
                baseUrl: path('mautic_webhook_index'),
                sessionVar: 'mautic_webhook'
            }
        ) }}
    </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': 'event--streams',
                        'size': '80'
                    } %}
                </div>
            {% endset %}

            {{ include('@MauticCore/Components/content-block.html.twig', {
                heading: 'mautic.webhook.contentblock.heading',
                subheading: 'mautic.webhook.contentblock.subheading',
                copy: 'mautic.webhook.contentblock.copy',
                childContainer: childContainer,
            }) }}
        </div>
    {% endif %}
{% endif %}
{% endblock %}
