{% set is_paid = is_paid|default(cart.cartCheckout and cart.cartCheckout.payedAt) %}
{% set mail_translation_prefix = mail_translation_prefix|default(is_paid ? 'order.paid_mail' : 'order.offline_mail') %}
{% set payment_method = cart.paymentMethod ?: (cart.cartCheckout ? cart.cartCheckout.paymentMethod : null) %}
{% set payment_configuration = payment_method ? payment_method.configuration : {} %}
{% set configured_name = payment_configuration['name'] ?? null %}
{% set payment_text = payment_configuration['text'] ?? null %}
{% set pricing = cart.pricing %}
{% set requires_order_reference_for_payment = payment_method and payment_method.method in ['check', 'bankTransfert', 'banktransfert'] %}
{% if configured_name %}
    {% set payment_method_label = '.' in configured_name ? configured_name|trans({}, null, locale|default('fr')) : configured_name %}
{% elseif payment_method %}
    {% set payment_method_label = payment_method.checkoutOnlineMethod
        ? 'checkout_design.payment_card'|trans({}, null, locale|default('fr'))
        : (payment_method.method in ['bankTransfert', 'banktransfert']
            ? 'checkout_design.payment_banktransfert'|trans({}, null, locale|default('fr'))
            : 'checkout_design.payment_check'|trans({}, null, locale|default('fr'))) %}
{% else %}
    {% set payment_method_label = null %}
{% endif %}
{% set reference_intro_translation_parameters = {
    '%payment_method%': payment_method_label ?: '-',
    '%reference%': reference
} %}
{% extends 'mail/_layout.html.twig' %}

{% block mail_subject %}{{ (mail_translation_prefix ~ '.subject_mail')|trans({'%company%': company, '%document%': document_label_lower }, null, locale|default('fr')) }}{% endblock %}
{% block mail_eyebrow %}{{ (mail_translation_prefix ~ '.eyebrow')|trans({}, null, locale|default('fr')) }}{% endblock %}
{% block mail_heading %}{{ (mail_translation_prefix ~ '.heading')|trans({}, null, locale|default('fr')) }}{% endblock %}

{% block mail_body %}
    <p style="margin:0 0 16px;">{{ (mail_translation_prefix ~ '.intro')|trans({}, null, locale|default('fr')) }}</p>
    <p style="margin:0 0 24px;">{{ (mail_translation_prefix ~ '.reference_intro')|trans(reference_intro_translation_parameters, null, locale|default('fr')) }}</p>

    <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="width:100%; margin:0 0 24px; border:1px solid #eadfce; background:#fcf7f0;">
        <tr>
            <td class="email-block" style="padding:18px 22px;">
                <div style="margin:0 0 6px; font-size:11px; letter-spacing:0.18em; text-transform:uppercase; color:#8a6a2f;">{{ (mail_translation_prefix ~ '.reference_label')|trans({}, null, locale|default('fr')) }}</div>
                <div style="font-family:Georgia, 'Times New Roman', serif; font-size:28px; line-height:1.2; color:#3f2f24;">{{ reference }}</div>
            </td>
        </tr>
    </table>

    <p style="margin:0 0 16px;">{{ (mail_translation_prefix ~ '.attachment_notice')|trans({'%document%': document_label_lower }, null, locale|default('fr')) }}</p>

    <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="width:100%; margin:0 0 24px; border:1px solid #eadfce; background:#fffaf5;">
        <tr>
            <td class="email-block" style="padding:20px 22px;">
                <div style="margin:0 0 8px; font-size:11px; letter-spacing:0.18em; text-transform:uppercase; color:#8a6a2f;">{{ 'checkout_design.total_ttc'|trans({}, null, locale|default('fr')) }}</div>
                <div style="margin:0 0 14px; font-family:Georgia, 'Times New Roman', serif; font-size:28px; line-height:1.2; color:#3f2f24;">
                    {{ pricing ? pricing.totalTTC|price_format : cart.total|price_format }} €
                </div>

                {% if is_paid %}
                    <div style="margin:0 0 8px; font-size:11px; letter-spacing:0.18em; text-transform:uppercase; color:#8a6a2f;">{{ (mail_translation_prefix ~ '.payment_paid_heading')|trans({}, null, locale|default('fr')) }}</div>
                    <p style="margin:0;">{{ (mail_translation_prefix ~ '.payment_paid_text')|trans({
                            '%payment_method%': payment_method_label ?: '-',
                            '%date%': cart.cartCheckout.payedAt|date('d/m/Y H:i')
                        }, null, locale|default('fr')) }}</p>
                {% else %}
                    <div style="margin:0 0 8px; font-size:11px; letter-spacing:0.18em; text-transform:uppercase; color:#8a6a2f;">{{ (mail_translation_prefix ~ '.payment_pending_heading')|trans({}, null, locale|default('fr')) }}</div>
                    <p style="margin:0 0 14px;">{{ (mail_translation_prefix ~ '.payment_pending_text')|trans({
                            '%payment_method%': payment_method_label ?: '-'
                        }, null, locale|default('fr')) }}</p>
                    {% if requires_order_reference_for_payment %}
                        <p style="margin:0 0 14px; color:#3f2f24; font-weight:700;">{{ (mail_translation_prefix ~ '.offline_reference_notice')|trans({
                                '%reference%': reference
                            }, null, locale|default('fr')) }}</p>
                    {% endif %}
                    {% if payment_text %}
                        <div style="margin:0; color:#6e5848;">{{ payment_text|raw }}</div>
                    {% else %}
                        <p style="margin:0;">{{ 'checkout_design.offline_payment_description_fallback'|trans({}, null, locale|default('fr')) }}</p>
                    {% endif %}
                {% endif %}
            </td>
        </tr>
    </table>

    <p style="margin:0;">{{ (mail_translation_prefix ~ '.footer')|trans({'%company%': company }, null, locale|default('fr')) }}</p>
{% endblock %}
