{# Form field macros - renders the appropriate input for each field type #}

{% macro render_field(field, values={}, errors={}) %}
{% set value = values.get(field.name, field.default) if values else field.default %}
{% set error = errors.get(field.name, "") if errors else "" %}

<div class="form-control w-full {% if error %}has-error{% endif %}">
  {% if field.type == "checkbox" %}
    {# Checkbox: label is inline #}
    <label class="label cursor-pointer justify-start gap-3">
      <input type="checkbox" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="checkbox checkbox-primary"
             {% if value %}checked{% endif %} />
      <span class="label-text">{{ field.label }}</span>
    </label>

  {% elif field.source %}
    {# Search-select: dynamic search with autofill #}
    <label class="label" for="search-input-{{ field.name }}">
      <span class="label-text">
        {{ field.label }}
        {% if field.required %}<span class="text-error">*</span>{% endif %}
      </span>
    </label>
    {% include "fragments/search_select.html" %}

  {% else %}
    {# Standard fields: label above #}
    <label class="label" for="field-{{ field.name }}">
      <span class="label-text">
        {{ field.label }}
        {% if field.required %}<span class="text-error">*</span>{% endif %}
      </span>
    </label>

    {% if field.type == "textarea" %}
      <textarea id="field-{{ field.name }}" name="{{ field.name }}"
                data-dazzle-field="{{ field.name }}"
                class="textarea textarea-bordered w-full {% if error %}textarea-error{% endif %}"
                placeholder="{{ field.placeholder }}"
                {% if field.required %}required{% endif %}
                rows="4">{{ value | default("", true) }}</textarea>

    {% elif field.type == "select" %}
      <select id="field-{{ field.name }}" name="{{ field.name }}"
              data-dazzle-field="{{ field.name }}"
              class="select select-bordered w-full {% if error %}select-error{% endif %}"
              {% if field.required %}required{% endif %}>
        <option value="" disabled {% if not value %}selected{% endif %}>
          {{ field.placeholder or ("Select " + field.label) }}
        </option>
        {% for opt in field.options %}
        <option value="{{ opt.value }}" {% if value == opt.value %}selected{% endif %}>
          {{ opt.label }}
        </option>
        {% endfor %}
      </select>

    {% elif field.type == "date" %}
      <input id="field-{{ field.name }}" type="date" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="input input-bordered w-full {% if error %}input-error{% endif %}"
             value="{{ value | default('', true) }}"
             {% if field.required %}required{% endif %} />

    {% elif field.type == "datetime" %}
      <input id="field-{{ field.name }}" type="datetime-local" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="input input-bordered w-full {% if error %}input-error{% endif %}"
             value="{{ value | default('', true) }}"
             {% if field.required %}required{% endif %} />

    {% elif field.type == "money" %}
      {# Money widget: major-unit display input + hidden minor-unit value #}
      {% if field.extra.get('currency_fixed', true) %}
      {# Pinned currency — static prefix symbol #}
      <div data-dz-money="{{ field.name }}" data-dz-currency="{{ field.extra.currency_code }}"
           data-dz-scale="{{ field.extra.scale }}">
        <div class="join w-full">
          <span class="join-item btn btn-ghost no-animation pointer-events-none dz-money-prefix">
            {{ field.extra.symbol }}
          </span>
          <input type="text" inputmode="decimal" id="field-{{ field.name }}"
                 data-dazzle-field="{{ field.name }}" data-dz-money-display
                 class="input input-bordered join-item w-full {% if error %}input-error{% endif %}"
                 placeholder="0.00" {% if field.required %}required{% endif %} />
        </div>
        <input type="hidden" name="{{ field.name }}_minor" data-dz-money-minor
               value="{{ values.get(field.name ~ '_minor', '') if values else '' }}" />
        <input type="hidden" name="{{ field.name }}_currency" data-dz-money-currency
               value="{{ field.extra.currency_code }}" />
      </div>
      {% else %}
      {# Unpinned currency — dropdown selector #}
      <div data-dz-money="{{ field.name }}" data-dz-scale="{{ field.extra.scale }}">
        <div class="join w-full">
          <select data-dz-money-currency name="{{ field.name }}_currency"
                  class="select select-bordered join-item w-36">
            {% for opt in field.extra.get('currency_options', []) %}
            <option value="{{ opt.code }}" data-scale="{{ opt.scale }}"
                    data-symbol="{{ opt.symbol }}"
                    {% if opt.code == field.extra.currency_code %}selected{% endif %}>
              {{ opt.symbol }} {{ opt.code }}
            </option>
            {% endfor %}
          </select>
          <input type="text" inputmode="decimal" id="field-{{ field.name }}"
                 data-dazzle-field="{{ field.name }}" data-dz-money-display
                 class="input input-bordered join-item w-full {% if error %}input-error{% endif %}"
                 placeholder="0.00" {% if field.required %}required{% endif %} />
        </div>
        <input type="hidden" name="{{ field.name }}_minor" data-dz-money-minor
               value="{{ values.get(field.name ~ '_minor', '') if values else '' }}" />
      </div>
      {% endif %}

    {% elif field.type == "number" %}
      <input id="field-{{ field.name }}" type="number" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="input input-bordered w-full {% if error %}input-error{% endif %}"
             value="{{ value | default('', true) }}"
             placeholder="{{ field.placeholder }}"
             {% if field.required %}required{% endif %} />

    {% elif field.type == "email" %}
      <input id="field-{{ field.name }}" type="email" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="input input-bordered w-full {% if error %}input-error{% endif %}"
             value="{{ value | default('', true) }}"
             placeholder="{{ field.placeholder }}"
             {% if field.required %}required{% endif %} />

    {% else %}
      {# Default: text input #}
      <input id="field-{{ field.name }}" type="text" name="{{ field.name }}"
             data-dazzle-field="{{ field.name }}"
             class="input input-bordered w-full {% if error %}input-error{% endif %}"
             value="{{ value | default('', true) }}"
             placeholder="{{ field.placeholder }}"
             {% if field.required %}required{% endif %} />
    {% endif %}
  {% endif %}

  {% if error %}
  <label class="label">
    <span class="label-text-alt text-error">{{ error }}</span>
  </label>
  {% endif %}
</div>
{% endmacro %}
