{{$metaConfig := .Meta.Config}}
{{$current_values := (raw_value_of .ResourceValue .Meta)}}

<div class="qor-field collection-edit qor-fieldset-container" {{if $metaConfig.Max}}data-max-item="{{$metaConfig.Max}}" data-max-item-hint="Up to {{$metaConfig.Max}} {{meta_label .Meta}}"{{end}}>
  <label class="qor-field__label" for="{{.InputId}}">
    {{meta_label .Meta}}
  </label>

  <div class="qor-field__block">
    {{$meta := .Meta}}
    {{if $current_values}}
      {{range $index, $value := $current_values }}
        <fieldset class="qor-fieldset">
          {{if has_delete_permission $meta}}
            <button data-confirm="{{t "qor_admin.form.are_you_sure" "Are you sure?"}}" class="mdl-button qor-button--muted mdl-button--icon mdl-js-button qor-fieldset__delete" type="button">
              <i class="material-icons md-18">delete</i>
            </button>
          {{end}}
          {{render_nested_form $value (edit_sections $meta.Resource) $index}}
        </fieldset>
      {{end}}
    {{end}}

    {{if has_create_permission .Meta}}
      <fieldset class="qor-fieldset qor-fieldset--new">
        <button data-confirm="{{t "qor_admin.form.are_you_sure" "Are you sure?"}}" class="mdl-button qor-button--muted mdl-button--icon mdl-js-button qor-fieldset__delete" type="button">
          <i class="material-icons md-18">delete</i>
        </button>
        {{if $current_values}}
          {{render_nested_form $meta.Resource.NewStruct (new_sections $meta.Resource) (len $current_values)}}
        {{else}}
          {{render_nested_form $meta.Resource.NewStruct (new_sections $meta.Resource) 0}}
        {{end}}
      </fieldset>

      <button class="mdl-button mdl-button--primary qor-fieldset__add" type="button">
        {{t (printf "%v.attributes.add_%v" .BaseResource.ToParam (singular (meta_label .Meta))) (printf "Add %v" (singular (meta_label .Meta)))}}
      </button>
    {{end}}
  </div>
</div>
