{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% block content %}
    <h1 class="page-header">{{ 'marketing.abandoned_cart.admin.title'|trans }}</h1>

    {{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
        <div class="panel panel-info">
            <div class="panel-heading">{{ 'marketing.abandoned_cart.configuration.title'|trans }}</div>
            <div class="panel-body">
                {{ form_row(form.enabled) }}
                {% include '@CpCreationVitiCore/admin/marketing/_email_title_modal.html.twig' with {
                    modal_id: 'abandonedCartEmailTitleModal',
                    title_key: 'marketing.abandoned_cart.configuration.email_content_title',
                    help_key: 'marketing.abandoned_cart.configuration.email_content_help',
                    translations_form: form.translations,
                    fields: ['emailTitle', 'emailSubject', 'emailPreheader'],
                } only %}
                <p>{{ 'marketing.abandoned_cart.admin.reminder_count'|trans({'%count%': reminder_count}) }}</p>
            </div>
            <div class="panel-footer">
                <a class="btn btn-danger btn-rounded" href="{{ path('admin_marketing') }}">
                    {{ 'marketing.common.back'|trans }}
                </a>
                <button type="submit" class="btn btn-primary btn-rounded">
                    {{ 'marketing.common.save'|trans }}
                </button>
                <a class="btn btn-info btn-rounded" href="{{ path('admin_marketing_abandoned_cart_reminders') }}">
                    {{ 'marketing.abandoned_cart.admin.view_reminders'|trans }}
                </a>
                {% include '@CpCreationVitiCore/admin/marketing/_test_email_modal.html.twig' with {
                    modal_id: 'abandonedCartTestEmailModal',
                    action: path('admin_marketing_abandoned_cart_test_email'),
                    recipient: test_email_recipient,
                    csrf_token_id: 'marketing-abandoned-cart-test-email',
                    show_modal: false,
                } only %}
            </div>
        </div>
    {{ form_end(form) }}

    {% include '@CpCreationVitiCore/admin/marketing/_test_email_modal.html.twig' with {
        modal_id: 'abandonedCartTestEmailModal',
        action: path('admin_marketing_abandoned_cart_test_email'),
        recipient: test_email_recipient,
        csrf_token_id: 'marketing-abandoned-cart-test-email',
        show_button: false,
    } only %}

    <div class="panel panel-default">
        <div class="panel-heading clearfix">
            <strong>{{ 'marketing.abandoned_cart.rule.list_title'|trans }}</strong>
            <a class="btn btn-primary btn-sm pull-right" href="{{ path('admin_marketing_abandoned_cart_rule_new') }}">
                <i class="fas fa-plus"></i>
                {{ 'marketing.abandoned_cart.rule.add'|trans }}
            </a>
        </div>
        <div class="panel-body">
            <div class="alert alert-info">
                {{ 'marketing.abandoned_cart.rule.list_help'|trans }}
            </div>
        </div>

        {% if rules is empty %}
            <div class="panel-body">
                <div class="alert alert-warning">{{ 'marketing.abandoned_cart.rule.empty'|trans }}</div>
            </div>
        {% else %}
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>{{ 'marketing.abandoned_cart.rule.position'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.name'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.delay_hours'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.conditions'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.benefit'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.enabled'|trans }}</th>
                            <th>{{ 'marketing.abandoned_cart.rule.actions'|trans }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rule in rules %}
                            <tr>
                                <td>{{ rule.position }}</td>
                                <td>{{ rule.name }}</td>
                                <td>{{ 'marketing.abandoned_cart.rule.delay_value'|trans({'%hours%': rule.delayHours}) }}</td>
                                <td>
                                    {% if rule.minSubtotalTTC is not null %}
                                        <div>{{ 'marketing.abandoned_cart.rule.condition_min'|trans({'%amount%': rule.minSubtotalTTC|price_format}) }}</div>
                                    {% endif %}
                                    {% if rule.maxSubtotalTTC is not null %}
                                        <div>{{ 'marketing.abandoned_cart.rule.condition_max'|trans({'%amount%': rule.maxSubtotalTTC|price_format}) }}</div>
                                    {% endif %}
                                    {% if rule.countryCodes is not empty %}
                                        <div>{{ 'marketing.abandoned_cart.rule.condition_countries'|trans({'%countries%': rule.countryCodes|join(', ')}) }}</div>
                                    {% endif %}
                                    <div>{{ ('marketing.abandoned_cart.customer.' ~ rule.customerType)|trans }}</div>
                                </td>
                                <td>
                                    {% if rule.discountType == constant('CpCreation\\VitiCore\\Marketing\\Model\\AbandonedCartRule::DISCOUNT_FIXED') %}
                                        {{ 'marketing.abandoned_cart.discount.fixed_value'|trans({'%amount%': rule.discountAmountTTC|price_format}) }}
                                    {% elseif rule.discountType == constant('CpCreation\\VitiCore\\Marketing\\Model\\AbandonedCartRule::DISCOUNT_PERCENT') %}
                                        {{ 'marketing.abandoned_cart.discount.percent_value'|trans({'%percent%': rule.discountPercent}) }}
                                    {% else %}
                                        {{ ('marketing.abandoned_cart.discount.' ~ rule.discountType)|trans }}
                                    {% endif %}
                                    {% if rule.createsCoupon and rule.couponOnlyWithoutExistingDiscount %}
                                        <div class="text-muted">
                                            {{ 'marketing.abandoned_cart.rule.coupon_without_existing_discount_short'|trans }}
                                        </div>
                                    {% endif %}
                                    <div class="text-muted">
                                        {{ 'marketing.abandoned_cart.rule.price_group_value'|trans({
                                            '%group%': rule.priceGroup.name
                                        }) }}
                                    </div>
                                </td>
                                <td>
                                    <span class="label {{ rule.enabled ? 'label-success' : 'label-default' }}">
                                        {{ (rule.enabled ? 'marketing.common.enabled' : 'marketing.common.disabled')|trans }}
                                    </span>
                                </td>
                                <td>
                                    <a class="btn btn-primary btn-xs" href="{{ path('admin_marketing_abandoned_cart_rule_edit', {id: rule.id}) }}">
                                        {{ 'marketing.abandoned_cart.rule.edit'|trans }}
                                    </a>
                                    <form method="post" action="{{ path('admin_marketing_abandoned_cart_rule_delete', {id: rule.id}) }}" style="display:inline" onsubmit="return confirm('{{ 'marketing.abandoned_cart.rule.delete_confirm'|trans|e('js') }}');">
                                        <input type="hidden" name="_token" value="{{ csrf_token('delete-abandoned-cart-rule-' ~ rule.id) }}">
                                        <button class="btn btn-danger btn-xs" type="submit">{{ 'marketing.abandoned_cart.rule.delete'|trans }}</button>
                                    </form>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% endif %}
    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script src="{{ asset('bundles/cpcreationviticore/js/admin/character-counter.js?v20260723') }}"></script>
{% endblock %}
