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

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

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

{% macro build_form(elements, values) %}
    {% for name, element in elements %}
    <div class="mb-3 row">
        {% if element[0] == 'select' %}
        <label class="form-label col-3 col-form-label">{{ element[1].label }}{% if element[1].description %} - {{ element[1].description }}{% endif %}</label>
        <div class="col">
            <select class="form-select" name="config[{{ name }}]">
            {% for k, v in element[1].multiOptions %}
                <option value="{{ k }}"{% if k == values[name] %} selected{% endif %}/><label>{{ v }}</label>
            {% endfor %}
            </select>
        </div>
        {% elseif element[0] == 'multiselect' %}
        <label class="form-label col-3 col-form-label">{{ element[1].label }}{% if element[1].description %} - {{ element[1].description }}{% endif %}</label>
        <div class="col">
            <select class="form-select" name="config[{{ name }}]" multiple>
            {% for k, v in element[1].multiOptions %}
                <option value="{{ k }}"{% if k == values[name] %} selected{% endif %}/><label>{{ v }}</label>
            {% endfor %}
            </select>
        </div>
        {% elseif element[0] == 'radio' %}
        <label class="form-label col-3 col-form-label">{{ element[1].label }}{% if element[1].description %} - {{ element[1].description }}{% endif %}</label>
        <div class="col">
            {% for k, v in element[1].multiOptions %}
                <div class="form-check form-check-inline">
                    <input class="form-check-input" id="el-{{ name ~ k }}" type="radio" name="config[{{ name }}]" value="{{ k }}"{% if k == values[name] %} checked{% endif %}>
                    <label class="form-check-label" for="el-{{ name ~ k }}">{{ v }}</label>
                </div>
            {% endfor %}
        </div>
        {% elseif element[0] == 'textarea' %}
        <label class="form-label col-3 col-form-label" for="el-{{ name }}">{{ element[1].label }}{% if element[1].description %} - {{ element[1].description }}{% endif %}</label>
        <div class="col">
            <textarea class="form-control" id="el-{{ name }}" name="config[{{ name }}]" rows="20" required>{{ values[name] }}</textarea>
        </div>
        {% else %}
        <label class="form-label col-3 col-form-label" for="el-{{ name }}">{{ element[1].label }}{% if element[1].description %} - {{ element[1].description }}{% endif %}</label>
        <div class="col">
            <input class="form-control" id="el-{{ name }}" type="{{ element[0] }}" name="config[{{ name }}]" value="{{ values[name] }}"{% if not element[1].required is defined and not element[1].required == 'false' %} required{% endif %}>
        </div>
        {% endif %}
    </div>
    {% endfor %}
{% endmacro %}

{% macro currency_format(price, currency) %}
    {% set c = guest.currency_get({"code":currency}) %}
    {% set p = (price)|number %}
    {% if c.price_format == 1 %}
        {% set p = p|number('2', '.', '') %}
    {% elseif c.price_format == 2 %}
        {% set p = p|number('2', '.', ',') %}
    {% elseif c.price_format == 3 %}
        {% set p = p|number('2', ',', '.') %}
    {% elseif c.price_format == 4 %}
        {% set p = p|number('0', '', ',') %}
    {% elseif c.price_format == 5 %}
        {% set p = p|number(0, '', '') %}
    {% endif %}
    {{ c.format|replace({'{{price}}': p }) }}
{% endmacro %}

{% macro currency(price, currency) %}
    {% if currency == NULL %}
        {% set c = guest.cart_get_currency %}
    {% else %}
        {% set c = guest.currency_get({"code":currency}) %}
    {% endif %}

    {% set p = (price * c.conversion_rate) %}

    {% if c.price_format == 1 %}
        {% set p = p|number('2', '.', '') %}
    {% elseif c.price_format == 2 %}
        {% set p = p|number('2', '.', ',') %}
    {% elseif c.price_format == 3 %}
        {% set p = p|number('2', ',', '.') %}
    {% elseif c.price_format == 4 %}
        {% set p = p|number('0', '', ',') %}
    {% elseif c.price_format == 5 %}
        {% set p = p|number(0, '', '') %}
    {% endif %}

    {{ c.format|replace({'{{price}}': p }) }}
{% endmacro %}

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

{% macro period_name(period) %}
    {{ guest.system_period_title({"code":period}) }}
{% endmacro %}

{% macro markdown_quote(text) %}

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

{% macro bb_editor(selector) %}
{% if guest.extension_is_on({"mod":"wysiwyg"}) %}
{{ include('mod_wysiwyg_js.html.twig', { 'class': selector|trim('.#') }) }}
{% else %}
<!-- No WYSIWYG, no fancy stuff. Enable the WYSIWYG extension for a better management experience. -->
{% endif %}
{% endmacro %}

{% macro table_search() %}
<div style="position: relative;">
    <div class="dataTables_filter">
        <form method="get" action="">
            <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
            <input type="hidden" name="_url" value="{{request._url}}"/>
            <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 %}
