---
title: "`r params$title`"
author: "`r params$author`"
date: "`r format(Sys.time(), '%Y-%m-%d at %H:%M:%S %Z (UTC%z)')`"
output:
  pdf_document:
    toc: true
    toc_depth: 2
params:
  title: "(Pre)registration"
  author: ""
---

```{r, eval=TRUE, echo=FALSE}

knitr::opts_chunk$set(echo = FALSE, comment="");

if (!exists('headingLevel') || !is.numeric(headingLevel) || (length(headingLevel) != 1)) {
  headingLevel <- 0;
}

if (is.null(section)) {
  sectionsToShow <- x$form$sections$section_id;
} else {
  sectionsToShow <-
    intersect(
      x$form$sections$section_id,
      section
    );
}

```

```{r, results='asis', eval=TRUE}

for (section in sectionsToShow) {

  preregr::heading(
    "Section: ",
    x$form$sections[
      x$form$sections$section_id==section,
      "section_label"
    ],
    headingLevel=headingLevel + 1
  );
  
  item_ids <- x$form$items$item_id[x$form$items$section_id == section];
  item_labels <- x$form$items$item_label[x$form$items$section_id == section];
  names(item_labels) <- item_ids;

  for (currentItemId in item_ids) {
    
    preregr::heading(
      item_labels[currentItemId],
      headingLevel=headingLevel + 2
    );
    
    if (x$specs[[currentItemId]] == x$config$initialText) {
      cat0("*No content specified*\n");
    } else {
      cat(
       ifelse(!is.null(x$specs[[currentItemId]]$text) &&
                !is.na(x$specs[[currentItemId]]$text) &&
                nchar(x$specs[[currentItemId]]$text) > 0,
              x$specs[[currentItemId]]$text,
              "\n\n")
      );
    }
    
    cat0("\n\n(Item identifier: `", currentItemId, "`)\n\n");
    
  }

}

```
