{% macro input(name, value='', type='text', placeholder='', required=False) -%}
    <input
        type="{{ type }}"
        value="{{ value|e }}"
        name="{{ name }}"
        placeholder={{ placeholder }}
        class="form-control"
        {% if required %}
        required
        {% endif %}
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
    />
{%- endmacro %}

{% macro checkbox(name, value='', label='', id=uuid(), checked=False) -%}
    <input
        type="checkbox"
        value="{{ value|e }}"
        name="{{ name }}"
        id="{{id}}"
        class="form-check-input"
        {% if checked %}
        checked
        {% endif %}
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
    />
    <label
        id="{{id}}"
        class="form-check-label"
    >
        {{ label }}
    </label>
{%- endmacro %}

{% macro radio(name, value='', label='', id=uuid(), checked=False) -%}
    <input
        type="radio"
        value="{{ value|e }}"
        name="{{ name }}"
        id="{{id}}"
        class="form-check-input"
        {% if checked %}
        checked
        {% endif %}
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
    />
    <label
        id="{{id}}"
        class="form-check-label"
    >
        {{ label }}
    </label>
{%- endmacro %}

{% macro range(name, value='', max='', min='', id=uuid()) %}
    <label
        for="{{id}}"
        class="form-label"
    >
        {{ label }}
    </label>
    <input
        class="form-range"
        id="{{id}}"
        min="{{min}}"
        max="{{max}}"
        value="{{value}}"
        name="{{name}}"
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
    />
{%- endmacro %}

{% macro textarea(name, value='', placeholder='') -%}
    <textarea name="{{ name }}" class="form-control" placeholder="{{ placeholder }}">{{ value }}</textarea>
{%- endmacro %}

{% macro select(name, value='', options={}) -%}
    <select
        class="form-select"
        name="{{ name }}"
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
        >
    {% for opt_name, opt_value in options.items() %}
        <option
            value="{{ opt_name }}"
            {% if opt_name == value %}
                selected
            {% endif %}
        >{{ opt_value }}</option>
    {% endfor %}
    </select>
{%- endmacro %}

{% macro combobox(name, value='', multiple=False, options={}) -%}
    {% set uuid = uuid()%}
    <input
        name="{{ name }}"
        value="{{ value }}"
        list="{{uuid}}"
        class="form-control"
        {% if multiple %}
        multiple
        {% endif %}
        {% for key, value in kwargs.items() %}
        {{key}}="{{value}}"
        {% endfor %}
    >
    <datalist id="{{uuid}}">
    {% for opt_name, opt_value in options.items() %}
        <option
            value="{{ opt_name }}"
            {% if opt_name == value %}
                selected
            {% endif %}
        >{{ opt_value }}</option>
    {% endfor %}
    </datalist>
{%- endmacro %}

{% macro csrf(request) %}
    {% set sess = request.session() %}
    <input type='hidden' name='mitama_csrf_token' value='{{sess.mitama_csrf_token}}' />
{% endmacro %}
