{% load widget_tweaks %}
{% load pyaa_format %}

{% for hidden_field in attrs.form.hidden_fields %}
    {{ hidden_field }}
{% endfor %}

{% if attrs.form.non_field_errors %}
<div class="alert alert-error mb-4">
    <ul class="list-disc list-inside">
        {% for error in attrs.form.non_field_errors %}
            <li>{{ error }}</li>
        {% endfor %}
    </ul>
</div>
{% endif %}

{% for field in attrs.form.visible_fields %}
{% with widget_name=field|widget_type %}
{% if field.field.widget.input_type == "checkbox" %}
<div class="form-control mb-4">
    <label class="flex items-start gap-3 cursor-pointer">
        {% if field.errors %}
            {% render_field field class="checkbox checkbox-error mt-0.5 shrink-0" %}
        {% else %}
            {% render_field field class="checkbox mt-0.5 shrink-0" %}
        {% endif %}
        <span class="text-sm leading-relaxed">{{ field.help_text|default:field.label|safe }}</span>
    </label>
    {% for error in field.errors %}
    <span class="text-xs text-error mt-1 ml-8">{{ error }}</span>
    {% endfor %}
</div>
{% else %}
<div class="form-control mb-4">
    <label class="label">
        <span class="label-text">{{ field.label }}</span>
    </label>

    {% if field.errors %}
        {% if field.field.widget.input_type == "radio" %}
            {% render_field field class="radio radio-error" %}
        {% elif field.field.widget.input_type == "select" or widget_name == "select" %}
            {% render_field field class="select select-bordered select-error w-full" %}
        {% elif widget_name == "textarea" %}
            {% render_field field class="textarea textarea-bordered textarea-error w-full" rows="6" %}
        {% elif field.field.widget.input_type == "file" or widget_name == "clearablefileinput" or widget_name == "fileinput" %}
            {% render_field field class="file-input file-input-bordered file-input-error w-full" %}
        {% else %}
            {% render_field field class="input input-bordered input-error w-full" %}
        {% endif %}
    {% else %}
        {% if field.field.widget.input_type == "radio" %}
            {% render_field field class="radio" %}
        {% elif field.field.widget.input_type == "select" or widget_name == "select" %}
            {% render_field field class="select select-bordered w-full" %}
        {% elif widget_name == "textarea" %}
            {% render_field field class="textarea textarea-bordered w-full" rows="6" %}
        {% elif field.field.widget.input_type == "file" or widget_name == "clearablefileinput" or widget_name == "fileinput" %}
            {% render_field field class="file-input file-input-bordered w-full" %}
        {% else %}
            {% render_field field class="input input-bordered w-full" %}
        {% endif %}
    {% endif %}

    {% if field.help_text %}
    <label class="label">
        <span class="label-text-alt text-base-content/60">{{ field.help_text|safe }}</span>
    </label>
    {% endif %}

    {% for error in field.errors %}
    <label class="label">
        <span class="label-text-alt text-error">{{ error }}</span>
    </label>
    {% endfor %}
</div>
{% endif %}
{% endwith %}
{% endfor %}
