//~OPTIONS
{
  clientInclude:   ["../css/backref.css"],
  multilingual:    true,
  clientRendering: true,
}

//~ARGUMENTS
{
  enableLabels: true,
  folding:      false,
  foldingTitle: "@{{record['@title'] ? record['@title'] : 'New record'}}@",
}

//~TEMPLATE
%{{
  let language = args["@language"] ? args["@language"] : "en";
  fcf.each(args.value, (a_key, a_value) => {
}}%
  <div class="fcf-dbcontrols-backref-item">
    %{{ if (args.folding) { }}%
    <div name="folding-title" owner="@{{args.fcfId}}@" key="@{{a_key}}@" class="fcf-dbcontrols-backref-top-folding-title">
    @{{ fcf.tokenize(args.foldingTitle, {record: a_value})}}@
    </div>
    %{{ } }}%
    <div class="fcf-dbcontrols-backref-top-controls">
      %{{ if (args.folding) { }}%
      <div name="folding-button" owner="@{{args.fcfId}}@" key="@{{a_key}}@" class="fcf-dbcontrols-backref-folding-button" fcfEventClick="wrapper.onFoldItem(@{{a_key}}@)"></div>
      %{{ } }}%
      <div class="fcf-dbcontrols-backref-remove-button " fcfEventClick="wrapper.onRemoveItem(@{{a_key}}@)"></div>
    </div>
    <div name="folding" owner="@{{args.fcfId}}@" key="@{{a_key}}@" class="fcf-dbcontrols-backref-folding @{{!args.folding ? "active" : ""}}@">
      %{{
        let formArgs = {
          fcfAlias:             a_key.toString(),
          mode:                 fcf.modeMerge(args.mode, "?.?.subform"),
          enableTranslations:   false,
          language:             args["@language"],
          projection:           args.projection,
          value:                fcf.argRef(`value["${a_key}"]`),
          enableLabels:         fcf.argRef("enableLabels"),
        };
        if (args.value[a_key] && args.value[a_key]["@recorduuid"]){
          formArgs.mode = fcf.modeMerge(formArgs.mode, "add.?.?");
        }
        fcf.each(args["@languages"], (lang)=>{
          formArgs[`value@${lang}`] = fcf.argRef(`value@${lang}["${a_key}"]`);
        });
      }}%
    @{{ render.template("@dbcontrols:form", formArgs)  }}@
    </div>
    <div class="fcf-dbcontrols-backref-item-end">
    </div>
  </div>
%{{
  });
}}%

@{{ render.template("@controls:button", {title: fcf.t("New record"), fcfEventClick: "parent.onNewRecord()"}); }}@

<description>!{{Adding an entry to "@{{args.title}}@" field}}!</description>
