{%- 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 }}],
        info: {
            title: 'riel-expose-nr_{{ item.assetNo }}-{{ "now"|date("Y-m-d") }}',
            author: 'vonRotenberg Estate Manager',
            creator: 'vonRotenberg Estate Manager',
            producer: 'vonRotenberg Estate Manager',
            subject: 'Expose für Objekt-Nr. {{ item.assetNo }}'
        },
        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: '#1476BB',
                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 }}',
                                fontSize: 20,
                                margin: [0, 0, 0,{{ 2.5|mm2pt }}],
                                color: '#1476BB'
                            },
                            {% if item.address %}
                            {
                                text: '{{ item.address }}',
                                fontSize: 16,
                                margin: [0, 0, 0, 0]
                            },
                            {% endif %}
                            {% if item.city and item.postal %}
                            {
                                text: '{{ item.postal }} {{ item.city }}',
                                fontSize: 16,
                                margin: [0, 0, 0, 0]
                            },
                            {% endif %}
                            {
                                text: 'Objekt-Nr.: {{ item.assetNo }}',
                                fontSize: 14,
                                color: '#1476BB',
                                margin: [0, {{ 2.5|mm2pt }}, 0,{{ 5|mm2pt }}]
                            },
                            {
                                qr: '{{ item.qrUrl }}',
                                fit: 80
                            }
                            {# {% 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: [15, 10, 15, 10],
                                // color: '#357792'
                            }
                        ]
                    ]
                },
                fillColor: '#e6ebf1',
                layout: 'noBorders',
                margin: [0, 0, 0,{{ 8|mm2pt }}],
            },
            {% endif %}
            {
                columns: [
                    {
                        stack: [
                            {
                                text: '{{ 'MSC.re_properties'|trans({}, 'contao_default') }}',
                                style: 'h3'
                            },
                            {
                                table: {
                                    widths: ['*'],
                                    body: [
                                        [
                                            {
                                                stack: [
                                                    {
                                                        table: {
                                                            widths: ['40%', '*'],
                                                            body: [
                                                                {% if item.livingSpace is defined and item.livingSpace is not empty %}{{ _self.tableRow('tl_vr_real_estate_assets.livingSpace.0'|trans({}, 'contao_default'),item.livingSpace|number_format(2, ',', ''),{ append: ' m²'}) }}{% endif %}
                                                                {% if item.numberOfRooms is defined and item.numberOfRooms is not empty %}{{ _self.tableRow('tl_vr_real_estate_assets.numberOfRooms.0'|trans({}, 'contao_default'),item.numberOfRooms) }}{% endif %}
                                                                {% if item.condition is defined and item.condition is not empty %}{{ _self.tableRow('tl_vr_real_estate_assets.condition.0'|trans({}, 'contao_default'),('REF.re_condition.' ~ item.condition)|trans({}, 'contao_default')) }}{% endif %}
                                                                {% if item.constructionYear is defined and item.constructionYear is not empty %}{{ _self.tableRow('tl_vr_real_estate_assets.constructionYear.0'|trans({}, 'contao_default'),item.constructionYear) }}{% endif %}
                                                            ],
                                                        },
                                                        layout: 'noBorders',
                                                        margin: [15, 10, 15, 5],
                                                    },
                                                    {% if item.heatingType is not empty and item.energyConsumption is not empty and item.energyPassType is not empty %}
                                                    {
                                                        table: {
                                                            widths: ['40%', '*'],
                                                            body: [
                                                                {% if item.heatingType is defined and item.heatingType is not empty %}{{ _self.tableRow('tl_vr_real_estate_assets.heatingType.0'|trans({}, 'contao_default'),('REF.re_heatingType.' ~ item.heatingType)|trans({}, 'contao_default')) }}{% endif %}
                                                                {% if item.energyConsumption and item.energyPassType %}
                                                                {{ _self.tableRow('MSC.re_energyConsumption'|trans({}, 'contao_default')|replace({'&shy;' : '-'}) ~ ' ' ~ ('REF.re_energyPassType.' ~ item.energyPassType)|trans({}, 'contao_default'),item.energyConsumption,{ append: ' kWh/(m²a)'}) }}
                                                                {% endif %}
                                                            ],
                                                        },
                                                        layout: 'noBorders',
                                                        margin: [15, 5, 15, 5],
                                                    },
                                                    {% endif %}
                                                    {
                                                        text: '{{ 'MSC.re_rent'|trans({}, 'contao_default') }}',
                                                        style: 'h4',
                                                        margin: [15, 5, 15, 5],
                                                    },
                                                    {
                                                        table: {
                                                            widths: ['40%', '*'],
                                                            body: [
                                                                {% if item.coldRent > 0 %}{{ _self.tableRow('tl_vr_real_estate_assets.coldRent.0'|trans({}, 'contao_default'),item.coldRent|number_format(2, ',', ''),{ append: ' €'}) }}{% endif %}
                                                                {% if item.advanceOperatingCosts > 0 %}{{ _self.tableRow('tl_vr_real_estate_assets.advanceOperatingCosts.0'|trans({}, 'contao_default')|replace({'&shy;' : '-'}),item.advanceOperatingCosts|number_format(2, ',', ''),{ append: ' €'}) }}{% endif %}
                                                                {% if item.rent > 0 %}{{ _self.tableRow('tl_vr_real_estate_assets.rent.0'|trans({}, 'contao_default')|replace({'&shy;' : '-'}),item.rent|number_format(2, ',', ''),{ append: ' €'}) }}{% endif %}
                                                            ],
                                                        },
                                                        layout: 'noBorders',
                                                        margin: [15, 5, 15, 5],
                                                    },
                                                    {
                                                        table: {
                                                            widths: ['40%', '*'],
                                                            body: [
                                                                {{ _self.tableRow('tl_vr_real_estate_assets.deposit.0'|trans({}, 'contao_default')|replace({'&shy;' : '-'}),item.deposit|number_format(2, ',', ''),{ append: ' €'}) }}
                                                                {{ _self.tableRow('tl_vr_real_estate_assets.availability.0'|trans({}, 'contao_default'),item.availableFrom) }}
                                                            ],
                                                        },
                                                        layout: 'noBorders',
                                                        margin: [15, 5, 15, 10],
                                                    },
                                                ]
                                            }
                                        ]
                                    ]
                                },
                                fillColor: '#e6ebf1',
                                layout: 'noBorders',
                            },
                        ],
                        width: '*',
                    }{% if item.features is iterable  and item.features|length > 0 or item.parking is iterable  and item.parking|length > 0 %},
                    {
                        stack: [
                            {% if item.parking is iterable and item.parking|length > 0 %}
                            {
                                text: '{{ 'MSC.re_parking'|trans({}, 'contao_default') }}',
                                style: 'h3'
                            },
                            {
                                table: {
                                    widths: ['*'],
                                    body: [
                                        [
                                            {
                                                stack: [
                                                    {
                                                        table: {
                                                            widths: [10, '*'],
                                                            body: [
                                                                {% for spot in item.parking %}
                                                                {% set parkingTrans = 'REF.re_parking.' ~ spot %}
                                                                [
                                                                    {
                                                                        canvas: [
                                                                            {
                                                                                type: 'ellipse',
                                                                                x: 5, y: 6,
                                                                                lineColor: 'black',
                                                                                lineWidth: 1,
                                                                                r1: 5, r2: 5
                                                                            },
                                                                            {
                                                                                type: 'polyline',
                                                                                lineWidth: 1,
                                                                                closePath: false,
                                                                                points: [
                                                                                    {x: 3, y: 5},
                                                                                    {x: 5, y: 8},
                                                                                    {x: 10, y: 1}
                                                                                ],
                                                                                lineColor: '#1476BB'
                                                                            }
                                                                        ]
                                                                    },
                                                                    {
                                                                        text: '{{ parkingTrans|trans({}, 'contao_default') }}',
                                                                        // color: '#357792'
                                                                    }
                                                                ],
                                                                {% endfor %}
                                                            ],
                                                        },
                                                        layout: 'noBorders',
                                                        margin: [15, 10, 15, 10],
                                                    },
                                                ]
                                            }
                                        ]
                                    ]
                                },
                                fillColor: '#e6ebf1',
                                layout: 'noBorders',
                                margin: [0,0,0,10],
                            },
                            {% endif %}
                            {% if item.features is iterable and item.features|length > 0 %}
                                {
                                    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: 'ellipse',
                                                                                    x: 5, y: 6,
                                                                                    lineColor: 'black',
                                                                                    lineWidth: 1,
                                                                                    r1: 5, r2: 5
                                                                                },
                                                                                {
                                                                                    type: 'polyline',
                                                                                    lineWidth: 1,
                                                                                    closePath: false,
                                                                                    points: [
                                                                                        {x: 3, y: 5},
                                                                                        {x: 5, y: 8},
                                                                                        {x: 10, y: 1}
                                                                                    ],
                                                                                    lineColor: '#1476BB'
                                                                                }
                                                                            ]
                                                                        },
                                                                        {
                                                                            text: '{{ featureTrans|trans({}, 'contao_default') }}',
                                                                            // color: '#357792'
                                                                        }
                                                                    ],
                                                                    {% endfor %}
                                                                ],
                                                            },
                                                            layout: 'noBorders',
                                                            margin: [15, 10, 15, 10],
                                                        },
                                                    ]
                                                }
                                            ]
                                        ]
                                    },
                                    fillColor: '#e6ebf1',
                                    layout: 'noBorders',
                                },
                            {% endif %}
                        ],
                        width: '*'
                    }{% endif %}
                ],
                margin: [0, 0, 0,{{ 8|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>