{% macro compute_field_required(element, context) %}
    {% set result = false %}
    {% if element[1].required_when is defined and element[1].required_when is iterable %}
        {% set result = true %}
        {% for cond_key, cond_value in element[1].required_when %}
            {% if attribute(context, cond_key) is null or attribute(context, cond_key) != cond_value %}
                {% set result = false %}
            {% endif %}
        {% endfor %}
    {% elseif element[1].required is defined %}
        {% set result = element[1].required != 'false' %}
    {% else %}
        {% set result = false %}
    {% endif %}
    {{ result ? 'true' : 'false' }}
{% endmacro %}

{% macro build_form(elements, values, context) %}
    {% set ctx = context|default({}) %}
    <div class="row g-3">
    {% for name, element in elements %}
    {% set field_class = element[0] == 'textarea' ? 'col-12' : 'col-lg-6' %}
    {% set field_required = _self.compute_field_required(element, ctx) == 'true' %}
    <div class="{{ field_class }}">
        {% if element[0] == 'select' %}
            <label class="form-label">{{ element[1].label }}</label>
            {% if element[1].description %}<div class="form-text mt-0 mb-2">{{ element[1].description | markdown_to_html }}</div>{% endif %}
            <select class="form-select" name="config[{{ name }}]">
            {% for k, v in element[1].multiOptions %}
                <option value="{{ k }}"{% if k == values[name] %} selected{% endif %}>{{ v }}</option>
            {% endfor %}
            </select>
        {% elseif element[0] == 'multiselect' %}
            <label class="form-label">{{ element[1].label }}</label>
            {% if element[1].description %}<div class="form-text mt-0 mb-2">{{ (element[1].description ?? '')|markdown_to_html }}</div>{% endif %}
            <select class="form-select" name="config[{{ name }}]" multiple>
            {% for k, v in element[1].multiOptions %}
                <option value="{{ k }}"{% if values[name] is iterable and k in values[name] %} selected{% endif %}>{{ v }}</option>
            {% endfor %}
            </select>
        {% elseif element[0] == 'radio' %}
            <label class="form-label d-block">{{ element[1].label }}</label>
            {% if element[1].description %}<div class="form-text mt-0 mb-2">{{ (element[1].description ?? '')|markdown_to_html }}</div>{% endif %}
            <div class="d-flex flex-wrap gap-3">
            {% for k, v in element[1].multiOptions %}
                <label class="form-check">
                    <input class="form-check-input" id="el-{{ name ~ k }}" type="radio" name="config[{{ name }}]" value="{{ k }}"{% if k == values[name] %}checked{% endif %}>
                    <span class="form-check-label">{{ v }}</span>
                </label>
            {% endfor %}
            </div>
        {% elseif element[0] == 'textarea' %}
            <label class="form-label" for="el-{{ name }}">{{ element[1].label }}</label>
            {% if element[1].description %}<div class="form-text mt-0 mb-2">{{ (element[1].description ?? '')|markdown_to_html }}</div>{% endif %}
            <textarea class="form-control" id="el-{{ name }}" name="config[{{ name }}]" rows="20"
                {% if field_required %} required{% endif %}
                {% if element[1].required_when is defined and element[1].required_when.test_mode is defined %} data-required-when-test-mode="{{ element[1].required_when.test_mode ? 'true' : 'false' }}"{% endif %}>{{ values[name] }}</textarea>
        {% else %}
            <label class="form-label" for="el-{{ name }}">{{ element[1].label }}</label>
            {% if element[1].description %}<div class="form-text mt-0 mb-2">{{ (element[1].description ?? '')|markdown_to_html }}</div>{% endif %}
            <input class="form-control" id="el-{{ name }}" type="{{ element[0] }}" name="config[{{ name }}]" value="{{ values[name] }}"
                {% if field_required %} required{% endif %}
                {% if element[1].required_when is defined and element[1].required_when.test_mode is defined %} data-required-when-test-mode="{{ element[1].required_when.test_mode ? 'true' : 'false' }}"{% endif %}>
        {% endif %}
    </div>
    {% endfor %}
    </div>
{% endmacro build_form %}

{% macro status_name(status) %}
    {% if status is same as(null) or (status is empty and status != '0') %}
        -
    {% else %}
    {% 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 == 'Inactive' %}
        {{ 'Inactive'|trans }}
    {% elseif status == 'Enabled' %}
        {{ 'Enabled'|trans }}
    {% elseif status == 'Disabled' %}
        {{ 'Disabled'|trans }}
    {% elseif status == 'Draft' %}
        {{ 'Draft'|trans }}
    {% elseif status == 'Sent' %}
        {{ 'Sent'|trans }}
    {% elseif status == 'Sending' %}
        {{ 'Sending'|trans }}
    {% elseif status == 'Unsent' %}
        {{ 'Unsent'|trans }}
    {% elseif status == 'Paid' %}
        {{ 'Paid'|trans }}
    {% elseif status == 'Unpaid' %}
        {{ 'Unpaid'|trans }}
    {% elseif status == 'Refunded' %}
        {{ 'Refunded'|trans }}
    {% elseif status == 'Approved' %}
        {{ 'Approved'|trans }}
    {% elseif status == 'Processed' %}
        {{ 'Processed'|trans }}
    {% elseif status == 'Received' %}
        {{ 'Received'|trans }}
    {% elseif status == 'Error' %}
        {{ 'Error'|trans }}
    {% elseif status == 'Unknown' %}
        {{ 'Unknown'|trans }}
    {% elseif status == 'Complete' %}
        {{ 'Complete'|trans }}
    {% elseif status == 'Completed' %}
        {{ 'Completed'|trans }}
    {% elseif status == 'Success' %}
        {{ 'Success'|trans }}
    {% elseif status == 'Failed' %}
        {{ 'Failed'|trans }}
    {% else %}
        {{ status|trans }}
    {% endif %}
    {% endif %}
{% endmacro status_name %}

{% macro ip_lookup_link(ip) %}
    {% if ip is not empty %}
        <a href="{{ 'security/iplookup'|url({'ip': ip }) }}" target="_blank" class='ip_lookup_link'>{{ ip }}</a>
    {% else %}
        {{ ip }}
    {% endif %}
{% endmacro ip_lookup_link %}

{% macro q(bool) %}
    {% if bool %}
        {{ 'Yes'|trans }}
    {% else %}
        {{ 'No'|trans }}
    {% endif %}
{% endmacro q %}

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

{% macro selectboxtld(name, options, selected, required) %}
    <select class="form-select" name="{{ name }}"{% if required %}required{% endif %}>
        {% for data in options %}
            <option value="{{ data.tld }}" label="{{ data.tld }}"{% if selected == data.tld %}selected{% endif %}>{{ data.tld }}</option>
        {% endfor %}
    </select>
{% endmacro selectboxtld %}

{% 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 %}

{% macro table_search() %}
<div style="position: relative;">
    <div class="dataTables_filter">
        <form method="get" action="">
            <label>{{ 'Search:'|trans }} <input type="text" name="search" placeholder="{{ 'Enter search text...'|trans }}" value="{{ request.search }}"><div class="srch"></div></label>
        </form>
    </div>
</div>
{% endmacro table_search %}

{% macro text_badge(label, badge_class) %}
    <span class="badge {{ badge_class }}">{{ label }}</span>
{% endmacro %}

{% macro boolean_badge(value, true_label, false_label, true_class, false_class) %}
    {% if value %}
        {{ _self.text_badge(true_label, true_class|default('text-bg-success')) }}
    {% else %}
        {{ _self.text_badge(false_label, false_class|default('text-bg-secondary')) }}
    {% endif %}
{% endmacro %}

{% macro status_badge(status, type) %}
    {% set status_key = status|default('')|lower %}
    {% if type == 'order' %}
        {% if status_key == 'active' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'pending_setup' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'failed_setup' or status_key == 'suspended' or status_key == 'failed_renew' %}{% set badge_class = 'text-bg-danger' %}
        {% elseif status_key == 'canceled' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'ticket' %}
        {% if status_key == 'open' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'on_hold' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'closed' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'invoice' %}
        {% if status_key == 'paid' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'unpaid' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'refunded' %}{% set badge_class = 'text-bg-info' %}
        {% elseif status_key == 'canceled' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'client' %}
        {% if status_key == 'active' %}{% set badge_class = 'bg-green text-green-fg' %}
        {% elseif status_key == 'suspended' %}{% set badge_class = 'bg-red text-red-fg' %}
        {% elseif status_key == 'canceled' %}{% set badge_class = 'bg-secondary text-secondary-fg' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'admin' %}
        {% if status_key == 'active' %}{% set badge_class = 'bg-green text-green-fg' %}
        {% elseif status_key == 'inactive' %}{% set badge_class = 'bg-secondary text-secondary-fg' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'subscription' %}
        {% if status_key == 'active' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'canceled' %}{% set badge_class = 'text-bg-secondary' %}
        {% elseif status_key == 'past_due' %}{% set badge_class = 'text-bg-danger' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'post' %}
        {% if status_key == 'published' or status_key == 'active' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'draft' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'product' %}
        {% if status_key == 'enabled' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'disabled' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'massmailer' %}
        {% if status_key == 'sent' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'draft' %}{% set badge_class = 'text-bg-secondary' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'transaction' %}
        {% if status_key == 'approved' or status_key == 'processed' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'received' or status_key == 'processing' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'error' %}{% set badge_class = 'text-bg-danger' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'gateway_tx' %}
        {% if status_key == 'approved' or status_key == 'complete' or status_key == 'completed' or status_key == 'processed' or status_key == 'success' or status_key == 'succeeded' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'pending' or status_key == 'processing' or status_key == 'received' or status_key == 'requires_action' or status_key == 'requires_capture' or status_key == 'requires_confirmation' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'canceled' or status_key == 'cancelled' or status_key == 'error' or status_key == 'failed' or status_key == 'requires_payment_method' or status_key == 'unknown' %}{% set badge_class = 'text-bg-danger' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'mail_queue' %}
        {% if status_key == 'sent' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'sending' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'unsent' %}{% set badge_class = 'text-bg-danger' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% elseif type == 'tx' %}
        {% if status_key == 'success' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'failed' %}{% set badge_class = 'text-bg-danger' %}
        {% elseif status_key == 'pending' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'refunded' %}{% set badge_class = 'text-bg-info' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% else %}
        {% if status_key == 'active' or status_key == 'open' or status_key == 'paid' or status_key == 'published' %}{% set badge_class = 'text-bg-success' %}
        {% elseif status_key == 'pending_setup' or status_key == 'on_hold' or status_key == 'unpaid' or status_key == 'pending' %}{% set badge_class = 'text-bg-warning' %}
        {% elseif status_key == 'failed_setup' or status_key == 'suspended' or status_key == 'failed_renew' or status_key == 'canceled' or status_key == 'closed' or status_key == 'failed' %}{% set badge_class = 'text-bg-danger' %}
        {% else %}{% set badge_class = 'text-bg-primary' %}
        {% endif %}
    {% endif %}
    {{ _self.text_badge(_self.status_name(status), badge_class) }}
{% endmacro %}

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