{%- macro tableRow(label, value, options = {}) -%}
  {%- set class = options.class|default({}) -%}
  {%- set prepend = options.prepend|default({}) %}
  {% set append = options.append|default({}) -%}
  {% apply spaceless %}
    {% if label and value %}
      [
{#        { text: '{{- label -}}', color: '#357792' },#}
        { text: '{{- label -}}' },
{#        { text: '{% if prepend %}{{ prepend }}{% endif %}{{- value -}}{% if append %}{{ append }}{% endif %}', color: '#001118', bold: true },#}
        { text: '{% if prepend %}{{ prepend }}{% endif %}{{- value -}}{% if append %}{{ append }}{% endif %}', bold: true },
      ],
    {% endif %}
  {% endapply %}
{%- endmacro -%}

{#<div id="iFrameContainer"></div>#}

<script src="/bundles/vonrotenbergrealestatelisting/js/pdfmake.js"></script>
<script src="/bundles/vonrotenbergrealestatelisting/js/vfs_fonts.js"></script>

<script>
  var docDefinition = {
    pageSize: 'A4',
    pageOrientation: 'portrait',
    pageMargins: [{{ 15|mm2pt }},{{ 35|mm2pt }},{{ 15|mm2pt }},{{ 15|mm2pt }}],
    defaultStyle: {
      font: 'Roboto',
      fontSize: 11,
      lineHeight: 1.1
    },
    styles:{
      h3: {
        fontSize: 13,
        bold: true,
        margin: [0,0,0,{{ 1.25|mm2pt }}]
      },
      h4: {
        fontSize: 13,
        // color: '#025577',
        margin: [0,0,0,{{ 1.25|mm2pt }}]
      }
    },
    header: function(currentPage, pageCount, pageSize) {
      if (currentPage === 1)
      {
        return [
          {
            stack: [
              { image: '{{ 'bundles/vonrotenbergrealestatelisting/images/pdf_logo.png'|imageDataUrl }}', width: {{ 35|mm2pt }}, alignment: 'center'},
            ],
            margin: [{{ 15|mm2pt }},{{ 5|mm2pt }},{{ 15|mm2pt }},0]
          },
        ];
      }
      return [];
    },
    footer: function(currentPage, pageCount, pageSize) {
      if (pageCount > 1)
      {
        return [
          {
            stack: [
              { text: 'Seite: '+currentPage+'/'+pageCount, alignment: 'right', margin: [0, 0, 0, 0], color: '#666'}
            ],
            margin: [{{ 15|mm2pt }},{{ 5|mm2pt }},{{ 15|mm2pt }},0]
          },
        ];
      }
      return [];
    },
    content: [
      {
        columns: [
          {
            stack: [
              {
                text: '{{ item.title }}{% if item.city %} in {{ item.city }}{% endif %}',
                fontSize: 20,
                margin: [0,0,0,{{ 2.5|mm2pt }}],
                color: '#025577'
              },
              {% if item.address %}
                {
                  text: '{{ item.address }}',
                  fontSize: 16,
                  margin: [0,0,0,{{ 2.5|mm2pt }}]
                },
              {% endif %}
              {
                text: 'Objekt-Nr.: {{ item.assetNo }}',
                fontSize: 14,
                color: '#025577',
                margin: [0,0,0,{{ 5|mm2pt }}]
              },
              {
                qr: '{{ item.qrUrl }}',
                fit: 70
              }
              {#{% if item.description %}
              {
                text: '{{ 'MSC.re_description'|trans({}, 'contao_default') }}',
                style: 'h3',
                margin: [0, {{ 10|mm2pt }}, 0, 0],
              },
              {
                text: '{{ item.description|raw|striptags }}',
                margin: [0, 0, 0,{{ 10|mm2pt }}],
              }
              {% endif %}#}
            ],
            width: '*'
          },
          {
            stack: [
              {
                image: '{{ item.teaserFigure.getImage().getFilePath()|imageDataUrl }}',
                fit: [{{ 85|mm2pt }},{{ 85|mm2pt }}],
                alignment: 'right'
              }
            ],
            width: '*'
          }
        ],
        margin: [0,0,0,{{ 10|mm2pt }}],
        columnGap: {{ 5|mm2pt }}
      },
      {% if item.description %}
      {
        text: '{{ 'MSC.re_description'|trans({}, 'contao_default') }}',
        style: 'h3'
      },
      {
        table: {
          widths: ['*'],
          body: [
            [
              {
                text: '{{ item.description|raw|striptags|replace({ '\r\n': '\\r\\n', '\n': '\\n', '\r': '\\r' }) }}',
                margin: [5,5,5,5],
                // color: '#357792'
              }
            ]
          ]
        },
        fillColor: '#f3fbff',
        layout: 'noBorders',
        margin: [0, 0, 0,{{ 10|mm2pt }}],
      },
      {% endif %}
      {
        columns: [
          {
            stack: [
              {
                text: '{{ 'MSC.re_properties'|trans({}, 'contao_default') }}',
                style: 'h3'
              },
              {
                table: {
                  widths: ['*'],
                  body: [
                    [
                      {
                        stack: [
                          {
                            table: {
                              widths: ['40%', '*'],
                              body: [
                                {{ _self.tableRow('tl_vr_real_estate_assets.livingSpace.0'|trans({}, 'contao_default'),item.livingSpace|number_format(2, ',', ''),{ append: ' m²'}) }}
                                {{ _self.tableRow('tl_vr_real_estate_assets.numberOfRooms.0'|trans({}, 'contao_default'),item.numberOfRooms) }}
                                {% set condition = 'REF.re_condition.' ~ item.condition %}
                                {{ _self.tableRow('tl_vr_real_estate_assets.condition.0'|trans({}, 'contao_default'),condition|trans({}, 'contao_default')) }}
                                {{ _self.tableRow('tl_vr_real_estate_assets.constructionYear.0'|trans({}, 'contao_default'),item.constructionYear) }}
                              ],
                            },
                            layout: 'noBorders',
                            margin: [5, 5, 5, 5],
                          },
                          {
                            table: {
                              widths: ['40%', '*'],
                              body: [
                                {% set heating = 'REF.re_heatingType.' ~ item.heatingType %}
                                {{ _self.tableRow('tl_vr_real_estate_assets.heatingType.0'|trans({}, 'contao_default'),heating|trans({}, 'contao_default')) }}
                                {% if item.energyConsumption and item.energyPassType %}
                                  {% set passType = 'REF.re_energyPassType.' ~ item.energyPassType %}
                                  {{ _self.tableRow('MSC.re_energyConsumption'|trans({}, 'contao_default') ~ ' ' ~ passType|trans({}, 'contao_default'),item.energyConsumption,{ append: ' kWh/(m²a)'}) }}
                                {% endif %}
                              ],
                            },
                            layout: 'noBorders',
                            margin: [5, 5, 5, 5],
                          },
                          {
                            text: '{{ 'MSC.re_rent'|trans({}, 'contao_default') }}',
                            style: 'h4',
                            margin: [5, 5, 5, 5],
                          },
                          {
                            table: {
                              widths: ['40%', '*'],
                              body: [
                                {{ _self.tableRow('tl_vr_real_estate_assets.coldRent.0'|trans({}, 'contao_default'),item.coldRent|number_format(2, ',', ''),{ append: ' €'}) }}
                                {{ _self.tableRow('Betriebskosten-vorauszahlungen',item.advanceOperatingCosts|number_format(2, ',', ''),{ append: ' €'}) }}
                              ],
                            },
                            layout: 'noBorders',
                            margin: [5, 5, 5, 5],
                          },
                          {
                            table: {
                              widths: ['40%', '*'],
                              body: [
                                {{ _self.tableRow('Genossenschafts-anteile',item.cooperativeShares) }}
                                {{ _self.tableRow('tl_vr_real_estate_assets.availability.0'|trans({}, 'contao_default'),item.availableFrom) }}
                              ],
                            },
                            layout: 'noBorders',
                            margin: [5, 5, 5, 5],
                          },
                        ]
                      }
                    ]
                  ]
                },
                fillColor: '#f3fbff',
                layout: 'noBorders',
              },
            ],
            width: '*',
          }{% if item.features is iterable  and item.features|length > 0 %},
          {
            stack: [
              {
                text: '{{ 'MSC.re_features'|trans({}, 'contao_default') }}',
                style: 'h3'
              },
              {
                table: {
                  widths: ['*'],
                  body: [
                    [
                      {
                        stack: [
                          {
                            table: {
                              widths: [10, '*'],
                              body: [
                                {% for feature in item.features %}
                                {% set featureTrans = 'REF.re_features.' ~ feature %}
                                [
                                  {
                                    canvas: [
                                      {
                                        type: 'polyline',
                                        lineWidth: 1,
                                        closePath: false,
                                        points: [{x: 0, y: 6}, {x: 4, y: 10}, {x: 10, y: 2}],
                                        lineColor: '#73160E'
                                      }
                                    ]
                                  },
                                  {
                                    text: '{{ featureTrans|trans({}, 'contao_default') }}',
                                    // color: '#357792'
                                  }
                                ],
                                {% endfor %}
                              ],
                            },
                            layout: 'noBorders',
                            margin: [5, 5, 5, 5],
                          },
                        ]
                      }
                    ]
                  ]
                },
                fillColor: '#f3fbff',
                layout: 'noBorders',
              },
            ],
            width: '*'
          }{% endif %}
        ],
        margin: [0,0,0,{{ 10|mm2pt }}],
        columnGap: {{ 5|mm2pt }},
        unbreakable: true
      },
      {% if item.floorPlansFigures|length > 0 %}
        {
          text: '{{ 'MSC.re_floorPlan'|trans({}, 'contao_default') }}',
          style: 'h3',
          pageBreak: 'before'
        },
        {% for image in item.floorPlansFigures %}
        {
          image: '{{ image.getImage().getFilePath()|imageDataUrl }}',
          fit: [{{ 180|mm2pt }},{{ 180|mm2pt }}],
          alignment: 'center'
        },
        {% endfor %}
      {% endif %}
      {% if item.galleryFigures|length > 1 %}
        {
          text: '{{ 'MSC.re_gallery'|trans({}, 'contao_default') }}',
          style: 'h3',
          pageBreak: 'before'
        },
        {
          columns: [
            {% for image in item.galleryFigures[1:] %}
            {% if loop.index % 2 > 0 and loop.index > 1 %}
                ],
                columnGap: {{ 5|mm2pt }},
                margin: [0,0,0,{{ 5|mm2pt }}],
              },
              {
                columns: [
            {% endif %}
            {
              image: '{{ image.getImage().getFilePath()|imageDataUrl }}',
              width: {{ 85|mm2pt }},
              alignment: 'left'
            },
            {% endfor %}
          ],
          columnGap: {{ 5|mm2pt }}
        }
      {% endif %}
    ],
    pageBreakBefore(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
      return currentNode.pageNumbers.length > 1 && currentNode.unbreakable;
    }
  };
  const pdfDocGenerator = pdfMake.createPdf(docDefinition);

  /*pdfDocGenerator.getDataUrl((dataUrl) => {
    const targetElement = document.querySelector('#iframeContainer');
    const iframe = document.createElement('object');
    iframe.data = dataUrl;
    iframe.setAttribute('style','width: 100%; height: 80vh;');
    iframe.setAttribute('type','application/pdf');
    targetElement.appendChild(iframe);
  });*/
  pdfDocGenerator.open();
  {#pdfDocGenerator.download('expose_{{ item.assetNo }}');#}
  // pdfDocGenerator.open({},window);
</script>