{#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2026 Johannes Löhnert <loehnert.kde@gmx.de>
#}

{% if not field.rw %}
    <span class="fieldvalue">{{ format_readonly_value(field, value) }}</span>
{% else %}
    <form class="fieldsetter" action="field-{{ field.disp_id }}" method="POST" onsubmit="return submit_field_{{ field.type.datatype.name }}(this);">
        {% if field.type.datatype.name == "Enum" %}
            <select name="value">
                {% if field.type.nullable %}
                    <option value="" {% if value is none %}selected{% endif %}>--</option>
                {% endif %}
                {% set vkey = value if value else '' %}
                {% set keys = field.enum.keys()|list %}
                {% for key in keys|sort %}
                    <option value="{{ key }}" {% if key == vkey %}selected{% endif %}>{{ key }} {{ field.enum[key] }}</option>
                {% endfor %}
            </select>

        {# datetime types #}
        {% elif field.type.datatype.name in ("Datetime", "DayMonth", "Time", "HourMinutes") %}
            {% for name in datetime_ctl_map[field.type.datatype] %}
                {% if name in "-.: " %}
                    {{ name }}
                {% else %}
                    <input type="text" class="number" name="{{ name }}" value="{% if value %}{{ value[name] }}{% else %}0{% endif %}">
                {% endif %}
            {% endfor %}

        {% elif field.type.datatype.name == "TimeProgram" %}
            <input type="text" name="value" value="
                {%- for entry in value -%}
{{ entry.on.strftime("%H:%M") }}-{{ entry.off.strftime("%H:%M") }}{% if not loop.last %},{% endif %}
                {%- endfor -%}
            ">
        {% elif field.type.datatype.name == "String" %}
            <input type="text" name="value" value="{{ value }}">
        {% else %}
            <input type="text" class="number" name="value" value="{% if value %}{{ value }}{% else %}--{% endif %}">{{ field.unit }} <span class="rangehint">{{ format_range(field) }}</span>

        {% endif %}
        <input type="submit" value="Set">
    </form>
    {% if field.type.nullable %}
        <form
            class="fieldsetter"
            action="field-{{ field.disp_id }}"
            method="POST"
            onsubmit="submit_field_null(this);return false;"
        >
            <input type="submit" value="-- (reset)">
        </form>
    {% endif %}
{% endif %}