{:*
    Neutral TS build-form
*:}

{:snippet; wrap-example >>
    <div class="">
        <div>
            {:;__eval__:}
        </div>
    </div>
:}

{:snippet; snippet-example >>
    <hr>
:}

{:snippet; post-value >>
    {:&;{:param; post-data :}->{:;current_field->attributes->name:}:}
:}

{:*
    Element INPUT
*:}
{:snippet; neutral-build-form-field:input >>
    <input
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}{:defined; current_field->value >>
            {:^snippet; neutral-build-form-field:input:{:;current_field->attributes->type:} :}
            {:^else;
                {:;:} value="{:snippet; post-value :}{:else; {:;current_field->value:} :}"
            :}
        :}>
:}

{:*
    Element INPUT checkbox value
*:}
{:snippet; neutral-build-form-field:input:checkbox >>
    {:;:} value="{:;current_field->value:}"
    {:filled; {:param; post-data :}->{:;current_field->attributes->name:} >>
       {:;:} checked
    :}
:}

{:*
    Element INPUT checkbox radio
*:}
{:snippet; neutral-build-form-field:input:radio >>
    {:;:} value="{:;current_field->value:}"
    {:same; /{:snippet; post-value :}/{:;current_field->value:}/ >>
       {:;:} checked
    :}
:}

{:*
    Element OUTPUT
*:}
{:snippet; neutral-build-form-field:output >>
    <output
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}{:^defined; current_field->value >>
            {:;:} value="{:snippet; post-value :}{:else; {:;current_field->value:} :}"
        :}></output>
:}

{:*
    Element LABEL
*:}
{:snippet; neutral-build-form-field:label >>
    <label
        {:^each; current_field->attributes name value >>
           {:;:} {:;name:}="{:;value:}"
    :}>{:trans; {:;current_field->text:} :}</label>
:}

{:*
    Element LEGEND
*:}
{:snippet; neutral-build-form-field:legend >>
    <legend
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
    :}>{:trans; {:;current_field->text:} :}</legend>
:}

{:*
    Element SELECT
*:}
{:snippet; neutral-build-form-field:select >>
    <select
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}>
        {:array; current_field->options >>  {:^;:}
            {:^each; current_field->options value opt >> {:;:}
                <option value="{:;value:}" {:snippet; neutral-build-form-field:select:selected:{:snippet; post-value :} :}>{:trans; {:;opt:} :}</option>
            :}
        :}{:else;
            {:^each; {:;current_field->options:} value opt >> {:;:}
                <option value="{:;value:}" {:snippet; neutral-build-form-field:select:selected :}>{:trans; {:;opt:} :}</option>
            :}
        :}
    </select>
:}

{:*
    Element SELECT selected
*:}
{:snippet; neutral-build-form-field:select:selected >>
    {:same; /{:snippet; post-value :}/{:;value:}/ >>
        selected
    :}
:}

{:*
    Element DATALIST
*:}
{:snippet; neutral-build-form-field:datalist >>
    <datalist
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}>
        {:array; current_field->options >>  {:^;:}
            {:^each; current_field->options value opt >> {:;:}
                <option value="{:trans; {:;opt:} :}"></option>
            :}
        :}
    </datalist>
:}

{:*
    Element TEXTAREA
*:}
{:snippet; neutral-build-form-field:textarea >>
    <textarea
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}>{:defined; current_field->value >>
            {:code; {:flg; {:;current_field->flags:} :} >>
                {:snippet; post-value :}{:else; {:;current_field->value:} :}
            :}
        :}</textarea>
:}

{:*
    Element BUTTON
*:}
{:snippet; neutral-build-form-field:button >>
    <button
        {:^each; current_field->attributes name value >>
            {:;:} {:;name:}="{:;value:}"
        :}{:defined; current_field->value >>
            {:;:} value="{:snippet; post-value :}{:else; {:;current_field->value:} :}"
        :}>{:trans; {:;current_field->text:} :}</button>
:}

{:*
    WRAP start
*:}
{:snippet; neutral-build-form-field:wrap-start >>
    <{:;current_field->tag:} {:^each; current_field->attributes name value >> {:;:} {:;name:}="{:;value:}":}>
:}

{:*
    WRAP end
*:}
{:snippet; neutral-build-form-field:wrap-end >>
    </{:;current_field->tag:}>
:}

{:*
    SNIPPET (custom field)
*:}
{:snippet; neutral-build-form-field:snippet >>
    {:snippet; {:;current_field->name:} :}
:}

{:*
    Parse form
*:}
{:snippet; neutral-build-form-parse >>
    {:^;:}
    <form {:each; {:param; form-name :}->form->attributes name value >> {:;name:}="{:;value:}" {:;:} :}>
    {:^each; {:param; form-name :}->fields none field_group >>
        {:array; field_group >>
            {:^each; field_group none current_field >>
                {:snippet; neutral-build-form-parse-field :}
            :}
        :}{:else;
            {:^each; {:;field_group:} none current_field >>
                {:snippet; neutral-build-form-parse-field :}
            :}
        :}
    :}
    </form>
:}

{:*
    Parse form field
*:}
{:snippet; neutral-build-form-parse-field >>
    {:^;:}
    {:defined; current_field->wrap >>
        {:array; current_field->wrap >>
            <div {:^each; current_field->wrap->attributes name value >> {:;:} {:;name:}="{:;value:}" :}>
                {:^snippet; neutral-build-form-field:{:;current_field->element:} :}
            {:^;:}</div>
            :}{:else;
                {:eval; {:snippet; neutral-build-form-field:{:;current_field->element:} :} >>
                    {:snippet; {:;current_field->wrap:} :}
                :}
        :}
    :}{:else;
        {:snippet; neutral-build-form-field:{:;current_field->element:} :}
    :}
:}
