{% extends '@MauticCore/Default/content.html.twig' %}

{% block indexButton %}
{{- include(
    '@MauticCore/Helper/page_actions.html.twig', {
        item: item,
        templateButtons: {
            'close': securityHasEntityAccess(
                permissions['webhook:webhooks:viewown'],
                permissions['webhook:webhooks:viewother'],
                item.getCreatedBy()
            ),
        },
        routeBase: 'webhook',
        'targetLabel': 'mautic.webhook.webhooks'|trans
}) -}}
{% endblock %}

{% block headerTitle %}{{ item.getName() }}{% endblock %}
{% block mauticContent %}mauticWebhook{% endblock %}

{% block actions %}
    {{- include(
        '@MauticCore/Helper/page_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'
    }) -}}
{% endblock %}

{% block content %}
<!-- start: box layout -->
<div class="box-layout">
    <!-- left section -->
    <div class="col-md-9 height-auto">
        {% include '@MauticCore/Helper/description.html.twig' with {'description': item.description} %}
        <div class="pa-md">
            <div class="row">
                <div class="col-md-12">
                    {% set hookLog = item.getLimitedLogs() %}
                    {% if hookLog is empty %}
                        <div class="alert alert-warning col-md-6 col-md-offset-3 mt-md" style="white-space: normal;">
                            <h4>
                                {% trans %}mautic.webhook.no.logs{% endtrans %}
                            </h4>
                            <p>
                                {% trans %}mautic.webhook.no.logs_desc{% endtrans %}
                            </p>
                        </div>
                    {% else %}
                        <table class="table table-responsive">
                            <thead>
                                <tr>
                                    <th>
                                        {% trans %}mautic.core.id{% endtrans %}
                                    </th>
                                    <th>
                                        {% trans %}mautic.webhook.status{% endtrans %}
                                    </th>
                                    <th>
                                        {% trans %}mautic.webhook.note{% endtrans %}
                                    </th>
                                    <th>
                                        {% trans %}mautic.webhook.runtime{% endtrans %}
                                    </th>
                                    <th>
                                        {% trans %}mautic.core.date.added{% endtrans %}
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for log in hookLog %}
                                    <tr>
                                        <td>{{ log.getId() }}</td>
                                        <td>{{- include(
                                            '@MauticWebhook/Helper/labelcode.html.twig', {
                                                code: log.getStatusCode()
                                            }
                                        ) }}</td>
                                        <td>{{ log.getNote() is not empty ? log.getNote() : 'mautic.webhook.webhook.logs.empty.response'|trans }}</td>
                                        <td>{{ log.getRuntime() }} s</td>
                                        <td>{{ dateToFull(log.getDateAdded()) }}</td>
                                    </tr>
                                {% endfor  %}
                            </tbody>
                        </table>
                        <div class="alert alert-info col-md-6 col-md-offset-3 mt-md">
                            <h4>
                                {% trans %}mautic.webhook.webhook.logs.title{% endtrans %}
                            </h4>
                            <p>
                                {% trans %}mautic.webhook.webhook.logs.desc{% endtrans %}
                            </p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- right section -->
    <div class="col-md-3 bdr-l height-auto">
        <!-- preview URL -->
        <div class="panel shd-none bdr-rds-0 bdr-w-0 mt-sm mb-0">
            <div class="panel-heading">
                <div class="panel-title">{% trans %}mautic.webhook.webhook_url{% endtrans %}</div>
            </div>
            <div class="panel-body pt-xs">
                <div class="input-group">
                    <input onclick="this.setSelectionRange(0, this.value.length);" type="text" class="form-control" readonly
                           value="{{ item.getWebhookUrl()|escape('html_attr') }}" />
                    <span class="input-group-btn">
                        {% include '@MauticCore/Helper/button.html.twig' with {
                            buttons: [
                                {
                                    label: 'mautic.core.open_link',
                                    variant: 'ghost',
                                    icon_only: true,
                                    icon: 'ri-external-link-line',
                                    onclick: 'window.open("' ~ item.getWebhookUrl()|escape('html_attr') ~ '", "_blank");',
                                }
                            ]
                        } %}
                    </span>
                </div>
            </div>

            <hr class="hr-w-2" style="width:50%">

            <!-- recent activity -->
            {{- include(
                '@MauticCore/Helper/recentactivity.html.twig', {
                    logs: logs
                }
            ) }}
        </div>
    </div>
    <!--/ right section -->
</div>
{% endblock %}
