{% macro markdown_quote(text) %}
    {% for line in text|split('\n') %}
        > {{ line }}
    {% endfor %}
{% endmacro markdown_quote %}

{% macro recaptcha() %}

{% if guest.extension_is_on({"mod":"antispam"}) %}
    {% set config = guest.antispam_recaptcha %}
{% endif %}

{% if config is defined and config.enabled %}
            {% set provider = config.captcha_provider|default('recaptcha_v2') %}
            {% if provider == 'turnstile' %}
                {% if config.turnstile_site_key %}
                    <div class="mb-3">
                        <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
                        <div class="cf-turnstile" data-sitekey="{{ config.turnstile_site_key }}"></div>
                    </div>
                {% endif %}

            {% elseif provider == 'recaptcha_v2' %}
                {% if config.publickey %}
                    <div class="mb-3">
                        <script src='https://www.google.com/recaptcha/api.js' async defer></script>
                        <div class="g-recaptcha" data-sitekey="{{ config.publickey }}"></div>
                    </div>
                {% endif %}
            {% elseif provider == 'recaptcha_v3' %}
                {% if config.publickey %}
                    <script src="https://www.google.com/recaptcha/api.js?render={{ config.publickey }}" async defer></script>
                    <input type="hidden" name="g-recaptcha-response" value="">
                    <input type="hidden" name="g-recaptcha-action" value="fossbilling_submit">
                    <script>
                    (function () {
                        const script = document.currentScript;
                        const form = script.closest('form');
                        if (!form) {
                            return;
                        }

                        form.addEventListener('submit', function (event) {
                            if (form.dataset.recaptchaV3Verified === 'true') {
                                form.dataset.recaptchaV3Verified = '';
                                return;
                            }

                            event.preventDefault();
                            event.stopImmediatePropagation();

                            if (!window.grecaptcha) {
                                form.dataset.recaptchaV3Verified = 'true';
                                form.requestSubmit();
                                return;
                            }

                            const tokenField = form.querySelector('input[name="g-recaptcha-response"]');
                            const actionField = form.querySelector('input[name="g-recaptcha-action"]');
                            window.grecaptcha.ready(function () {
                                window.grecaptcha.execute('{{ config.publickey|e('js') }}', { action: actionField.value }).then(function (token) {
                                    tokenField.value = token;
                                    form.dataset.recaptchaV3Verified = 'true';
                                    form.requestSubmit();
                                }).catch(function () {
                                    form.dataset.recaptchaV3Verified = 'true';
                                    form.requestSubmit();
                                });
                            });
                        }, true);
                    }());
                    </script>
                {% endif %}
            {% elseif provider == 'hcaptcha' %}
                {% if config.hcaptcha_site_key %}
                    <div class="mb-3">
                        <script src='https://js.hcaptcha.com/1/api.js' async defer></script>
                        <div class="h-captcha" data-sitekey="{{ config.hcaptcha_site_key }}"></div>
                    </div>
                {% endif %}
            {% endif %}
        {% endif %}
{% endmacro recaptcha %}

{% macro selectbox(name, options, selected, required, nullOption) %}
    <select name="{{ name }}" class="form-control{% if required %} is-required{% endif %}">
        {% if nullOption %}<option value="">{{ nullOption }}</option>{% endif %}
        {% for val, label in options %}
            <option value="{{ val }}" {% if selected == val %}selected{% endif %}>{{ label }}</option>
        {% endfor %}
    </select>
{% endmacro selectbox %}

{% macro status_name(status) %}
    {% set status = status|replace({'_': " "})|title|trim %}
    {% if status == 'Active' %}
        {{ 'Active'|trans }}
    {% elseif status == 'Pending Setup' %}
        {{ 'Pending Setup'|trans }}
    {% elseif status == 'Failed Setup' %}
        {{ 'Failed Setup'|trans }}
    {% elseif status == 'Failed Renew' %}
        {{ 'Failed Renewal'|trans }}
    {% elseif status == 'Suspended' %}
        {{ 'Suspended'|trans }}
    {% elseif status == 'Canceled' %}
        {{ 'Canceled'|trans }}
    {% elseif status == 'Paid' %}
        {{ 'Paid'|trans }}
    {% elseif status == 'Unpaid' %}
        {{ 'Unpaid'|trans }}
    {% elseif status == 'Refunded' %}
        {{ 'Refunded'|trans }}
    {% else %}
        {{ status|trans }}
    {% endif %}
{% endmacro status_name %}


{% macro status_badge(status, type) %}
{% set status_title = status|replace({'_': " "})|title|trim %}
{% if status_title == 'Active' %}
    {% set status_label = 'Active'|trans %}
{% elseif status_title == 'Pending Setup' %}
    {% set status_label = 'Pending Setup'|trans %}
{% elseif status_title == 'Failed Setup' %}
    {% set status_label = 'Failed Setup'|trans %}
{% elseif status_title == 'Failed Renew' %}
    {% set status_label = 'Failed Renewal'|trans %}
{% elseif status_title == 'Suspended' %}
    {% set status_label = 'Suspended'|trans %}
{% elseif status_title == 'Canceled' %}
    {% set status_label = 'Canceled'|trans %}
{% elseif status_title == 'Paid' %}
    {% set status_label = 'Paid'|trans %}
{% elseif status_title == 'Unpaid' %}
    {% set status_label = 'Unpaid'|trans %}
{% elseif status_title == 'Refunded' %}
    {% set status_label = 'Refunded'|trans %}
{% else %}
    {% set status_label = status_title|trans %}
{% endif %}
{% if type == 'order' %}
    {% if status == 'active' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'pending_setup' %}{% set badge_class = 'bg-warning' %}
    {% elseif status == 'failed_setup' or status == 'suspended' or status == 'failed_renew' %}{% set badge_class = 'bg-danger' %}
    {% elseif status == 'canceled' %}{% set badge_class = 'bg-secondary' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'ticket' %}
    {% if status == 'open' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'on_hold' %}{% set badge_class = 'bg-warning' %}
    {% elseif status == 'closed' %}{% set badge_class = 'bg-secondary' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'invoice' %}
    {% if status == 'paid' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'unpaid' %}{% set badge_class = 'bg-warning' %}
    {% elseif status == 'refunded' %}{% set badge_class = 'bg-info' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'client' %}
    {% if status == 'active' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'suspended' %}{% set badge_class = 'bg-danger' %}
    {% elseif status == 'canceled' %}{% set badge_class = 'bg-secondary' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'subscription' %}
    {% if status == 'active' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'canceled' %}{% set badge_class = 'bg-secondary' %}
    {% elseif status == 'past_due' %}{% set badge_class = 'bg-danger' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'post' %}
    {% if status == 'published' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'draft' %}{% set badge_class = 'bg-secondary' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% elseif type == 'tx' %}
    {% if status == 'success' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'failed' %}{% set badge_class = 'bg-danger' %}
    {% elseif status == 'pending' %}{% set badge_class = 'bg-warning' %}
    {% elseif status == 'refunded' %}{% set badge_class = 'bg-info' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% else %}
    {% if status == 'active' or status == 'open' or status == 'paid' or status == 'published' %}{% set badge_class = 'bg-success' %}
    {% elseif status == 'pending_setup' or status == 'on_hold' or status == 'unpaid' or status == 'pending' %}{% set badge_class = 'bg-warning' %}
    {% elseif status == 'failed_setup' or status == 'suspended' or status == 'failed_renew' or status == 'canceled' or status == 'closed' or status == 'failed' %}{% set badge_class = 'bg-danger' %}
    {% else %}{% set badge_class = 'bg-primary' %}
    {% endif %}
{% endif %}
<span class="badge {{ badge_class }}">{{ status_label }}</span>
{% endmacro %}

{% macro empty_list(colspan, message) %}
<td class="text-muted" colspan="{{ colspan|default(5) }}">{{ (message|default('The list is empty'))|trans }}</td>
{% endmacro %}
